Alert This post is over a year old, some of this information may be out of date.

#DevHack: VS Code extension storage options

For Front Matter and another VS Code extension which is currently in development, I wanted to understand which options there are for storing data. Data can be anything, for some extensions, it will be settings, for others, it is more complicated sets of data.

In this article, I will give you an overview of all the available storage options and when to use them.

Configuration settings

Configuration settings are not a way to store data for your extension, it only provides a way to how you want the user to use your extension. You can still use this option when you for instance want to manipulate the behavior of your extension from within your user interface.

Each extension can define its own configuration settings, and the user will be able to change it via the settings editor or straight within the JSON file.

Settings can be set and changed on various levels:

  • Default
  • Global
  • Workspace
  • Workspace folder level
  • Language-specific settings
Important

Only use settings for what they are intended for.

State storage

All extensions got access to the memento storage which can keep a key/value state for your extension. It works on two levels: global or workspace.

The state is kept whenever you update Visual Studio Code or update the extension.

You can update the global or workspace state via:

1
2
await context.globalstate.update(`key`, value)
await context.workspaceState.update(`key`, value)

Retrieving state values is achieved similarly:

1
2
await context.globalstate.get(`key`)
await context.workspaceState.get(`key`)
Info

Behind the scenes, VS Code uses an SQLite database to store these key/value pairs.

Secret storage

In some cases, you might want to keep secrets, for instance, connection strings. For some time, you had to use other dependencies, but the VS Code team provided a SecretStorage which is intended to be used to store secrets.

The secret storage is accessible via the VS Code extension context:

1
2
3
4
5
// Retrieving a secret
await context.secrets.get(`secretkey`)

// Storing a secret
await context.secrets.store(`secretkey`, `This is the secret`);

Files

If the previous options are not enough, you could still read/write files to the current workspace/solution/project.

Files still give you full flexibility as you won’t be limited to what the VS Code APIs impose.

In order to work with files (read/write), you can use the workspace.fs API. The workspace file system API exposes the editors’ built-in file system provider.

Info

Use it whenever you work with complex data like for instance search indexes, databases, or complex JSON data. Be aware, if you store files to the workspace/solution, the users might be able to edit these. If this is not something, you want it might be best to use one of the above options.

Comments

Back to top