Skip to content

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

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

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -264,6 +269,28 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
})
if compileError != nil && output.OutputFormat != "json" {
Copy link
Contributor

Choose a reason for hiding this comment

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

All the added logic happens if output.OutputFormat != "json", this means that no error is returned in case the output is 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)
}
}
Expand Down
28 changes: 28 additions & 0 deletions cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

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