Skip to content

Commit a012824

Browse files
Check .ino sketch file exists in path before creating a new one
Running "arduino-cli sketch new ." from a sketch directory overwrites an existing .ino sketch file, without asking for confirmation. This fix introduces a check to verify if the specified path already contains a .ino sketch file. If it does, an error is returned, otherwise a new sketch is created as usual.
1 parent d86bc13 commit a012824

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

Diff for: commands/sketch/new.go

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package sketch
1717

1818
import (
1919
"context"
20+
"errors"
2021

2122
"github.com/arduino/arduino-cli/arduino"
2223
"github.com/arduino/arduino-cli/arduino/globals"
@@ -47,6 +48,9 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
4748
}
4849
sketchName := sketchDirPath.Base()
4950
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
51+
if sketchMainFilePath.Exist() {
52+
return nil, &arduino.CantCreateSketchError{Cause: errors.New(".ino file already exists")}
53+
}
5054
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
5155
return nil, &arduino.CantCreateSketchError{Cause: err}
5256
}

0 commit comments

Comments
 (0)