diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 05b83b53e90..db7025326ae 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -19,12 +19,18 @@ import ( "bytes" "context" "encoding/json" + "errors" + "fmt" "os" + "strings" + "github.com/arduino/arduino-cli/arduino" + "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/arduino/discovery" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/cli/arguments" "github.com/arduino/arduino-cli/cli/feedback" + "github.com/arduino/arduino-cli/cli/globals" "github.com/arduino/arduino-cli/cli/output" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/configuration" @@ -262,8 +268,30 @@ func runCompileCommand(cmd *cobra.Command, args []string) { BuilderResult: compileRes, Success: compileError == nil, }) - if compileError != nil && output.OutputFormat != "json" { + if compileError != nil { feedback.Errorf(tr("Error during build: %v"), compileError) + + // Check the error type to give the user better feedback on how + // to resolve it + var platformErr *arduino.PlatformNotFoundError + if errors.As(compileError, &platformErr) { + split := strings.Split(platformErr.Platform, ":") + if len(split) < 2 { + panic(tr("Platform ID is not correct")) + } + + pm := commands.GetPackageManager(inst.GetId()) + platform := pm.FindPlatform(&packagemanager.PlatformReference{ + Package: split[0], + PlatformArchitecture: split[1], + }) + + if platform != nil { + feedback.Errorf(tr("Try running %s", fmt.Sprintf("`%s core install %s`", globals.VersionInfo.Application, platformErr.Platform))) + } else { + feedback.Errorf(tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)) + } + } os.Exit(errorcodes.ErrGeneric) } } diff --git a/cli/upload/upload.go b/cli/upload/upload.go index c333795cfef..f0a77a5d4dc 100644 --- a/cli/upload/upload.go +++ b/cli/upload/upload.go @@ -17,13 +17,20 @@ package upload import ( "context" + "errors" + "fmt" "os" + "strings" + "github.com/arduino/arduino-cli/arduino" + "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/cli/arguments" "github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/feedback" + "github.com/arduino/arduino-cli/cli/globals" "github.com/arduino/arduino-cli/cli/instance" + "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands/upload" "github.com/arduino/arduino-cli/i18n" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -109,6 +116,28 @@ func runUploadCommand(command *cobra.Command, args []string) { }) if err != nil { feedback.Errorf(tr("Error during Upload: %v"), err) + + // Check the error type to give the user better feedback on how + // to resolve it + var platformErr *arduino.PlatformNotFoundError + if errors.As(err, &platformErr) { + split := strings.Split(platformErr.Platform, ":") + if len(split) < 2 { + panic(tr("Platform ID is not correct")) + } + + pm := commands.GetPackageManager(instance.GetId()) + platform := pm.FindPlatform(&packagemanager.PlatformReference{ + Package: split[0], + PlatformArchitecture: split[1], + }) + + if platform != nil { + feedback.Errorf(tr("Try running %s", fmt.Sprintf("`%s core install %s`", globals.VersionInfo.Application, platformErr.Platform))) + } else { + feedback.Errorf(tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)) + } + } os.Exit(errorcodes.ErrGeneric) } diff --git a/commands/upload/upload.go b/commands/upload/upload.go index fd4af21a358..38286195ff2 100644 --- a/commands/upload/upload.go +++ b/commands/upload/upload.go @@ -67,7 +67,12 @@ func SupportedUserFields(ctx context.Context, req *rpc.SupportedUserFieldsReques } _, platformRelease, _, boardProperties, _, err := pm.ResolveFQBN(fqbn) - if err != nil { + if platformRelease == nil { + return nil, &arduino.PlatformNotFoundError{ + Platform: fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch), + Cause: err, + } + } else if err != nil { return nil, &arduino.UnknownFQBNError{Cause: err} } @@ -286,7 +291,12 @@ func runProgramAction(pm *packagemanager.PackageManager, // Find target board and board properties _, boardPlatform, board, boardProperties, buildPlatform, err := pm.ResolveFQBN(fqbn) - if err != nil { + if boardPlatform == nil { + return &arduino.PlatformNotFoundError{ + Platform: fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch), + Cause: err, + } + } else if err != nil { return &arduino.UnknownFQBNError{Cause: err} } logrus. diff --git a/test/test_compile_part_4.py b/test/test_compile_part_4.py index 757095f63ba..d2c16a64869 100644 --- a/test/test_compile_part_4.py +++ b/test/test_compile_part_4.py @@ -392,8 +392,26 @@ def test_compile_non_installed_platform_with_wrong_packager_and_arch(run_command res = run_command(["compile", "-b", "wrong:avr:uno", sketch_path]) assert res.failed assert "Error during build: Platform 'wrong:avr' not found: platform not installed" in res.stderr + assert "Platform wrong:avr is not found in any known index" in res.stderr # Compile with wrong arch res = run_command(["compile", "-b", "arduino:wrong:uno", sketch_path]) assert res.failed assert "Error during build: Platform 'arduino:wrong' not found: platform not installed" in res.stderr + assert "Platform arduino:wrong is not found in any known index" in res.stderr + + +def test_compile_with_known_platform_not_installed(run_command, data_dir): + assert run_command(["update"]) + + # Create a sketch + sketch_name = "SketchSimple" + sketch_path = Path(data_dir, sketch_name) + assert run_command(["sketch", "new", sketch_path]) + + # Try to compile using a platform found in the index but not installed + res = run_command(["compile", "-b", "arduino:avr:uno", sketch_path]) + assert res.failed + assert "Error during build: Platform 'arduino:avr' not found: platform not installed" in res.stderr + # Verifies command to fix error is shown to user + assert "Try running `arduino-cli core install arduino:avr`" in res.stderr