typescript 在 VSCode 扩展中找不到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49534068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command not found in VSCode extension
提问by Eldy
I am trying to create a VSCode extension. This extension provides two commands, never mind their implementation:
我正在尝试创建一个 VSCode 扩展。这个扩展提供了两个命令,不用管它们的实现:
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:
这是我package.json
文件的一部分:
"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
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.
请注意,我显然更改了一些变量名称,我在实际代码中仔细检查了拼写错误。
采纳答案by Eldy
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.
我能够通过手动编译 Typescript 源(通过运行tsc -p ./
到我的根文件夹)来解决这个问题。这个命令应该在调试时自动运行,但是,我仍然无法找到为什么在我的机器上不是这种情况。
回答by TheLandoCal
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
package.json 中的列表中,以便它们在调用时可用。像这样更新你的 package.json:
{
...
"activationEvents": [
"onCommand:extension.openMyExtension",
"onCommand:extension.useMyExtension"
]
...
}
You can find more details on activation events in the official VSCode Documentation.
您可以在官方VSCode 文档 中找到有关激活事件的更多详细信息。
回答by Bart Theeten
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.
我遇到过同样的问题。一切都配置正确,但结果我的 package.json 缺少一个依赖项。这导致扩展加载失败,因此命令在运行时没有正确注册。
To find out what is wrong with your code, open up the Help > Toggle Developer Tools and look for errors in the console.
要找出您的代码有什么问题,请打开“帮助”>“切换开发人员工具”并在控制台中查找错误。
回答by lwpro2
you have not activated the extension:
您尚未激活扩展程序:
"activationEvents": [
"onCommand:extension. useMyExtension"
],
回答by Mister Epic
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:
我的问题是我运行 Cmder 作为我的 VS Code 终端。当我开始调试时,构建永远不会完成 - 您可以从底部工具栏监控正在运行的任务。当我查看正在运行的任务时,我看到了:
Executing task: npm run watch <
执行任务: 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.
VS Code 正在尝试运行 watch 脚本,但它从未完成 - 我猜这里有一些语法问题。
Anyway, while compiling does work, I'd recommend running the watch
script instead so that your extension will re-compile with every file change:
无论如何,虽然编译确实有效,但我建议watch
改为运行脚本,以便您的扩展程序会在每次文件更改时重新编译:
npm run watch
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)
您现在应该能够在您的扩展主机中运行您的扩展,F5
而无需不断重新编译。(不过,您仍然需要重新加载主机)
回答by intheweeds
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.
我使用 PowerShell 作为我的默认终端,并将我的配置文件配置为将新 shell 的路径(使用 Set-Location)设置为特定文件夹,以方便我正在进行的其他开发工作。
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.
当按 F5 启动我的扩展时,VS Code 试图从这个目录而不是我的项目目录运行编译命令,这当然失败了。
This failure was obvious in the C:\Users\<your_user_name>\AppData\Roaming\npm-cache\_logs files once I realized to look there.
一旦我意识到在那里查看,此失败在 C:\Users\<your_user_name>\AppData\Roaming\npm-cache\_logs 文件中很明显。
I removed the path setting from my PowerShell profile and VS Code extension building worked as expected.
我从我的 PowerShell 配置文件中删除了路径设置,并且 VS Code 扩展构建按预期工作。
回答by crossminder
Your problem looks similar to this issue: https://github.com/Microsoft/vscode/issues/25026
您的问题与此问题类似:https: //github.com/Microsoft/vscode/issues/25026