Description
Bug Report
Currently, the Arduino tools (including arduino-cli) require that a sketch, which is a directory containing one or more .ino files and optionally additional files, includes a "primary" .ino file which shares the same name as the directory it is in.
This requirement has surprised users in the past, judging from various bug reports and questions over the years.
In the discussions of one such issues (arduino/Arduino#10755), the thought occurred that this requirement could maybe be lifted, in a mostly backward compatible way. A sketch would still need to be a directory (so this is not about compiling single .ino files), but it would no longer need to contain a .ino file named after the directory.
Currently, the primary .ino file is (AFAIK) only relevant when deciding the order in which .ino files are merged into a single .cpp file. This ordering is:
- The primary .ino file
- All other .ino files, sorted alphabetically
Now, if no primary .ino file is present, one could just skip point 1. This means that any sketch that is currently valid will still be valid and compiled in the same way, you just add more flexibility in defining a sketch.
There is one problem here: If you have a sketch with a primary file, then rename the sketch directory, then the primary file will likely become non-existent, changing the order of compilation and possibly breaking the build. In the current situation, such a rename will cause the tooling to reject the sketch with an error, rather than potentially breaking the build, which can be considered better. Note that a related problem already exists currently: If you rename the directory to the same name as a secondary .ino file, the sketch is still valid but might fail to build as well. This is of course a smaller chance.
One alternative approach would be to introduce a special filename, say "Main.ino" (or maybe "Sketch.ino" or "Primary.ino") and use either that file, or the .ino file named after the directory as the primary file. This leaves two further choices:
- If both Main.ino and a file named after the directory is present, which one to prefer. Using Main.ino is makes sketches that contain it completely robust against directory renames, but also has a small compatibility problem for existing sketches that contain a Main.ino.
- If neither are present, should the sketch still be valid? If not, then sketches not containing Main.ino are more robust against renames, if they are valid, you have more flexibility in naming your files.
Personally, I like the "Main.ino" approach, together with "Prefer Main.ino over directory-named .ino" and "Require either Main.ino or directory-named .ino". Using a Main.ino file could also become the default/recommended structure for sketches (maybe after some transition period), with a fallback for directory-named .ino files kept, of course.
This approach still poses limitations on sketch names, but I suspect that "You need a main.ino file" would be easier to grasp than "The main file must be named after the directory". It also is a lot more reliable when renaming sketch directories, and easier, since you can just rename the directory, without also having to rename the .ino file.