Activation Events
Activation events define when your extension should be loaded and activated. VS Code follows a lazy loading model where extensions are only activated when specific conditions are met, ensuring optimal performance.Overview
Activation events are declared in your extension’spackage.json file under the activationEvents field. When VS Code detects that one of these events has occurred, it loads and calls your extension’s activate() function.
Core Activation Events
onLanguage
Activates when a file of a specific language is opened.Real-world example from TypeScript extension
Real-world example from TypeScript extension
Language IDs are case-sensitive and must match the language identifiers defined in VS Code or other extensions.
onCommand
Activates when a specific command is invoked via the Command Palette, keybinding, or programmatically.Real-world example from TypeScript extension
Real-world example from TypeScript extension
onFileSystem
Activates when a file or folder from a specific scheme is accessed.Real-world example from Git extension
Real-world example from Git extension
git or git-show file system schemes, allowing it to provide virtual file content for git operations.onView
Activates when a specific view is expanded in the VS Code UI.onTaskType
Activates when tasks of a specific type are about to run.Real-world example from TypeScript extension
Real-world example from TypeScript extension
onDebug
Activates when a debug session is about to start.onEditSession
Activates when an edit session for a specific scheme is loaded.Real-world example from Git extension
Real-world example from Git extension
onWalkthrough
Activates when a walkthrough is opened.Real-world example from TypeScript extension
Real-world example from TypeScript extension
onUri
Activates when a URI with your extension’s scheme is opened.onWebviewPanel
Activates when a webview of a specific type needs to be restored.onCustomEditor
Activates when a custom editor for a specific view type is opened.onAuthenticationRequest
Activates when authentication for a specific provider is requested.onRenderer
Activates when a notebook renderer is needed.onStartupFinished
Activates after VS Code has fully started, but before the user interacts with the editor.This is better than using
* as it doesn’t slow down VS Code startup. Use this when you need to activate early but don’t need to be available immediately.* (Star - Activate on Startup)
Activates immediately when VS Code starts.Real-world example from Git extension
Real-world example from Git extension
* because git repository detection needs to happen immediately on startup.Best Practices
Activation Strategy
- Be specific: Use precise activation events rather than broad ones
- Combine events: Use multiple specific events rather than one general event
- Avoid
*: UseonStartupFinishedif you need early activation - Test activation: Verify your extension only activates when needed
Good Example
Poor Example
Debugging Activation
You can check when your extension activates by adding logging to youractivate() function:
Multiple Activation Events
Extensions typically need multiple activation events. Your extension will activate when any of the listed events occur:- A TypeScript file is opened OR
- A JavaScript file is opened OR
- The refactor command is invoked OR
- A TypeScript task runs
Migration from workspaceContains
The
workspaceContains activation event has been deprecated. Use onStartupFinished combined with workspace file detection in your activate() function instead.