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

Sample command to solve compile or upload issue is now shown to CLI users #1647

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
Expand Down
29 changes: 29 additions & 0 deletions cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
14 changes: 12 additions & 2 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions test/test_compile_part_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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