Skip to content

Commit 2c5bd04

Browse files
per1234Akos Kitta
authored and
Akos Kitta
committed
Allow leading underscore in sketch filenames (arduino#2105)
The Arduino Sketch Specification defines the allowed format of sketch folder names and sketch code filenames. The origin of the specification is the text of the error message shown in Arduino IDE when the user attempts to save to a name that contains disallowed characters: https://github.com/arduino/Arduino/blob/89539b1131f8cde9f7a83225f21c811071af53a8/app/src/processing/app/SketchController.java#L847-L853 However, the implementation of the restriction in the IDE codebase has a bug that allows a leading underscore (because the code uses _ as the replacement character). After the restriction was implemented correctly in Arduino IDE 2.x, it was discovered that the loss of compatibility with these non-compliant names was impactful. One of the primary purposes for the specification and restrictions is to ensure sketches can be used in any tool. Since the tools support this name format and there is no technical reason to disallow it, the best thing to do is change the sketch specification to follow the historic tool behavior (even if that behavior was the result of a benign bug). Leading underscores in sketch folder names and sketch code filenames are hereby permitted by the Arduino Sketch Specification and `arduino-cli sketch new`.
1 parent fae7b69 commit 2c5bd04

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

Diff for: commands/sketch/new.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void loop() {
3737

3838
// sketchNameMaxLength could be part of the regex, but it's intentionally left out for clearer error reporting
3939
var sketchNameMaxLength = 63
40-
var sketchNameValidationRegex = regexp.MustCompile(`^[0-9a-zA-Z][0-9a-zA-Z_\.-]*$`)
40+
var sketchNameValidationRegex = regexp.MustCompile(`^[0-9a-zA-Z_][0-9a-zA-Z_\.-]*$`)
4141

4242
// NewSketch creates a new sketch via gRPC
4343
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
@@ -80,7 +80,7 @@ func validateSketchName(name string) error {
8080
sketchNameMaxLength))}
8181
}
8282
if !sketchNameValidationRegex.MatchString(name) {
83-
return &arduino.CantCreateSketchError{Cause: errors.New(tr(`invalid sketch name "%[1]s": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".`,
83+
return &arduino.CantCreateSketchError{Cause: errors.New(tr(`invalid sketch name "%[1]s": the first character must be alphanumeric or "_", the following ones can also contain "-" and ".".`,
8484
name))}
8585
}
8686
return nil

Diff for: commands/sketch/new_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func Test_SketchNameWrongPattern(t *testing.T) {
1313
invalidNames := []string{
1414
"&",
1515
".hello",
16-
"_hello",
1716
"-hello",
1817
"hello*",
1918
"||||||||||||||",
@@ -25,7 +24,7 @@ func Test_SketchNameWrongPattern(t *testing.T) {
2524
SketchDir: t.TempDir(),
2625
})
2726

28-
require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: invalid sketch name "%s": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".`,
27+
require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: invalid sketch name "%s": the first character must be alphanumeric or "_", the following ones can also contain "-" and ".".`,
2928
name))
3029
}
3130
}
@@ -66,6 +65,7 @@ func Test_SketchNameOk(t *testing.T) {
6665
"h..ello-world",
6766
"h..ello-world.",
6867
"hello_world__",
68+
"_hello_world",
6969
string(lengthLimitName),
7070
}
7171
for _, name := range validNames {

Diff for: docs/sketch-specification.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ The programs that run on Arduino boards are called "sketches". This term was inh
55

66
## Sketch folders and files
77

8-
The sketch root folder name and code file names must start with a basic letter (`A`-`Z` or `a`-`z`) or number (`0`-`9`),
9-
followed by basic letters, numbers, underscores (`_`), dots (`.`) and dashes (`-`). The maximum length is 63 characters.
8+
The sketch root folder name and code file names must start with a basic letter (`A`-`Z` or `a`-`z`), number (`0`-`9`)
9+
[<sup>1</sup>](#leading-number-note), or underscore (`_`) [<sup>2</sup>](#leading-underscore-note) followed by basic
10+
letters, numbers, underscores, dots (`.`) and dashes (`-`). The maximum length is 63 characters.
1011

11-
Support for names starting with a number was added in Arduino IDE 1.8.4.
12+
<a id="leading-number-note"></a> <sup>1</sup> Supported from Arduino IDE 1.8.4. <br />
13+
<a id="leading-underscore-note"></a> <sup>2</sup> Supported in all versions except Arduino IDE 2.0.4/Arduino CLI
14+
0.30.0 - 0.30.1.
1215

1316
### Sketch root folder
1417

0 commit comments

Comments
 (0)