【问题标题】:debug.activeDebugSession API returns undefined on vscodedebug.activeDebugSession API 在 vscode 上返回 undefined
【发布时间】:2019-02-21 22:49:21
【问题描述】:

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

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" 扩展。
  • 用上面的代码替换了extension.js
  • F5 启动(和调试)扩展程序。
  • 在新窗口中打开了一个简单的node.js程序。
  • F5调试简单程序。
  • F1 并输入“Hello”并运行“Hello World”扩展程序。

但是const session = vscode.debug.activeDebugSession; 返回undefined

vscode.debug.activeDebugConsole.append("Hello from Debug Console"); 也没有显示任何内容。

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

我是否正确地有一个活动的调试会话?我在这里做错了什么?

【问题讨论】:

    标签: javascript visual-studio-code vscode-extensions vscode-debugger


    【解决方案1】:

    我已经按照以下步骤解决了这个问题:

    • 使用 yeoman 创建一个简单的 "Hello World" 扩展。
    • 用上面的代码替换extension.js
    • F5 启动(和调试)扩展程序。
    • 在打开的新窗口中,点击File Menu,然后点击Open Folder,选择一个简单的node.js程序的文件夹 strong> 或 vscode 工作场所这非常重要)。
    • 在某行设置断点(对于了解测试的行为也很重要)。
    • F5 调试简单程序(这是打开活动调试会话的方法)。
    • F1 并键入“Hello”并运行“Hello World”扩展程序。
    • 欣赏结果!!!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您的建议并没有解决我的问题,但它们将我的头对准了新的研究。感谢那 ! (点赞)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2020-04-12
      • 1970-01-01
      • 2020-08-07
      相关资源
      最近更新 更多