乐闻世界logo
搜索文章和话题

What is the difference between VS Code workspace and multi-root workspaces?

2月18日 18:24

VS Code Workspace and Multi-root Workspaces

VS Code workspace refers to the currently opened folder or project. VS Code supports multi-root workspace feature, allowing multiple folders to be opened simultaneously and managed within a single workspace.

Single-root Workspace

Single-root workspace is the most common way of working, opening one folder as the workspace root directory.

Features

  • All files are under the same root directory
  • Configuration file .vscode/settings.json is stored in the workspace root
  • Suitable for single project development

Multi-root Workspace

Multi-root workspace allows opening multiple unrelated folders simultaneously.

Creating Multi-root Workspace

  1. Through menu: File > Add Folder to Workspace
  2. Through command palette: Ctrl+Shift+P > "Add Folder to Workspace"
  3. Save workspace: File > Save Workspace As...

Configuration File

Multi-root workspace configuration is stored in .code-workspace file:

json
{ "folders": [ { "path": "/path/to/project1" }, { "path": "/path/to/project2" } ], "settings": { "editor.fontSize": 14 } }

Workspace API

Get Workspace Folders

typescript
const workspaceFolders = vscode.workspace.workspaceFolders; if (workspaceFolders) { workspaceFolders.forEach(folder => { console.log(folder.name, folder.uri.fsPath); }); }

Listen to Workspace Changes

typescript
vscode.workspace.onDidChangeWorkspaceFolders(event => { console.log('Workspace folders changed'); });

Use Cases

  • Microservices Architecture: Manage multiple service projects simultaneously
  • Frontend-Backend Separation: Open both frontend and backend projects at the same time
  • Multi-project Development: Quickly switch between different projects
  • Library Development: Develop library and example projects simultaneously

Important Notes

  • Multi-root workspace configuration is global and doesn't affect individual project configurations
  • Each folder can have its own .vscode configuration
  • Extensions need to properly handle multi-root workspace scenarios
  • Workspace settings priority: User settings > Remote settings > Workspace settings > Folder settings
标签:VSCode