-
-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: enforce valid sketch folder name on Save as
#1821
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UPDATE: resolved
- Select File > New Sketch from the Arduino IDE menus.
- Click the ●●● icon on the right side of the editor toolbar.
- Select "New Tab" from the menu.
- Type ``~!@#$%^&()=+[{]};',.cpp` in the "Name for new file" dialog field.
- Click the "OK" button.
🐛 The IDE allowed the addition of a sketch code file with non-compliant name. - Select Tools > Board > Arduino AVR Boards > Arduino Uno from the Arduino IDE menus.
- Select Sketch > Verify/Compile from the Arduino IDE menus.
🐛 Compilation fails with a cryptic error:avr-g++: error: C:\Users\per\AppData\Local\Temp\arduino-sketch-58EC7305E05EBCCAD711E866CB3F08F6\sketch\`~!@#$%^&()=+[;',.cpp: No such file or directory avr-g++: warning: '-x c++' after last input file has no effect avr-g++: fatal error: no input files compilation terminated. exit status 1 Compilation error: exit status 1
No problem, I can add the validation when creating a new file. #1599 does not mention it. Where can I find the validation constraints for the other files? Besides the additional code files and the allowed extensions, IDE2 needs to know how to deal with non-code files, such as I have noticed that the spec, IDE2 code, and the CLI are not in sync regarding the allowed code file extensions:
Would you like for me to adjust them? |
I don't think any validation is needed for these files because they don't have any technical usage (with the exception of the special JSON files with fixed filenames, for which name validation is not applicable).
That would be wonderful. |
Thank you! What do you think about the code files? Should they follow the sketch folder name validation constraints?
Sure. |
Per, when this PR gets merged, IDE2 will filter all sketches in invalid sketch folders (File > Sketchbook). What should IDE2 do when there is a code file with an invalid name in the sketch? Thank you! |
Yes. This is already required by the specification: https://arduino.github.io/arduino-cli/dev/sketch-specification/#sketch-folders-and-files
Arduino IDE 1.x shows a notification only when the sketch is opened:
However, that is a lie. The file is not ignored as promised. I think the best approach is to check for and treat invalid sketch code filenames the same as you are already doing for invalid sketch folder names. Either way it is an invalid sketch so I don't think there is a benefit from handling the two in different ways from a UX standpoint. |
Thanks. So IDE2 should filter them.
IDE2 does not use the CLI to discover the sketch folders in the sketchbook. IDE2 uses a glob pattern. When IDE2 detects an invalid sketch folder, it warns the users and filters the sketch from IDE2 does not know which files are associated with a sketch. The CLI provides this info via the |
OK, then I guess the only option is to validate the sketch files when the sketch is loaded and show the invalid sketch dialog then. Thinking of it more, this approach actually makes the most sense because the user may open sketches that are outside the sketchbook. Maybe it would be best to also postpone the folder name validation until the sketch is opened? |
This approach is possible. IDE2 raises a warning dialog regarding the invalid sketch code files when the sketch is opened. IDE2 won't be able to filter those sketches from
It is unrelated to whether the sketch is in the sketchbook or not. Users can open any sketches, any file that ends
This is what IDE2 will do if this PR gets merged. IDE2 opens a sketch and warns about all invalid sketch folder names inside the sketchbook. This happens only when the sketch is opened in IDE2. What IDE2 cannot do easily is warn about all sketches which contain invalid code files. The latter would require loading all sketches when any sketch opens.
Actually, IDE2 can do it quickly, but it would be bad for the startup performance, which is not the greatest anyway. |
I meant that it would only validate the folder name of the specific sketch that was opened, just as it will only validate filenames in the specific sketch that was opened. |
export function validateCloudSketchFolderName( | ||
candidate: string | ||
): string | undefined { | ||
return /^[0-9a-zA-Z_]{1,36}$/.test(candidate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to get a confirmation that it really is necessary to disallow the -
and .
characters in Cloud sketches. That seems a completely unnecessary departure from the Arduino Sketch Specification to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started an internal dialog.
export function validateCloudSketchFolderName( | ||
candidate: string | ||
): string | undefined { | ||
return /^[0-9a-zA-Z_]{1,36}$/.test(candidate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return /^[0-9a-zA-Z_]{1,36}$/.test(candidate) | |
return /^[0-9a-zA-Z]{1}[0-9a-zA-Z_]{0,35}$/.test(candidate) |
I'm ready to accept that there is some hypothetical reason why the maximum length must be more restrictive for Cloud sketch. At least in this case a valid cloud sketch name length is still in compliance with the specification, even though the converse is not true.
But it is unacceptable to allow Cloud sketch names that are in violation of the Arduino Sketch Specification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it is unacceptable to allow Cloud sketch names that are in violation of the Arduino Sketch Specification.
It was requested in one of the design files that IDE2 must rename sketches when cloning from and pushing to the cloud. We need to add tests to verify IDE2 can do the name transformation.
Users can still create and pull a remote sketch, so relaxing the regex here might not help. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users can still create and pull a remote sketch, so relaxing the regex here might not help. What do you think?
I don't think it is a problem because the new approach is to validate names when the sketch is opened. So even if an invalid sketch is pulled, it will still be at the time the user opens the pulled sketch.
I'm referring to this feature:
- When you open an invalid sketch, IDE2 will prompt for user actions to fix the issues.
- First, IDE2 prompts for the sketch folder name fix as a
Save as...
operation. IDE2 prompt until the user specifies a valid folder name or aborts the operation.- If the sketch folder name has been corrected, IDE2 reloads and IDE2 validates the code files
- If there are any invalid code files, the user has to fix them as a
Rename
operation.- When all problems have been fixed, IDE2 is ready for use. If no, IDE2 prompts users whether they want to interrupt the operation and navigate to a new temp sketch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is a problem because the new approach is to validate names when the sketch is opened. So even if an invalid sketch is pulled, it will still be at the time the user opens the pulled sketch.
You're correct, but it only covers some user cases. After IDE2 pulls the cloud sketch and fixes any possible sketch folder name and code file name errors, the user can create a file remotely and pull it again. It will be spec incompatible if the name of the code file starts with an underscore. Please correct me if I am overlooking something. Thanks!
IDE2 may run a validation after pulling from the remote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the user can create a file remotely and pull it again. It will be spec incompatible if the name of the code file starts with an underscore. Please correct me if I am overlooking something.
You are right that this would provide a path to having a non-compliant sketch open in Arduino IDE. However, that will only be possible during that session. The next time the user tries to open the sketch, the added file will be validated.
So I wouldn't consider this scenario to completely invalidate the proposed approach of only doing the filename validation on an open operation.
Ideally validation would also be done when a file is added or renamed through an "external" process such as a pull. There is an equivalent scenario for local sketches in that the user may add or rename files of the open sketch externally. But I don't consider such validation to be absolutely essential. The most important reason for validating against the specification is to ensure shared sketches are compatible with any official Arduino development tool as well as 3rd party tools from conscientious developers. I think it would be rare that a developer would not end up later opening the type of sketch that is shared (as opposed to a quick throwaway test sketch we might write and use during a single IDE session and never look at again).
So if it is relatively easy to add a validation triggered by external addition or name change of sketch file, then great. But if this is something of significant difficulty to implement then I think the "on open" approach is sufficient.
Closes #1599 Co-authored-by: Akos Kitta <[email protected]> Co-authored-by: per1234 <[email protected]> Signed-off-by: Akos Kitta <[email protected]>
As discussed, I completely rewrote the implementation. Expectations:
Please let me know if you have better wording for the dialog titles and messages. Please review. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm experiencing unexpected results when opening Cloud sketches with invalid name:
- Download the sketch ZIP file from this link:
Invalid(Name).zip - Use Arduino Web Editor to import it into your Cloud sketchbook:
https://create.arduino.cc/editor - Sync Cloud sketchbook in Arduino IDE.
- Pull the "Invalid(Name)" sketch.
- Open the "Invalid(Name)" sketch.
🙂 I get a dialog informing me the sketch has an invalid name.
😕 But the dialog tells me the local sketch name restrictions (i.e., max length 63) instead of the Cloud sketch restrictions (i.e., max length 36):
- Click the "OK" button.
🙂 I get a "Save sketch folder as..." dialog
😕 But the dialog is for saving it as a local sketch. - Download the sketch ZIP file from this link:
InvalidAdditionalFileName.zip - Use Arduino Web Editor to import it into your Cloud sketchbook:
https://create.arduino.cc/editor - Sync Cloud sketchbook in Arduino IDE.
- Pull the "InvalidAdditionalFileName" sketch.
- Open the "InvalidAdditionalFileName" sketch.
🙂 I get a dialog informing me the sketch has a file with invalid name.
😕 But the dialog tells me the local sketch name restrictions (i.e., max length 63) instead of the Cloud sketch restrictions (i.e., max length 36):
- Click the "OK" button.
🙂 I get a rename file dialog
😕 But my impression is that renaming Cloud sketch files is not currently supported: Enable rename and delete options for Cloud Sketch and/or any of its files #1825 - Type
InvalidAdditionalFileName12345678901234567890123456789012345678890.h
in the sketch filename field.
😕 The error message tells me the local sketch name restrictions (i.e., max length 63) instead of the Cloud sketch restrictions (i.e., max length 36):
- Change the name to
InvalidAdditionalFileName12345678901234567890123456789012345678
😕 The dialog considers it a valid name, even though it is longer than the 36 character Cloud sketch maximum. - Click the "OK" button.
🙂 The sketch is still in my Cloud sketchbook. - Push the sketch.
🙂 The push was successful.
Thank you for the review. This PR depends on #1833. I will not update this PR, but I won't close it either until the other one is ready for review. |
#1833 should handle these. |
Closing as duplicate. Please review #1833 instead. The other PR must comply to all the requirements this PR meant to cover. Thank you! |
Motivation
IDE2 must disallow the creation of sketches with invalid folder names: https://arduino.github.io/arduino-cli/dev/sketch-specification/#sketch-root-folder
Change description
File
>Sketchbook
.Save As...
), IDE2 does not allow the save if the sketch folder name is invalid. IDE2 opens a dialog and warns the user. The user can decide whether to edit the new sketch folder name or cancel the operation.I had a hard time recording screencasts that fit the max upload size, so no teasers this time.
Other information
Closes #1599
Reviewer checklist