I am trying to create a VSCode extension. This extension provides two commands, never mind their implementation:
export function activate(context: ExtensionContext) {
const provider = new ContentProvider();
const providerRegistrations = Disposable.from(
workspace.registerTextDocumentContentProvider(ContentProvider.scheme, provider)
);
// Open the dynamic document, and shows it in the next editor
const openMyExtensionCommandRegistration = commands.registerTextEditorCommand('extension.openMyExtension', editor => {
// Activate the extension and do something
});
const useMyExtensionCommandRegistration = commands.registerTextEditorCommand('extension.useMyExtension', editor => {
// Do something
});
context.subscriptions.push(
provider,
openMyExtensionCommandRegistration,
useMyExtensionCommandRegistration,
providerRegistrations
);
}
And this is a part of my package.json
file:
"activationEvents": [
"onCommand:extension.openMyExtension"
],
"main": "./out/extension",
"contributes": {
"commands": [
{
"command": "extension.openMyExtension",
"title": "Open my extension",
"category": "MyExtension"
},
{
"command": "extension.useMyExtension",
"title": "Do something with my extension",
"category": "MyExtension"
}
],
The first command, which is supposed to activates my extension, works. It appears in the command palette, and actually does what it is supposed to do when invoked.
The second command however, despite appearing in the command palette, raise the following error message when called:
command 'extension.useMyExtension' not found
I find it weird that my first command works fine but not the second since the code is quite similar. Any ideas why?
Note that I obviously changed some variable names, I double checked for typos in the real code.
You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.
The solution was to go to Windows PowerShell and open Visual Studio Code, then go to the extensions and restart the Remote - WSL extension, and go back to WSL. It then started working immediately.
Try uninstalling and reinstalling VS Code. If code is still not found, consult the platform-specific setup topics for Windows and Linux. On macOS, you need to manually run the Shell Command: Install 'code' command in PATH command (available through the Command Palette Ctrl+Shift+P).
You need to add all registered commands to the activationEvents
list in package.json so that they are available on invocation. Update your package.json as such:
{
...
"activationEvents": [
"onCommand:extension.openMyExtension",
"onCommand:extension.useMyExtension"
]
...
}
You can find more details on activation events in the official VSCode Documentation.
I had the same issue. Everything was configured correctly but it turned out my package.json was missing one dependency. That caused the extension to fail loading and consequently the command not having been registered correctly at runtime.
To find out what is wrong with your code, open up the Help > Toggle Developer Tools and look for errors in the console.
you have not activated the extension in activationEvents
:
"activationEvents": [
"onCommand:extension.useMyExtension"
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With