From 0daef8ade87051dd36be1162dd05ad80de13b28d Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 12 Mar 2023 18:59:43 -0700 Subject: [PATCH] Allow leading underscore in sketch filenames 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`. --- commands/sketch/new.go | 4 ++-- commands/sketch/new_test.go | 4 ++-- docs/sketch-specification.md | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/commands/sketch/new.go b/commands/sketch/new.go index 5efbee8ff48..ff2828b4e6f 100644 --- a/commands/sketch/new.go +++ b/commands/sketch/new.go @@ -37,7 +37,7 @@ void loop() { // sketchNameMaxLength could be part of the regex, but it's intentionally left out for clearer error reporting var sketchNameMaxLength = 63 -var sketchNameValidationRegex = regexp.MustCompile(`^[0-9a-zA-Z][0-9a-zA-Z_\.-]*$`) +var sketchNameValidationRegex = regexp.MustCompile(`^[0-9a-zA-Z_][0-9a-zA-Z_\.-]*$`) // NewSketch creates a new sketch via gRPC func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) { @@ -80,7 +80,7 @@ func validateSketchName(name string) error { sketchNameMaxLength))} } if !sketchNameValidationRegex.MatchString(name) { - 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 ".".`, + 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 ".".`, name))} } return nil diff --git a/commands/sketch/new_test.go b/commands/sketch/new_test.go index a0de66710ed..4ef67d50b2d 100644 --- a/commands/sketch/new_test.go +++ b/commands/sketch/new_test.go @@ -13,7 +13,6 @@ func Test_SketchNameWrongPattern(t *testing.T) { invalidNames := []string{ "&", ".hello", - "_hello", "-hello", "hello*", "||||||||||||||", @@ -25,7 +24,7 @@ func Test_SketchNameWrongPattern(t *testing.T) { SketchDir: t.TempDir(), }) - 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 ".".`, + 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 ".".`, name)) } } @@ -66,6 +65,7 @@ func Test_SketchNameOk(t *testing.T) { "h..ello-world", "h..ello-world.", "hello_world__", + "_hello_world", string(lengthLimitName), } for _, name := range validNames { diff --git a/docs/sketch-specification.md b/docs/sketch-specification.md index 105c220cfed..f7488c794ca 100644 --- a/docs/sketch-specification.md +++ b/docs/sketch-specification.md @@ -5,10 +5,13 @@ The programs that run on Arduino boards are called "sketches". This term was inh ## Sketch folders and files -The sketch root folder name and code file names must start with a basic letter (`A`-`Z` or `a`-`z`) or number (`0`-`9`), -followed by basic letters, numbers, underscores (`_`), dots (`.`) and dashes (`-`). The maximum length is 63 characters. +The sketch root folder name and code file names must start with a basic letter (`A`-`Z` or `a`-`z`), number (`0`-`9`) +[1](#leading-number-note), or underscore (`_`) [2](#leading-underscore-note) followed by basic +letters, numbers, underscores, dots (`.`) and dashes (`-`). The maximum length is 63 characters. -Support for names starting with a number was added in Arduino IDE 1.8.4. + 1 Supported from Arduino IDE 1.8.4.
+ 2 Supported in all versions except Arduino IDE 2.0.4/Arduino CLI +0.30.0 - 0.30.1. ### Sketch root folder