Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash and provide better error when chip is wrong. #559

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions cmd/jag/commands/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ import (
// GetCachedFirmwareEnvelopePath returns the path to the cached firmware envelope.
// If necessary, downloads the envelope from the server first.
func GetCachedFirmwareEnvelopePath(ctx context.Context, version string, model string) (string, error) {
// Envelopes published in the Toit envelope repository are always lowercase.
model = strings.ToLower(model)
path, err := getFirmwareEnvelopePath(version, model)
if err != nil && err != os.ErrNotExist {
return "", err
}
if err == os.ErrNotExist {
// Download the envelope from the server.
if err := downloadPublishedFirmware(ctx, version, model); err != nil {
fmt.Printf("Failed to download firmware: %v\n", err)
switch model {
case "esp32", "esp32s3", "esp32c3", "esp32s2":
// These names are correct. Simply return the error.
case "ESP32-S3", "ESP32-C3", "ESP32-S2":
fmt.Printf("Chip model names must be lowercase without dashes. Please try again with 'esp32s3', 'esp32c3', or 'esp32s2'.\n")
default:
fmt.Printf("Make sure the model name is correct. You can find supported models at\n")
fmt.Printf("https://github.com/toitlang/envelopes/releases/tag/%s.\n", version)
}
return "", err
}
}
Expand All @@ -41,11 +53,10 @@ func isURL(path string) bool {

func downloadGzipped(ctx context.Context, url string, path string) error {
bundle, err := download(ctx, url)
defer bundle.Close()
if err != nil {
return err
}

defer bundle.Close()
Comment on lines -44 to +59

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh.. this seems unrelated, right?

Also (I'm not an expert at golang so please correct me) can deferring after the err != nil check lead to bundle not being closed because in case of an error the program will return before the defer is set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thinking is that if err == nil, then bundle will be nil or at least not open. If bundle is nil, then calling bundle.Close (even deferred) will cause a crash.

return storeGzipped(bundle, path)
}

Expand Down