Javascript debug.activeDebugSession API在vscode上返回未定义

Javascript debug.activeDebugSession API在vscode上返回未定义,javascript,visual-studio-code,vscode-extensions,vscode-debugger,Javascript,Visual Studio Code,Vscode Extensions,Vscode Debugger,我在尝试使用vscode的debug api时遇到问题。这是我正在使用的代码: const vscode = require('vscode'); function activate(context) { console.log('Congratulations, your extension "helloworld" is now active!'); let disposable = vscode.commands.registerCommand('extension.

我在尝试使用
vscode
debug api
时遇到问题。这是我正在使用的代码:

const vscode = require('vscode');

function activate(context) {

    console.log('Congratulations, your extension "helloworld" is now active!');

    let disposable = vscode.commands.registerCommand('extension.helloWorld', function () {

        // Display a message box to the user
        vscode.window.showInformationMessage('Hello World!'); // This works

        const session = vscode.debug.activeDebugSession;
        console.log(session); // returns undefined

        // I have tried this one, and no text is shown
        vscode.debug.activeDebugConsole.append("Hello from Debug Console");

        if (session) {
            session.customRequest("evaluate", {
                "expression": "Math.sqrt(10)"
            }).then(reply => {
                vscode.window.showInformationMessage(`result: ${reply.result}`);
            }, error => {
                vscode.window.showInformationMessage(`error: ${error.message}`);
            });
        }
    });

    context.subscriptions.push(disposable);
}
exports.activate = activate;

function deactivate() {}

module.exports = {
    activate,
    deactivate
}
为了尝试这一点,我采取了以下步骤:

  • 使用yeoman创建了简单的
    “Hello World”
    扩展
  • 扩展名.js
    替换为上面的代码
  • 按F5启动(并调试)扩展
  • 在新窗口中打开了一个简单的node.js程序
  • F5调试简单程序
  • 按F1并键入“Hello”并运行“Hello World”扩展
但是
const session=vscode.debug.activeDebugSession返回未定义的

另外
vscode.debug.activeDebugConsole.append(“调试控制台的你好”)不显示任何内容

我知道如果没有活动的调试会话,上面的代码将无法正常工作。因此:


我是否正确地拥有活动的调试会话?我做错了什么?

我的建议:您必须订阅
vscode.debug.onDidStartDebugSession
vscode.debug.onDidTerminateDebugSession
,以跟踪会话何时开始/结束,或者在更改时使用
onDidChangeActiveDebugSession
。此时,您可以知道会话处于活动状态,您的代码应该运行。

我已经解决了这个问题,遵循以下步骤:

  • 使用yeoman创建一个简单的
    “Hello World”
    扩展名
  • 用上面的代码替换
    扩展名.js
  • 按F5启动(并调试)扩展
  • 在打开的新窗口中,单击文件菜单,然后单击打开文件夹,然后选择一个文件夹,其中有一个简单的node.js程序或一个vscode工作区(这非常重要)
  • 在某行中设置断点(对于了解测试的行为也很重要)
  • 按F5调试简单程序(这是如何打开活动调试会话的方法)
  • 按F1并键入“Hello”,然后运行“Hello World”扩展
  • 感谢结果
你的建议并没有解决我的问题,但它们把我的注意力放在了新的研究上。谢谢你!(投票赞成)