Skip to content

Commit 54422aa

Browse files
committed
Fix upload command not working as expected when certain flags are used
1 parent 2fc2b54 commit 54422aa

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

arduino/sketch/sketch.go

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ var tr = i18n.Tr
5858
// New creates an Sketch instance by reading all the files composing a sketch and grouping them
5959
// by file type.
6060
func New(path *paths.Path) (*Sketch, error) {
61+
if path == nil {
62+
return nil, fmt.Errorf(tr("sketch path is not valid"))
63+
}
64+
6165
path = path.Canonical()
6266
if !path.IsDir() {
6367
path = path.Parent()

arduino/sketch/sketch_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ func TestNew(t *testing.T) {
3131
mainFilePath := sketchFolderPath.Join(fmt.Sprintf("%s.ino", "SketchSimple"))
3232
otherFile := sketchFolderPath.Join("other.cpp")
3333

34+
sketch, err := New(nil)
35+
assert.Nil(t, sketch)
36+
assert.Error(t, err)
37+
3438
// Loading using Sketch folder path
35-
sketch, err := New(sketchFolderPath)
39+
sketch, err = New(sketchFolderPath)
3640
assert.Nil(t, err)
3741
assert.True(t, mainFilePath.EquivalentTo(sketch.MainFile))
3842
assert.True(t, sketchFolderPath.EquivalentTo(sketch.FullPath))

cli/upload/upload.go

+30-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/arduino/arduino-cli/commands/upload"
2929
"github.com/arduino/arduino-cli/i18n"
3030
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
31+
"github.com/arduino/go-paths-helper"
3132
"github.com/spf13/cobra"
3233
)
3334

@@ -81,27 +82,40 @@ func run(command *cobra.Command, args []string) {
8182
if len(args) > 0 {
8283
path = args[0]
8384
}
84-
sketchPath := arguments.InitSketchPath(path)
85+
var sketchPath *paths.Path
86+
var sk *sketch.Sketch = nil
87+
// If the user explicitly specified a directory that contains binaries to upload
88+
// use those, we don't really need a valid Sketch to upload in this case
89+
if importDir == "" && importFile == "" {
90+
sketchPath = arguments.InitSketchPath(path)
8591

86-
// .pde files are still supported but deprecated, this warning urges the user to rename them
87-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
88-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
89-
for _, f := range files {
90-
feedback.Error(f)
92+
// .pde files are still supported but deprecated, this warning urges the user to rename them
93+
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
94+
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
95+
for _, f := range files {
96+
feedback.Error(f)
97+
}
9198
}
92-
}
9399

94-
sk, err := sketch.New(sketchPath)
95-
if err != nil {
96-
feedback.Errorf(tr("Error during Upload: %v"), err)
97-
os.Exit(errorcodes.ErrGeneric)
100+
var err error
101+
sk, err = sketch.New(sketchPath)
102+
if err != nil {
103+
feedback.Errorf(tr("Error during Upload: %v"), err)
104+
os.Exit(errorcodes.ErrGeneric)
105+
}
98106
}
99107
discoveryPort, err := port.GetPort(instance, sk)
100108
if err != nil {
101109
feedback.Errorf(tr("Error during Upload: %v"), err)
102110
os.Exit(errorcodes.ErrGeneric)
103111
}
104112

113+
if fqbn == "" && sk != nil && sk.Metadata != nil {
114+
// If the user didn't specify an FQBN and a sketch.json file is present
115+
// read it from there.
116+
fqbn = sk.Metadata.CPU.Fqbn
117+
}
118+
105119
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
106120
Instance: instance,
107121
Fqbn: fqbn,
@@ -118,10 +132,14 @@ func run(command *cobra.Command, args []string) {
118132
fields = arguments.AskForUserFields(userFieldRes.UserFields)
119133
}
120134

135+
if sketchPath != nil {
136+
path = sketchPath.String()
137+
}
138+
121139
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
122140
Instance: instance,
123141
Fqbn: fqbn,
124-
SketchPath: sketchPath.String(),
142+
SketchPath: path,
125143
Port: discoveryPort.ToRPC(),
126144
Verbose: verbose,
127145
Verify: verify,

test/test_upload.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_upload_with_input_dir_flag(run_command, data_dir, detected_boards):
7171
assert run_command(f"compile -b {fqbn} {sketch_path} --output-dir {output_dir}")
7272

7373
# Upload with --input-dir flag
74-
assert run_command(f"upload -b {fqbn} -p {address} --input-dir {output_dir} {sketch_path}")
74+
assert run_command(f"upload -b {fqbn} -p {address} --input-dir {output_dir}")
7575

7676

7777
def test_upload_with_input_file_flag(run_command, data_dir, detected_boards):
@@ -320,7 +320,7 @@ def test_upload_with_input_dir_containing_multiple_binaries(run_command, data_di
320320
assert res.failed
321321
assert (
322322
"Error during Upload: "
323-
+ "retrieving build artifacts: "
323+
+ "Error finding build artifacts: "
324324
+ "autodetect build artifact: "
325325
+ "multiple build artifacts found:"
326326
in res.stderr
@@ -358,7 +358,7 @@ def test_compile_and_upload_combo_sketch_with_mismatched_casing(run_command, dat
358358
# Try to compile
359359
res = run_command(f"compile --clean -b {board.fqbn} -u -p {board.address} {sketch_path}")
360360
assert res.failed
361-
assert "Error during build: opening sketch: no valid sketch found" in res.stderr
361+
assert "Error during build: Can't open sketch: no valid sketch found in" in res.stderr
362362

363363

364364
def test_upload_sketch_with_mismatched_casing(run_command, data_dir, detected_boards, wait_for_board):
@@ -381,4 +381,4 @@ def test_upload_sketch_with_mismatched_casing(run_command, data_dir, detected_bo
381381
# searching for binaries since the sketch is not valid
382382
res = run_command(f"upload -b {board.fqbn} -p {board.address} {sketch_path}")
383383
assert res.failed
384-
assert "Error during Upload: opening sketch: no valid sketch found" in res.stderr
384+
assert "Error during Upload: no valid sketch found in" in res.stderr

0 commit comments

Comments
 (0)