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.jsonis 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
- Through menu: File > Add Folder to Workspace
- Through command palette: Ctrl+Shift+P > "Add Folder to Workspace"
- 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
typescriptconst workspaceFolders = vscode.workspace.workspaceFolders; if (workspaceFolders) { workspaceFolders.forEach(folder => { console.log(folder.name, folder.uri.fsPath); }); }
Listen to Workspace Changes
typescriptvscode.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
.vscodeconfiguration - Extensions need to properly handle multi-root workspace scenarios
- Workspace settings priority: User settings > Remote settings > Workspace settings > Folder settings