Skip to content

Commit 79dd878

Browse files
committed
Fix path in error messages on sketch loading
1 parent 63b53c0 commit 79dd878

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

arduino/sketch/sketch.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func New(path *paths.Path) (*Sketch, error) {
6464
}
6565

6666
path = path.Canonical()
67-
if !path.IsDir() {
67+
if _, validIno := globals.MainFileValidExtensions[path.Ext()]; validIno && !path.IsDir() {
6868
path = path.Parent()
6969
}
7070

@@ -82,6 +82,9 @@ func New(path *paths.Path) (*Sketch, error) {
8282
}
8383
}
8484
}
85+
if mainFile == nil {
86+
return nil, fmt.Errorf(tr("main file missing from sketch: %s", path.Join(path.Base()+globals.MainFileValidExtension)))
87+
}
8588

8689
sketch := &Sketch{
8790
Name: path.Base(),
@@ -269,7 +272,6 @@ func (s *Sketch) checkSketchCasing() error {
269272
return &InvalidSketchFolderNameError{
270273
SketchFolder: s.FullPath,
271274
SketchFile: sketchFile,
272-
Sketch: s,
273275
}
274276
}
275277

@@ -280,7 +282,6 @@ func (s *Sketch) checkSketchCasing() error {
280282
type InvalidSketchFolderNameError struct {
281283
SketchFolder *paths.Path
282284
SketchFile *paths.Path
283-
Sketch *Sketch
284285
}
285286

286287
func (e *InvalidSketchFolderNameError) Error() string {

arduino/sketch/sketch_test.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,39 @@ func TestNewSketchWrongMain(t *testing.T) {
114114
}
115115

116116
func TestNewSketchCasingWrong(t *testing.T) {
117-
sketchPath := paths.New("testdata", "SketchWithWrongMain")
118-
sketch, err := New(sketchPath)
119-
assert.Nil(t, sketch)
120-
assert.Error(t, err)
121-
assert.IsType(t, &InvalidSketchFolderNameError{}, err)
122-
e := err.(*InvalidSketchFolderNameError)
123-
assert.NotNil(t, e.Sketch)
124-
sketchPath, _ = sketchPath.Abs()
125-
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchPath.String(), sketchPath.Join(sketchPath.Base()+".ino"))
126-
assert.EqualError(t, err, expectedError)
117+
{
118+
sketchPath := paths.New("testdata", "SketchWithWrongMain")
119+
sketch, err := New(sketchPath)
120+
assert.Nil(t, sketch)
121+
assert.Error(t, err)
122+
_, ok := err.(*InvalidSketchFolderNameError)
123+
assert.False(t, ok)
124+
sketchPath, _ = sketchPath.Abs()
125+
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
126+
assert.EqualError(t, err, expectedError)
127+
}
128+
{
129+
sketchPath := paths.New("testdata", "SketchWithWrongMain", "main.ino")
130+
sketch, err := New(sketchPath)
131+
assert.Nil(t, sketch)
132+
assert.Error(t, err)
133+
_, ok := err.(*InvalidSketchFolderNameError)
134+
assert.False(t, ok)
135+
sketchPath, _ = sketchPath.Parent().Abs()
136+
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
137+
assert.EqualError(t, err, expectedError)
138+
}
139+
{
140+
sketchPath := paths.New("testdata", "non-existent")
141+
sketch, skerr := New(sketchPath)
142+
require.Nil(t, sketch)
143+
require.Error(t, skerr)
144+
_, ok := skerr.(*InvalidSketchFolderNameError)
145+
assert.False(t, ok)
146+
sketchPath, _ = sketchPath.Abs()
147+
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
148+
require.EqualError(t, skerr, expectedError)
149+
}
127150
}
128151

129152
func TestNewSketchCasingCorrect(t *testing.T) {

legacy/builder/container_setup.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package builder
1717

1818
import (
19-
"fmt"
20-
2119
sk "github.com/arduino/arduino-cli/arduino/sketch"
2220
"github.com/arduino/arduino-cli/legacy/builder/types"
2321
"github.com/pkg/errors"
@@ -61,17 +59,9 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
6159

6260
// load sketch
6361
sketch, err := sk.New(sketchLocation)
64-
if e, ok := err.(*sk.InvalidSketchFolderNameError); ctx.IgnoreSketchFolderNameErrors && ok {
65-
// ignore error
66-
// This is only done by the arduino-builder since the Arduino Java IDE
67-
// supports sketches with invalid names
68-
sketch = e.Sketch
69-
} else if err != nil {
62+
if err != nil {
7063
return errors.WithStack(err)
7164
}
72-
if sketch.MainFile == nil {
73-
return fmt.Errorf(tr("main file missing from sketch"))
74-
}
7565
sketch.BuildPath = ctx.BuildPath
7666
ctx.SketchLocation = sketch.MainFile
7767
ctx.Sketch = sketch

0 commit comments

Comments
 (0)