VSCode how to clear workspaceState data globally?

Viewed 921

I have implemented a backup per workspace functionality in my extension using workspaceState. Since the data can be sensitive - I'd like to clear all workspaceStates on extension deactivation/uninstall.

The ExtensionContext provides no ability to clear all extension related data across different workspaces with their workspaceStates.

So I've considered saving data on the ExtensionContext globalState, tagging each entry with a workspace id. Problem is that the workspace namespace doesn't provide a way to uniquely identify the current workspace. I thought about hashing workspace name and path but both of these things are changeable and any change will destroy the pointer to the data. This is exactly why I cant just write files to internal folders. The only other solution I have is to write the backup data directly to the workspace and I'd like to avoid that.

How does VSCode maintain the knowledge of which workspaceState belongs to which workspace? How can I tie data to the workspace but have access from anywhere else in VSCode?

2 Answers

Side note: You should avoid saving sensitive data in general. And if necessary, try to encrypt it.

Anyway:

I don't have the full answers but i was researching something similar (An extension I use crashes due to now invalid settings in the WorkspaceState).

I found the Storage for the Workspace state in this folder (windows):

%appdata%\Code\User\workspaceStorage\

In there, you find a lot of folders with hex-based names. Inside those folders, I always found 2 Files named state.vscdb and state.vscdb.backup.

There usually is a 3rd file called workspace.json which helps you figure out if you are in the correct workspace. (but you'd have to iterate through all the folders - maybe there is a way to figure out the folder name coming from the extension API?)

If you open the state.vscdb-file you find something that looks quite like a serialized object set in my eyes. It does have some Seperator chars of unknown function. But you also find full paths or names in there that clearly origin from different modules of VSC - Including the extensions.

I don't need to worry about the other cached stuff i'm just gonna delete the whole folder to fix my current issue. But I'm pretty sure, one can figure out the way the file is built and edit out your sensitive data if one has to.

The state.vscdb.backup-file looks pretty much like what the name is telling you: they probably just make a copy of the other file every few minutes so you have a fallback position.

To add to the conversation, there are two SQLite state databases:

<user-data-dir>\User\globalStorage\state.vscdb

<user-data-dir>\User\workspaceStorage\<workspace.id>\state.vscdb

Depending on how VS Code was launched you could have a Single Folder Workspace or a Multi-Folder Workspace that is global or local. Globally, the data lives here:

  • Linux: $HOME/.config/Code/
  • OS X: $HOME/Library/Application Support/Code/
  • Windows: %APPDATA%\Code\

Locally, the data will be in the .vscode folder of the current workspace.

In my situation:

  1. I open a new workspace.
  2. Set it up as I want it to start every time.
  3. Makes copies of the two SQLite databases.
  4. Copy over the databases before launching VS Code.

This leads to a clean VS Code state.

To see how workspace.id is generated check this link.

Related