Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command not found in VSCode extension

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.

like image 234
Eldy Avatar asked Mar 28 '18 12:03

Eldy


People also ask

How do I run an extension command in VS 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.

Why my extensions are not working in VS Code?

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.

Why is VS Code not working?

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).


3 Answers

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.

like image 138
TheLandoCal Avatar answered Oct 25 '22 15:10

TheLandoCal


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.

like image 38
Bart Theeten Avatar answered Oct 25 '22 16:10

Bart Theeten


you have not activated the extension in activationEvents:

"activationEvents": [
    "onCommand:extension.useMyExtension"
],
like image 26
engineer Avatar answered Oct 25 '22 15:10

engineer