问题
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.
回答1:
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.
回答2:
you have not activated the extension:
"activationEvents": [
"onCommand:extension. useMyExtension"
],
回答3:
Since my comment helped someone, I would like to post it as an answer to give it more visibility:
I was able to fix this issue by manually compiling the Typescript source (by running tsc -p ./
into my root folder). This command should be ran automatically when debugging, however, I was still not able to find why it was not the case on my machine.
回答4:
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.
回答5:
My issue is I was running Cmder as my VS Code terminal. When I started debugging, the build would never complete - you can monitor Running Tasks from the bottom toolbar. When I looked at the task being run, I saw this:
Executing task: npm run watch <
VS Code is attempting to run the watch script but it never finished - I'm guessing some syntactical issue is at work here.
Anyway, while compiling does work, I'd recommend running the watch
script instead so that your extension will re-compile with every file change:
npm run watch
You should now be able to run your extension in your extension host via F5
without having to re-compile constantly. (You'll still need to reload your host though)
回答6:
I'm adding this answer for anyone who might experience this behavior for the same reason I did because it took me quite some time to realize what was happening.
I use PowerShell as my default terminal and had my profile configured to set the path of new shells (using Set-Location) to a specific folder for convenience on other development work I'm doing.
When pressing F5 to launch my extension, VS Code was attempting to run the compile command from this directory rather than my project directory, which failed of course.
This failure was obvious in the C:\Users\<your_user_name>\AppData\Roaming\npm-cache\_logs files once I realized to look there.
I removed the path setting from my PowerShell profile and VS Code extension building worked as expected.
回答7:
Your problem looks similar to this issue: https://github.com/Microsoft/vscode/issues/25026
来源:https://stackoverflow.com/questions/49534068/command-not-found-in-vscode-extension