Kevin Yin
Kevin Yin

Reputation: 872

How does Visual Studio Code recover data after a crash?

I'm interested in the backup mechanics provided by VS Code. "Crash" includes both sudden power outages and handled exceptions. As examples of what I mean,

  1. Notepad++ has a backup folder which periodically saves copies of your files. Nothing is lost beyond the last 7 seconds even if the power goes out.
  2. Atom keeps an IndexedDB which should recover everything after an exception. I have no idea what happens on power outage.
  3. Inkscape relies on its exception handling to recover data. If the power goes out, the data is lost.
  4. Scite has no backup plan.

Upvotes: 18

Views: 37414

Answers (3)

Ivan Buryanov
Ivan Buryanov

Reputation: 11

I found versions of recent edited files in C:\Users<username>\AppData\Roaming\Code\User\History

Upvotes: 1

Kay V
Kay V

Reputation: 4046

Brief answer to the question "How does Visual Studio Code recover data after a crash?":

  • VSCode runs a "Hot exit". In my experience it's pretty reliable. When it runs, it creates a copy of all unsaved files that were open in the tabs of a VSCode window (they call the window an Editor).

  • VSCode saves these copies in a Backups directory. It also generates a list of paths to the Editors that contained unsaved files.

  • You can reopen a previously open Editor in VSCode and see your backed up files.

Steps for meeting typical needs

To list the Editors that were open when Hot exit ran

  • using Terminal on MacOS

    cat $HOME/Library/Application\ Support/Code/Backups/workspaces.json`  
    
    • alternative using jq for a more readable list

      cat ~/Library/Application\ Support/Code/Backups/workspaces.json | jq '.'
      
  • using Terminal on Linux

    cat $HOME/.config/Code/Backups/workspaces.json
    
  • using Powershell on Windows

    gc %APPDATA%\Code\Backups\workspaces.json
    

To browse the Backups directory

code $HOME/Library/Application\ Support/Code/Backups/

To Reopen the Editor you want, with the previously open files in the same tab order

code {{ folderUri from workspaces.json }}

Why these steps are useful

I often forget what directories I had opened before VSCode quits/crashes, and the menu to "open recent" from within VSCode doesn't indicate which Editors have unsaved files. For that need the workspaces.json list that VSCode generates seems irreplaceable.

VSCode docs:

Hot Exit
VS Code will remember unsaved changes to files when you exit by default. Hot exit is triggered when the application is closed via File > Exit (Code > Quit on macOS) or when the last window is closed.

Upvotes: 4

Kevin Yin
Kevin Yin

Reputation: 872

According to https://code.visualstudio.com/blogs/2016/11/30/hot-exit-in-insiders,

The way hot exit works is to periodically make backups of unsaved files. If VS Code happens to crash, a backup restore will occur the next time the folder is opened.

On Windows, the backup folder is C:\Users\<username>\AppData\Roaming\Code\Backups.

As for the period between backups, I haven't found anything.

Upvotes: 13

Related Questions