Skip to content

Commit 56419ec

Browse files
committed
Fix upload command not working as expected when certain flags are used (#1429)
* Fix upload command not working as expected when certain flags are used * Fix absurd upload corner case
1 parent fefc306 commit 56419ec

File tree

6 files changed

+52
-29
lines changed

6 files changed

+52
-29
lines changed

Diff for: 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()

Diff for: 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))

Diff for: cli/upload/upload.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,31 @@ func run(command *cobra.Command, args []string) {
8484
sketchPath := arguments.InitSketchPath(path)
8585

8686
// .pde files are still supported but deprecated, this warning urges the user to rename them
87-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
87+
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 && importDir == "" && importFile == "" {
8888
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
8989
for _, f := range files {
9090
feedback.Error(f)
9191
}
9292
}
9393

9494
sk, err := sketch.New(sketchPath)
95-
if err != nil {
95+
if err != nil && importDir == "" && importFile == "" {
9696
feedback.Errorf(tr("Error during Upload: %v"), err)
9797
os.Exit(errorcodes.ErrGeneric)
9898
}
99+
99100
discoveryPort, err := port.GetPort(instance, sk)
100101
if err != nil {
101102
feedback.Errorf(tr("Error during Upload: %v"), err)
102103
os.Exit(errorcodes.ErrGeneric)
103104
}
104105

106+
if fqbn == "" && sk != nil && sk.Metadata != nil {
107+
// If the user didn't specify an FQBN and a sketch.json file is present
108+
// read it from there.
109+
fqbn = sk.Metadata.CPU.Fqbn
110+
}
111+
105112
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
106113
Instance: instance,
107114
Fqbn: fqbn,
@@ -118,10 +125,14 @@ func run(command *cobra.Command, args []string) {
118125
fields = arguments.AskForUserFields(userFieldRes.UserFields)
119126
}
120127

128+
if sketchPath != nil {
129+
path = sketchPath.String()
130+
}
131+
121132
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
122133
Instance: instance,
123134
Fqbn: fqbn,
124-
SketchPath: sketchPath.String(),
135+
SketchPath: path,
125136
Port: discoveryPort.ToRPC(),
126137
Verbose: verbose,
127138
Verify: verify,

Diff for: i18n/data/en.po

+18-14
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ msgstr "Error during JSON encoding of the output: %v"
726726
#: cli/compile/compile.go:212
727727
#: cli/compile/compile.go:244
728728
#: cli/upload/upload.go:96
729-
#: cli/upload/upload.go:101
730-
#: cli/upload/upload.go:111
731-
#: cli/upload/upload.go:134
729+
#: cli/upload/upload.go:102
730+
#: cli/upload/upload.go:118
731+
#: cli/upload/upload.go:145
732732
msgid "Error during Upload: %v"
733733
msgstr "Error during Upload: %v"
734734

@@ -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:124
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

@@ -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

@@ -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

Diff for: i18n/rice-box.go

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

Diff for: 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)