Skip to content

Commit 47c634b

Browse files
Sample command to solve compile or upload issue is now shown to CLI users (#1647)
* Sample command to solve compile or upload issue is now shown to CLI users * Fix error strings Co-authored-by: per1234 <[email protected]> * Enhance error message if core is unknown * Fix compile error not being printed if output is json Co-authored-by: per1234 <[email protected]>
1 parent e9ade7e commit 47c634b

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

cli/compile/compile.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ import (
1919
"bytes"
2020
"context"
2121
"encoding/json"
22+
"errors"
23+
"fmt"
2224
"os"
25+
"strings"
2326

27+
"github.com/arduino/arduino-cli/arduino"
28+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2429
"github.com/arduino/arduino-cli/arduino/discovery"
2530
"github.com/arduino/arduino-cli/arduino/sketch"
2631
"github.com/arduino/arduino-cli/cli/arguments"
2732
"github.com/arduino/arduino-cli/cli/feedback"
33+
"github.com/arduino/arduino-cli/cli/globals"
2834
"github.com/arduino/arduino-cli/cli/output"
2935
"github.com/arduino/arduino-cli/commands"
3036
"github.com/arduino/arduino-cli/configuration"
@@ -262,8 +268,30 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
262268
BuilderResult: compileRes,
263269
Success: compileError == nil,
264270
})
265-
if compileError != nil && output.OutputFormat != "json" {
271+
if compileError != nil {
266272
feedback.Errorf(tr("Error during build: %v"), compileError)
273+
274+
// Check the error type to give the user better feedback on how
275+
// to resolve it
276+
var platformErr *arduino.PlatformNotFoundError
277+
if errors.As(compileError, &platformErr) {
278+
split := strings.Split(platformErr.Platform, ":")
279+
if len(split) < 2 {
280+
panic(tr("Platform ID is not correct"))
281+
}
282+
283+
pm := commands.GetPackageManager(inst.GetId())
284+
platform := pm.FindPlatform(&packagemanager.PlatformReference{
285+
Package: split[0],
286+
PlatformArchitecture: split[1],
287+
})
288+
289+
if platform != nil {
290+
feedback.Errorf(tr("Try running %s", fmt.Sprintf("`%s core install %s`", globals.VersionInfo.Application, platformErr.Platform)))
291+
} else {
292+
feedback.Errorf(tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform))
293+
}
294+
}
267295
os.Exit(errorcodes.ErrGeneric)
268296
}
269297
}

cli/upload/upload.go

+29
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ package upload
1717

1818
import (
1919
"context"
20+
"errors"
21+
"fmt"
2022
"os"
23+
"strings"
2124

25+
"github.com/arduino/arduino-cli/arduino"
26+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2227
"github.com/arduino/arduino-cli/arduino/sketch"
2328
"github.com/arduino/arduino-cli/cli/arguments"
2429
"github.com/arduino/arduino-cli/cli/errorcodes"
2530
"github.com/arduino/arduino-cli/cli/feedback"
31+
"github.com/arduino/arduino-cli/cli/globals"
2632
"github.com/arduino/arduino-cli/cli/instance"
33+
"github.com/arduino/arduino-cli/commands"
2734
"github.com/arduino/arduino-cli/commands/upload"
2835
"github.com/arduino/arduino-cli/i18n"
2936
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -109,6 +116,28 @@ func runUploadCommand(command *cobra.Command, args []string) {
109116
})
110117
if err != nil {
111118
feedback.Errorf(tr("Error during Upload: %v"), err)
119+
120+
// Check the error type to give the user better feedback on how
121+
// to resolve it
122+
var platformErr *arduino.PlatformNotFoundError
123+
if errors.As(err, &platformErr) {
124+
split := strings.Split(platformErr.Platform, ":")
125+
if len(split) < 2 {
126+
panic(tr("Platform ID is not correct"))
127+
}
128+
129+
pm := commands.GetPackageManager(instance.GetId())
130+
platform := pm.FindPlatform(&packagemanager.PlatformReference{
131+
Package: split[0],
132+
PlatformArchitecture: split[1],
133+
})
134+
135+
if platform != nil {
136+
feedback.Errorf(tr("Try running %s", fmt.Sprintf("`%s core install %s`", globals.VersionInfo.Application, platformErr.Platform)))
137+
} else {
138+
feedback.Errorf(tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform))
139+
}
140+
}
112141
os.Exit(errorcodes.ErrGeneric)
113142
}
114143

commands/upload/upload.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ func SupportedUserFields(ctx context.Context, req *rpc.SupportedUserFieldsReques
6767
}
6868

6969
_, platformRelease, _, boardProperties, _, err := pm.ResolveFQBN(fqbn)
70-
if err != nil {
70+
if platformRelease == nil {
71+
return nil, &arduino.PlatformNotFoundError{
72+
Platform: fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch),
73+
Cause: err,
74+
}
75+
} else if err != nil {
7176
return nil, &arduino.UnknownFQBNError{Cause: err}
7277
}
7378

@@ -286,7 +291,12 @@ func runProgramAction(pm *packagemanager.PackageManager,
286291

287292
// Find target board and board properties
288293
_, boardPlatform, board, boardProperties, buildPlatform, err := pm.ResolveFQBN(fqbn)
289-
if err != nil {
294+
if boardPlatform == nil {
295+
return &arduino.PlatformNotFoundError{
296+
Platform: fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch),
297+
Cause: err,
298+
}
299+
} else if err != nil {
290300
return &arduino.UnknownFQBNError{Cause: err}
291301
}
292302
logrus.

test/test_compile_part_4.py

+18
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,26 @@ def test_compile_non_installed_platform_with_wrong_packager_and_arch(run_command
392392
res = run_command(["compile", "-b", "wrong:avr:uno", sketch_path])
393393
assert res.failed
394394
assert "Error during build: Platform 'wrong:avr' not found: platform not installed" in res.stderr
395+
assert "Platform wrong:avr is not found in any known index" in res.stderr
395396

396397
# Compile with wrong arch
397398
res = run_command(["compile", "-b", "arduino:wrong:uno", sketch_path])
398399
assert res.failed
399400
assert "Error during build: Platform 'arduino:wrong' not found: platform not installed" in res.stderr
401+
assert "Platform arduino:wrong is not found in any known index" in res.stderr
402+
403+
404+
def test_compile_with_known_platform_not_installed(run_command, data_dir):
405+
assert run_command(["update"])
406+
407+
# Create a sketch
408+
sketch_name = "SketchSimple"
409+
sketch_path = Path(data_dir, sketch_name)
410+
assert run_command(["sketch", "new", sketch_path])
411+
412+
# Try to compile using a platform found in the index but not installed
413+
res = run_command(["compile", "-b", "arduino:avr:uno", sketch_path])
414+
assert res.failed
415+
assert "Error during build: Platform 'arduino:avr' not found: platform not installed" in res.stderr
416+
# Verifies command to fix error is shown to user
417+
assert "Try running `arduino-cli core install arduino:avr`" in res.stderr

0 commit comments

Comments
 (0)