Skip to content

Commit 6cf5613

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

File tree

6 files changed

+77
-47
lines changed

6 files changed

+77
-47
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,

i18n/data/en.po

+30-26
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ msgstr "Available"
232232
msgid "Available Commands:"
233233
msgstr "Available Commands:"
234234

235-
#: cli/upload/upload.go:61
235+
#: cli/upload/upload.go:62
236236
msgid "Binary file to upload."
237237
msgstr "Binary file to upload."
238238

@@ -552,7 +552,7 @@ msgstr "Detects and displays a list of boards connected to the current computer.
552552
msgid "Directory containing binaries for debug."
553553
msgstr "Directory containing binaries for debug."
554554

555-
#: cli/upload/upload.go:60
555+
#: cli/upload/upload.go:61
556556
msgid "Directory containing binaries to upload."
557557
msgstr "Directory containing binaries to upload."
558558

@@ -573,7 +573,7 @@ msgid "Do not install dependencies."
573573
msgstr "Do not install dependencies."
574574

575575
#: cli/burnbootloader/burnbootloader.go:58
576-
#: cli/upload/upload.go:65
576+
#: cli/upload/upload.go:66
577577
msgid "Do not perform the actual upload, just log out actions"
578578
msgstr "Do not perform the actual upload, just log out actions"
579579

@@ -725,10 +725,10 @@ msgstr "Error during JSON encoding of the output: %v"
725725
#: cli/compile/compile.go:202
726726
#: cli/compile/compile.go:212
727727
#: cli/compile/compile.go:244
728-
#: cli/upload/upload.go:96
729-
#: cli/upload/upload.go:101
730-
#: cli/upload/upload.go:111
731-
#: cli/upload/upload.go:134
728+
#: cli/upload/upload.go:103
729+
#: cli/upload/upload.go:109
730+
#: cli/upload/upload.go:125
731+
#: cli/upload/upload.go:152
732732
msgid "Error during Upload: %v"
733733
msgstr "Error during Upload: %v"
734734

@@ -1086,7 +1086,7 @@ msgstr "Force skip of post-install scripts (if the CLI is running interactively)
10861086
#: cli/burnbootloader/burnbootloader.go:53
10871087
#: cli/compile/compile.go:85
10881088
#: cli/debug/debug.go:62
1089-
#: cli/upload/upload.go:58
1089+
#: cli/upload/upload.go:59
10901090
msgid "Fully Qualified Board Name, e.g.: arduino:avr:uno"
10911091
msgstr "Fully Qualified Board Name, e.g.: arduino:avr:uno"
10921092

@@ -1575,12 +1575,12 @@ msgid "Optional, suppresses almost every output."
15751575
msgstr "Optional, suppresses almost every output."
15761576

15771577
#: cli/compile/compile.go:98
1578-
#: cli/upload/upload.go:63
1578+
#: cli/upload/upload.go:64
15791579
msgid "Optional, turns on verbose mode."
15801580
msgstr "Optional, turns on verbose mode."
15811581

15821582
#: cli/compile/compile.go:109
1583-
#: cli/upload/upload.go:64
1583+
#: cli/upload/upload.go:65
15841584
msgid "Optional, use the specified programmer to upload."
15851585
msgstr "Optional, use the specified programmer to upload."
15861586

@@ -1947,7 +1947,7 @@ msgstr "Sketch uses {0} bytes ({2}%%) of program storage space. Maximum is {1} b
19471947

19481948
#: cli/compile/compile.go:137
19491949
#: cli/sketch/archive.go:66
1950-
#: cli/upload/upload.go:88
1950+
#: cli/upload/upload.go:94
19511951
msgid "Sketches with .pde extension are deprecated, please rename the following files to .ino:"
19521952
msgstr "Sketches with .pde extension are deprecated, please rename the following files to .ino:"
19531953

@@ -2219,11 +2219,11 @@ msgstr "Upgrades one or all installed platforms to the latest version."
22192219
msgid "Upgrading platform %[1]s with %[2]s"
22202220
msgstr "Upgrading platform %[1]s with %[2]s"
22212221

2222-
#: cli/upload/upload.go:50
2222+
#: cli/upload/upload.go:51
22232223
msgid "Upload Arduino sketches."
22242224
msgstr "Upload Arduino sketches."
22252225

2226-
#: cli/upload/upload.go:51
2226+
#: cli/upload/upload.go:52
22272227
msgid "Upload Arduino sketches. This does NOT compile the sketch prior to upload."
22282228
msgstr "Upload Arduino sketches. This does NOT compile the sketch prior to upload."
22292229

@@ -2252,7 +2252,7 @@ msgid "Upload the bootloader."
22522252
msgstr "Upload the bootloader."
22532253

22542254
#: cli/compile/compile.go:218
2255-
#: cli/upload/upload.go:117
2255+
#: cli/upload/upload.go:131
22562256
msgid "Uploading to specified board using %s protocol requires the following info:"
22572257
msgstr "Uploading to specified board using %s protocol requires the following info:"
22582258

@@ -2320,7 +2320,7 @@ msgstr "VERSION_NUMBER"
23202320

23212321
#: cli/burnbootloader/burnbootloader.go:55
23222322
#: cli/compile/compile.go:102
2323-
#: cli/upload/upload.go:62
2323+
#: cli/upload/upload.go:63
23242324
msgid "Verify uploaded binary after the upload."
23252325
msgstr "Verify uploaded binary after the upload."
23262326

