Skip to main content

Workspace API

The Workspace API provides access to workspace folders, files, configuration, and file system operations. It’s the primary way to interact with the user’s project files and settings.

Namespace

All workspace-related functionality is available through the workspace namespace.

Workspace Folders

Accessing Workspace Folders

readonly WorkspaceFolder[] | undefined
Array of workspace folders or undefined if no workspace is open

WorkspaceFolder Interface

Workspace Folder Events

Working with Files

Opening Documents

Finding Files

GlobPattern
required
Glob pattern defining files to search for
GlobPattern | null
Glob pattern for exclusion (null = no excludes)
number
Maximum number of results to return
CancellationToken
Cancellation token for the search operation

Relative Patterns

File System Operations

Reading Files

Writing Files

File Operations

File System Watchers

Creating Watchers

File watchers work across all workspace folders by default. Use RelativePattern to watch a specific folder.

Workspace-scoped Watcher

Configuration

Reading Configuration

string
Configuration section (e.g., ‘editor’ or ‘myExtension.subSection’)
ConfigurationScope
Scope for which to get configuration (resource, folder, language)

Updating Configuration

Configuration Inspection

Configuration Events

Text Documents

Accessing Documents

Document Events

Workspace Edit

Creating Edits

File Operations in Edits

Edit Metadata

File Operations Events

Before Operations

After Operations

These events are triggered by user gestures and workspace.applyEdit, but NOT by direct file system operations.

Text Encoding

Encoding and Decoding

Workspace Trust

Common Patterns

Get Workspace Folder for File

Save All Files

Open Folder in Workspace

Best Practices

  • Use glob patterns to limit file searches
  • Cancel long-running operations with CancellationToken
  • Dispose file watchers when no longer needed
  • Batch file operations in WorkspaceEdit
  • Always check if workspace folders exist before accessing
  • Handle cases where files may not exist
  • Use try-catch for file system operations
  • Validate URIs before file operations
  • Show progress for long file operations
  • Use workspace edit for atomic changes
  • Respect user’s file encoding settings
  • Request confirmation for destructive operations