Skip to content

Commit 422a42c

Browse files
authored
Fix crash in upload if the upload tool platform is missing (#2211)
* Added integration test * Fixed panic in upload tool selection
1 parent 8cd7297 commit 422a42c

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

Diff for: commands/upload/upload.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,18 @@ func runProgramAction(pme *packagemanager.Explorer,
272272
Property: fmt.Sprintf("%s.tool.%s", action, port.Protocol), // TODO: Can be done better, maybe inline getToolID(...)
273273
Value: uploadToolID}
274274
} else if len(split) == 2 {
275+
p := pme.FindPlatform(&packagemanager.PlatformReference{
276+
Package: split[0],
277+
PlatformArchitecture: boardPlatform.Platform.Architecture,
278+
})
279+
if p == nil {
280+
return &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
281+
}
275282
uploadToolID = split[1]
276-
uploadToolPlatform = pme.GetInstalledPlatformRelease(
277-
pme.FindPlatform(&packagemanager.PlatformReference{
278-
Package: split[0],
279-
PlatformArchitecture: boardPlatform.Platform.Architecture,
280-
}),
281-
)
283+
uploadToolPlatform = pme.GetInstalledPlatformRelease(p)
284+
if uploadToolPlatform == nil {
285+
return &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
286+
}
282287
}
283288

284289
// Build configuration for upload

Diff for: internal/integrationtest/upload/upload_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,27 @@ func TestCompileAndUploadToPortWithBoardAutodetect(t *testing.T) {
579579
require.NoError(t, err)
580580
}
581581
}
582+
583+
func TestPanicIfUploadToolReferenceAnInexistentPlatform(t *testing.T) {
584+
// See: https://github.com/arduino/arduino-cli/issues/2042
585+
586+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
587+
defer env.CleanUp()
588+
589+
// Run update-index with our test index
590+
_, _, err := cli.Run("core", "install", "arduino:[email protected]")
591+
require.NoError(t, err)
592+
593+
// Install test data into datadir
594+
boardsTxt := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.5", "boards.txt")
595+
boardsTxtData, err := boardsTxt.ReadFile()
596+
require.NoError(t, err)
597+
boardsTxtData = append(boardsTxtData, []byte("uno.upload.tool.default=foo:bar\n")...)
598+
require.NoError(t, boardsTxt.WriteFile(boardsTxtData))
599+
600+
sketch, err := paths.New("..", "testdata", "bare_minimum").Abs()
601+
require.NoError(t, err)
602+
603+
stdout, _, _ := cli.Run("compile", "-b", "arduino:avr:uno", "--upload", sketch.String())
604+
require.NotContains(t, stdout, []byte("panic:"))
605+
}

0 commit comments

Comments
 (0)