Incorrect default sketch opened when starting IDE

IDE 2.1.0
Windows 10

I have a blueprint sketch defined for the IDE File/New as follows

However, the incorrect sketch is opened when starting the IDE under some circumstances

  • close all instances of the IDE
  • open the IDE
  • ignore whatever sketch is opened but leave the IDE open
  • File/New will open the boilerplate sketch with the normal date based name
  • close the IDE instance that was originally opened
  • make no changes to the newly opened sketch and close the IDE
  • all instances of the IDE are now closed
  • open the IDE and a seemingly random sketch will be opened

I have noticed similar behaviour where sometimes an old sketch that I did not work on for ages is opened when the IDE is started; I however never linked it to the file→ new operation.

I'm using the standard "new sketch", not a "modified" version.

It took me ages to spot what was going on and it was only after I had written a long, complicated explanation of what was happening that I realised that I could reproduce the problem in just a few steps

1 Like

@pert Has this already been reported ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

This is still happening in 2.2.1 and is very annoying

I would be interested to know whether others have experienced the same behaviour

@ptillisch Does this problem ring any bells with you ?

Hi @UKHeliBob. If I understand your post correctly, you are expecting Arduino IDE to open a new sketch on startup.

However, the intended behavior is for Arduino IDE to open the last saved sketches that were open at the time you exited the IDE previously. It will only fall back to starting with a new sketch open if the saved sketches that were open during a previous session are no longer accessible (e.g., you deleted or moved them), or if you never had any saved sketches open during a previous session of Arduino IDE (as is the case on the first startup after a fresh installation, or if you cleared the IDE's stored data to simulate those conditions).

Something to note is that sketches opened via the File > Examples menu are opened as new sketches so these will not be restored on subsequent IDE startups unless you saved the sketch.

So my guess is that what you are interpreting as "random" is actually the deterministic behavior of opening whatever saved sketch you had open at exit of a previous IDE session.

I'll provide a demo you can follow in case the behavior isn't clear from my description above.

  1. Start Arduino IDE.
  2. Select File > New Sketch from the Arduino IDE menus.
  3. Select File > Save As.. from the Arduino IDE menus.
  4. Save the sketch with the name "SavedSketch1" to any convenient location.
  5. Close any other IDE windows other than the one that contains the "SavedSketch1" sketch.
  6. Wait a couple of seconds.
    This is required to ensure consistent results because Arduino IDE remembers the sketches that were open during a short period before exit.
  7. Select File > Quit from the Arduino IDE menus.
    This will cause the "SavedSketch1" sketch to be recorded in the Arduino IDE data as the saved sketch that was open during the previous session.
  8. Start Arduino IDE.
    Note that the "SavedSketch1" sketch is opened.
  9. Select File > New Sketch from the Arduino IDE menus.
  10. Close the window that contains the "SavedSketch1" sketch.
  11. Wait a couple of seconds.
  12. Select File > Quit from the Arduino IDE menus.
  13. Start Arduino IDE.
    Note that the "SavedSketch1" sketch is opened. The reason this sketch is still restored even though it wasn't open during the time you exited the last IDE session is because the sketch you did have open at that time was only a temporary staged new sketch.
  14. Select File > New Sketch from the Arduino IDE menus.
  15. Select File > Save As.. from the Arduino IDE menus.
  16. Save the sketch with the name "SavedSketch2" to any convenient location.
    You will now have two Arduino IDE windows open:
    • "SavedSketch1"
    • "SavedSketch2"
  17. Select File > Quit from the Arduino IDE menus.
  18. Start Arduino IDE.
    Note that the "SavedSketch1" and "SavedSketch2" sketches are opened.
  19. Close the window that contains the "SavedSketch2" sketch.
  20. Wait a couple of seconds.
  21. Select File > Quit from the Arduino IDE menus.
  22. Start Arduino IDE.
    Note that the "SavedSketch1" sketch is opened.
  23. Select File > Quit from the Arduino IDE menus.
  24. Delete the folder of the "SavedSketch1" sketch.
  25. Start Arduino IDE.
    Note that a new sketch is opened, using the contents of the file from the "Arduino › Sketch: Ino Blueprint" path if you have configured on via the Arduino IDE advanced settings

With this information, do you now find Arduino IDE is behaving as designed, or are you still experiencing some behavior that doesn't match with what I described above?

I carried out the steps that you described and the IDE behaved as you described

However, I am convinced that under some circumstances the wrong sketch is opened when starting the IDE

As an example, after following your 25 steps and observing the results that you describe, what do you expect to happen if you close the the IDE currently holding the sketch created in step 25, which is automatically given the date as its name, and open the IDE again ?

I just did it and my blueprint sketch was opened with its original name rather than the date based name. Presumably that is not expected behaviour

I expect it to open "SavedSketch2", since that is the last saved sketch that was open in Arduino IDE (step 18 of the demo).


Here is how the data is recorded:

  1. When you open a saved sketch in Arduino IDE, its path is prepended to the recentRoots[] array in C:\Users\<username>\.arduinoIDE\recentworkspace.json.
  2. When you exit Arduino IDE, the paths of all open saved sketches are saved to the workspaces[*].file fields in C:\Users\<username>\AppData\Roaming\arduino-ide\config.json.

Here is how that data is used on IDE startup:

Even though it is very deterministic, there is some inconsistency:

  • Sketches restored when the config.json data is used are the sketches that were open at the time of IDE exit, but the sketch restored when the recentworkspace.json data is used is not necessarily one that was open at exit (because the recentworkspace.json data is recorded when the sketch is opened).
  • The fallback behavior when the sketches to be restored can't be loaded is always to open a new sketch, even though there might be a sketch available in the recentworkspace.json data to restore. This is why a new sketch opens at step (25) of my demo, but then if you close the IDE and start it again a saved sketch will be restored.

Even so, I don't see that the inconsistency significantly harms the user experience. The inconsistencies I listed above occur under fairly uncommon conditions.

Thanks for the details

I will look at what is stored in the .json files under certain circumstances as I am still experiencing what I think are anomalies in respect of the sketch opened when starting the IDE

More details when/if I have them

I left the two files open in a text editor (one that automatically reflects external changes to the file) while I performed various experiments in Arduino IDE. Being able to watch the data changing in real time, and modifying it manually as needed was very helpful for me to understand the system (I didn't know about the system with these two files before).