@@ -2443,7 +2443,7 @@ msgstr "calling %[1]s: %[2]w"
24432443
msgid "can't find latest release of %s"
24442444
msgstr "can't find latest release of %s"
24452445

2446-
#: arduino/sketch/sketch.go:101
2446+
#: arduino/sketch/sketch.go:105
24472447
msgid "can't find main Sketch file in %s"
24482448
msgstr "can't find main Sketch file in %s"
24492449

@@ -2535,7 +2535,7 @@ msgstr "creating temp dir for extraction: %s"
25352535
msgid "data section exceeds available space in board"
25362536
msgstr "data section exceeds available space in board"
25372537

2538-
#: arduino/sketch/sketch.go:207
2538+
#: arduino/sketch/sketch.go:211
25392539
msgid "decoding sketch metadata: %s"
25402540
msgstr "decoding sketch metadata: %s"
25412541

@@ -2587,7 +2587,7 @@ msgstr "downloading %[1]s tool: %[2]s"
25872587
msgid "empty board identifier"
25882588
msgstr "empty board identifier"
25892589

2590-
#: arduino/sketch/sketch.go:196
2590+
#: arduino/sketch/sketch.go:200
25912591
msgid "encoding sketch metadata: %s"
25922592
msgstr "encoding sketch metadata: %s"
25932593

@@ -2607,7 +2607,7 @@ msgstr "error processing response from server"
26072607
msgid "error querying Arduino Cloud Api"
26082608
msgstr "error querying Arduino Cloud Api"
26092609

2610-
#: cli/upload/upload.go:72
2610+
#: cli/upload/upload.go:73
26112611
msgid "error: %s and %s flags cannot be used together"
26122612
msgstr "error: %s and %s flags cannot be used together"
26132613

@@ -2710,7 +2710,7 @@ msgstr "getting parent dir of %[1]s: %[2]s"
27102710
msgid "getting tool dependencies for platform %[1]s: %[2]s"
27112711
msgstr "getting tool dependencies for platform %[1]s: %[2]s"
27122712

2713-
#: arduino/sketch/sketch.go:151
2713+
#: arduino/sketch/sketch.go:155
27142714
msgid "importing sketch metadata: %s"
27152715
msgstr "importing sketch metadata: %s"
27162716

@@ -2942,7 +2942,7 @@ msgstr "moving extracted archive to destination dir: %s"
29422942
msgid "multiple build artifacts found: '%[1]s' and '%[2]s'"
29432943
msgstr "multiple build artifacts found: '%[1]s' and '%[2]s'"
29442944

2945-
#: arduino/sketch/sketch.go:73
2945+
#: arduino/sketch/sketch.go:77
29462946
msgid "multiple main sketch files found (%[1]v, %[2]v)"
29472947
msgstr "multiple main sketch files found (%[1]v, %[2]v)"
29482948

@@ -2970,7 +2970,7 @@ msgstr "no unique root dir in archive, found '%[1]s' and '%[2]s'"
29702970
msgid "no upload port provided"
29712971
msgstr "no upload port provided"
29722972

2973-
#: arduino/sketch/sketch.go:259
2973+
#: arduino/sketch/sketch.go:263
29742974
msgid "no valid sketch found in %[1]s: missing %[2]s"
29752975
msgstr "no valid sketch found in %[1]s: missing %[2]s"
29762976

@@ -3095,7 +3095,7 @@ msgstr "reading directory %[1]s: %[2]s"
30953095
msgid "reading file %[1]s: %[2]s"
30963096
msgstr "reading file %[1]s: %[2]s"
30973097

3098-
#: arduino/sketch/sketch.go:229
3098+
#: arduino/sketch/sketch.go:233
30993099
msgid "reading files: %v"
31003100
msgstr "reading files: %v"
31013101

@@ -3123,7 +3123,7 @@ msgstr "reading library_index.json: %s"
31233123
msgid "reading package root dir: %s"
31243124
msgstr "reading package root dir: %s"
31253125

3126-
#: arduino/sketch/sketch.go:188
3126+
#: arduino/sketch/sketch.go:192
31273127
msgid "reading sketch metadata %[1]s: %[2]s"
31283128
msgstr "reading sketch metadata %[1]s: %[2]s"
31293129

@@ -3185,6 +3185,10 @@ msgstr "searching package root dir: %s"
31853185
msgid "setting DTR to OFF"
31863186
msgstr "setting DTR to OFF"
31873187

3188+
#: arduino/sketch/sketch.go:62
3189+
msgid "sketch path is not valid"
3190+
msgstr "sketch path is not valid"
3191+
31883192
#: cli/board/attach.go:35
31893193
#: cli/sketch/archive.go:38
31903194
msgid "sketchPath"
@@ -3339,7 +3343,7 @@ msgstr "unknown package %s"
33393343
msgid "unknown platform %s:%s"
33403344
msgstr "unknown platform %s:%s"
33413345

3342-
#: arduino/sketch/sketch.go:142
3346+
#: arduino/sketch/sketch.go:146
33433347
msgid "unknown sketch file extension '%s'"
33443348
msgstr "unknown sketch file extension '%s'"
33453349

@@ -3359,7 +3363,7 @@ msgstr "upgrade everything to the latest version"
33593363
msgid "uploading error: %s"
33603364
msgstr "uploading error: %s"
33613365

3362-
#: arduino/sketch/sketch.go:212
3366+
#: arduino/sketch/sketch.go:216
33633367
msgid "writing sketch metadata %[1]s: %[2]s"
33643368
msgstr "writing sketch metadata %[1]s: %[2]s"
33653369

i18n/rice-box.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)