From 08c14b4932e6e25d4e5ad4c9537effe3428e7ca4 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Mon, 31 Jan 2022 10:40:05 +0100 Subject: [PATCH 1/4] Sample command to solve compile or upload issue is now shown to CLI users --- cli/compile/compile.go | 27 +++++++++++++++++++++++++++ cli/upload/upload.go | 28 ++++++++++++++++++++++++++++ commands/upload/upload.go | 14 ++++++++++++-- test/test_compile_part_4.py | 18 ++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 05b83b53e90..e78a3cb3996 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -19,12 +19,17 @@ import ( "bytes" "context" "encoding/json" + "errors" "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" @@ -264,6 +269,28 @@ func runCompileCommand(cmd *cobra.Command, args []string) { }) if compileError != nil && output.OutputFormat != "json" { 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 core install %s`", globals.VersionInfo.Application, platformErr.Platform)) + } else { + feedback.Errorf(tr("Platform %s is not found in any known index", platformErr.Platform)) + } + } os.Exit(errorcodes.ErrGeneric) } } diff --git a/cli/upload/upload.go b/cli/upload/upload.go index c333795cfef..38eeb17bccc 100644 --- a/cli/upload/upload.go +++ b/cli/upload/upload.go @@ -17,13 +17,19 @@ package upload import ( "context" + "errors" "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 +115,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 core install %s`", globals.VersionInfo.Application, platformErr.Platform)) + } else { + feedback.Errorf(tr("Platform %s is not found in any known index", 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 From 8fc0a65562b35aeea39cb16758d9a164bd509b47 Mon Sep 17 00:00:00 2001 From: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com> Date: Tue, 1 Feb 2022 15:04:52 +0100 Subject: [PATCH 2/4] Fix error strings Co-authored-by: per1234 --- cli/compile/compile.go | 2 +- cli/upload/upload.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/compile/compile.go b/cli/compile/compile.go index e78a3cb3996..2b044beb486 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -286,7 +286,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) { }) if platform != nil { - feedback.Errorf(tr("Try running `%s core install %s`", globals.VersionInfo.Application, platformErr.Platform)) + 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", platformErr.Platform)) } diff --git a/cli/upload/upload.go b/cli/upload/upload.go index 38eeb17bccc..b4faa71eea3 100644 --- a/cli/upload/upload.go +++ b/cli/upload/upload.go @@ -132,7 +132,7 @@ func runUploadCommand(command *cobra.Command, args []string) { }) if platform != nil { - feedback.Errorf(tr("Try running `%s core install %s`", globals.VersionInfo.Application, platformErr.Platform)) + 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", platformErr.Platform)) } From 615e7048a3067910ffd023f74e6589f5db6e62df Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Tue, 1 Feb 2022 15:14:03 +0100 Subject: [PATCH 3/4] Enhance error message if core is unknown --- cli/compile/compile.go | 3 ++- cli/upload/upload.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 2b044beb486..3df2e4e4718 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -20,6 +20,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "os" "strings" @@ -288,7 +289,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) { 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", platformErr.Platform)) + 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 b4faa71eea3..f0a77a5d4dc 100644 --- a/cli/upload/upload.go +++ b/cli/upload/upload.go @@ -18,6 +18,7 @@ package upload import ( "context" "errors" + "fmt" "os" "strings" @@ -134,7 +135,7 @@ func runUploadCommand(command *cobra.Command, args []string) { 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", platformErr.Platform)) + 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) From c59ff6a496981fc07ea2e0faffeb4e2f1edad116 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Tue, 1 Feb 2022 15:14:20 +0100 Subject: [PATCH 4/4] Fix compile error not being printed if output is json --- cli/compile/compile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 3df2e4e4718..db7025326ae 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -268,7 +268,7 @@ 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