Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Running truffle debug with appropriate flag (--vscode) #5684

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
11 changes: 11 additions & 0 deletions packages/core/lib/commands/debug/meta.js
Expand Up @@ -31,6 +31,12 @@ module.exports = {
describe: "Force debugger to skip compilation (dangerous!)",
type: "boolean",
default: false
},
"vscode": {
describe:
"Starts the debug session in VS Code using the Truffle for VS Code extension",
type: "boolean",
default: false
}
},
help: {
Expand Down Expand Up @@ -75,6 +81,11 @@ module.exports = {
option: "--compile-none",
description:
"Forces the debugger to use artifacts even if it detects a problem. Dangerous; may cause errors."
},
{
option: "--vscode",
description:
"Starts the debug session in VS Code using the Truffle for VS Code extension."
}
],
allowedGlobalOptions: ["config"]
Expand Down
15 changes: 13 additions & 2 deletions packages/core/lib/commands/debug/run.js
Expand Up @@ -60,9 +60,20 @@ module.exports = async function (options) {
if (config.compileAll && config.compileNone) {
throw new Error("Incompatible options passed regarding what to compile");
}
const interpreter = await new CLIDebugger(config, {

// Create a new CLIDebugger instance
const cliDebugger = new CLIDebugger(config, {
txHash,
compilations
}).run();
});

// Checks if the user wants to open the debugger in vscode
if (config.vscode) {
await cliDebugger.openVSCodeDebug();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this method not be part of the thing called the "CLI" debugger? Seems better to keep the CLIDebugger just for the CLI and push this VS Code logic elsewhere.

Copy link
Author

@xhulz xhulz Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @gnidan, Thank you for that

What would be the best place (package/class) to put this code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinbluer for visibility

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up @xhulz! Also sounds like we've got a way forward for this from our earlier conversation 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe just have a VSCodeDebugger class?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @gnidan @kevinbluer

I just moved all the vscode debug function to a new class called VSCodeDebugger.

Thank you all again

return;
}

// Starts the cli debugger
const interpreter = await cliDebugger.run();
return await promisify(interpreter.start.bind(interpreter))();
};
37 changes: 37 additions & 0 deletions packages/core/lib/debug/cli.js
Expand Up @@ -3,6 +3,7 @@ const debug = debugModule("lib:debug:cli");

const fs = require("fs-extra");
const path = require("path");
const childProcess = require("child_process");

const Debugger = require("@truffle/debugger");
const DebugUtils = require("@truffle/debug-utils");
Expand Down Expand Up @@ -204,6 +205,42 @@ class CLIDebugger {

return contracts;
}

/**
* This function is responsible for opening the debugger in vscode.
*/
async openVSCodeDebug() {
// Sets the URL
const url = new URL("/debug", "vscode://trufflesuite-csi.truffle-vscode");

// Sets the query parameters
url.searchParams.set("txHash", this.txHash);
url.searchParams.set("workingDirectory", this.config.working_directory);
url.searchParams.set("providerUrl", this.config.provider.host);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is brittle, since we can't always rely on being able to get a URL from a provider

url.searchParams.set("fetchExternal", this.config.fetchExternal);

// Opens VSCode based on OS
const openCommand = process.platform === "win32" ? `start ""` : `open`;
const commandLine = `${openCommand} "${url}"`;

// Defines the options for the child process. An abort signal is used to cancel the process, if necessary.
const controller = new AbortController();
const { signal } = controller;

// Executes the command
childProcess.exec(commandLine, { signal }, (stderr, error) => {
if (stderr) {
throw new Error(`Error opening the debug session in VSCode: ${stderr}`);
}
if (error) {
controller.abort();
throw new Error(`Error opening the debug session in VSCode: ${error}`);
}
});

// Sends a message to the user
this.config.logger.log("Opening truffle debugger in VSCode...");
}
}

module.exports = {
Expand Down