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
Original file line number Diff line number Diff line change
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
11 changes: 10 additions & 1 deletion packages/core/lib/commands/debug/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = async function (options) {
const FromHardhat = require("@truffle/from-hardhat");
const Codec = require("@truffle/codec");
const TruffleError = require("@truffle/error");
const { CLIDebugger } = require("../../debug");
const { CLIDebugger, VSCodeDebugger } = require("../../debug");

if (options.url && options.network) {
const message =
Expand Down Expand Up @@ -60,6 +60,15 @@ module.exports = async function (options) {
if (config.compileAll && config.compileNone) {
throw new Error("Incompatible options passed regarding what to compile");
}

// Checks if the user wants to open the debugger in vscode
if (config.vscode) {
// await new VSCodeDebugger(config, txHash).run();
const vsDebugger = new VSCodeDebugger(config, txHash);
await vsDebugger.run();
return;
}

const interpreter = await new CLIDebugger(config, {
txHash,
compilations
Expand Down
4 changes: 3 additions & 1 deletion packages/core/lib/debug/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { CLIDebugger } = require("./cli");
const { VSCodeDebugger } = require("./vscode");

module.exports = {
CLIDebugger
CLIDebugger,
VSCodeDebugger
};
73 changes: 73 additions & 0 deletions packages/core/lib/debug/vscode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const childProcess = require("child_process");

class VSCodeDebugger {

Choose a reason for hiding this comment

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

Just a small detail, the name VSCodeDebugger might be a bit misleading. What about VSCodeOpenDebugger or VSCodeDebugAdapter?

constructor(config, txHash) {
this.config = config;
this.txHash = txHash;
}

/**
* This function is responsible for opening the debugger in vscode.
*/
async run() {

Choose a reason for hiding this comment

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

Given the purpose of this method, I would name it open instead of run.

// Sets the URL
const url = new URL("/debug", "vscode://trufflesuite-csi.truffle-vscode");

const disableFetchExternal = this.config.fetchExternal ? false : true;

// Sets the query parameters
url.searchParams.set("txHash", this.txHash);
url.searchParams.set("workingDirectory", this.config.working_directory);
url.searchParams.set("providerUrl", this.getProviderUrl());
url.searchParams.set("network", this.config.network);
url.searchParams.set("disableFetchExternal", disableFetchExternal);

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

Choose a reason for hiding this comment

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

Does open also works in Linux?

Copy link
Member

Choose a reason for hiding this comment

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

IIRC, win32: start, osX: open, and *nix is xdg-open or similar resource openers mimeopen, mimeo, linopen, handlr etc.

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...");
}

/**
* This function is for getting the provider URL.
*/
getProviderUrl() {
// Checks if the user is using a custom provider
if (this.config.url) {
return this.config.url;
}

// Checks if there is a provider in the config
if (this.config.provider) {
return this.config.provider.host;
}

// Creates the provider URL from host and port
if (this.config.network_config) {
return new URL(
`http://${this.config.network_config.host}:${this.config.network_config.port}`
).toString();
Comment on lines +64 to +66

Choose a reason for hiding this comment

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

Is it worth to do new URL(string).toString()? In other words, what's the issue with returning just http://[...]?

}
}
}

module.exports = {
VSCodeDebugger
};