robe007
robe007

Reputation: 3927

debug.activeDebugSession API returns undefined on vscode

I have an issue when trying to play with the debug api of vscode. This is the code I'm using:

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
}

To try this I have made the following steps:

But const session = vscode.debug.activeDebugSession; returns undefined.

Also vscode.debug.activeDebugConsole.append("Hello from Debug Console"); does not shows anything.

I know that if there is no active debug session, the code from above will not work properly. So:

Do I correctly have an active debug session? What I'm doing wrong here?

Upvotes: 2

Views: 793

Answers (2)

bluefox
bluefox

Reputation: 53

My suggestion: you have to subscribe to vscode.debug.onDidStartDebugSession and vscode.debug.onDidTerminateDebugSession to keep track of when a session starts/ends or use onDidChangeActiveDebugSession when changing. At that point, you can know that a session is active, and your code should run.

Upvotes: 1

robe007
robe007

Reputation: 3927

I have solved the issue, following these steps:

  • create a simple "Hello World" extension with yeoman.
  • replace the extension.js with the code from above.
  • press F5 to launch (and debug) extension.
  • in new window opened, click on File Menu, then click on Open Folder, and select a folder where is a simple node.js program or a vscode workplace (this is very important).
  • Set a breakpoint in some line (also important to a appreciate the behaviour of the test).
  • press F5 to debug simple program (this is how to open an active debug session).
  • press F1 and type "Hello" and ran the "Hello World" extension.
  • Appreciate the results !!!

Upvotes: 2

Related Questions