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 doesn't provide any 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.
The 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 can’t 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 Visual Studio Code 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 Visual Studio Code?
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 two files named state.vscdb and state.vscdb.backup.
There usually is a third 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 separator characters of unknown function. But you also find full paths or names in there that clearly origin from different modules of Visual Studio Code, including the extensions.
I don't need to worry about the other cached stuff; I'm just going to 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 Visual Studio 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:
Locally, the data will be in the .vscode folder of the current workspace.
In my situation:
This leads to a clean Visual Studio Code state.
To see how workspace.id is generated, check this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With