From 801061f01bccb2e945752966c100accff980f325 Mon Sep 17 00:00:00 2001 From: xhulz Date: Mon, 7 Nov 2022 17:26:16 +0000 Subject: [PATCH 1/8] feat: first version of using debugging in vscode has been implemented --- packages/core/lib/commands/debug/meta.js | 9 +++++++ packages/core/lib/commands/debug/run.js | 12 +++++++-- packages/core/lib/debug/cli.js | 33 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/core/lib/commands/debug/meta.js b/packages/core/lib/commands/debug/meta.js index e269f827924..cfd2a771f97 100644 --- a/packages/core/lib/commands/debug/meta.js +++ b/packages/core/lib/commands/debug/meta.js @@ -31,6 +31,11 @@ module.exports = { describe: "Force debugger to skip compilation (dangerous!)", type: "boolean", default: false + }, + "vscode": { + describe: "Starts the debug session on Truffle VSCode extension", + type: "boolean", + default: false } }, help: { @@ -75,6 +80,10 @@ 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 on Truffle VSCode extension." } ], allowedGlobalOptions: ["config"] diff --git a/packages/core/lib/commands/debug/run.js b/packages/core/lib/commands/debug/run.js index a7d6d903900..033274cd186 100644 --- a/packages/core/lib/commands/debug/run.js +++ b/packages/core/lib/commands/debug/run.js @@ -60,9 +60,17 @@ 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, { + + const cliDebugger = new CLIDebugger(config, { txHash, compilations - }).run(); + }); + + if (config.vscode) { + await cliDebugger.openVSCodeDebug(); + return; + } + + const interpreter = await cliDebugger.run(); return await promisify(interpreter.start.bind(interpreter))(); }; diff --git a/packages/core/lib/debug/cli.js b/packages/core/lib/debug/cli.js index b5b57811c61..b62c7a80f56 100644 --- a/packages/core/lib/debug/cli.js +++ b/packages/core/lib/debug/cli.js @@ -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"); @@ -204,6 +205,38 @@ class CLIDebugger { return contracts; } + + async openVSCodeDebug() { + // Sets the parameters + const params = new URLSearchParams({ + txHash: this.txHash, + workingDirectory: this.config.working_directory, + providerUrl: this.config.provider.host + }); + + // Opens VSCode based on the OS + const openCommand = process.platform === "win32" ? `start ""` : `open`; + const commandLine = `${openCommand} "vscode://trufflesuite-csi.truffle-vscode/debug?${params}"`; + + // 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(commandLine); + this.config.logger.log("Opening truffle debugger in VSCode..."); + } } module.exports = { From 60b00a23411d81fa04cd9ec265484fb3c75ba98a Mon Sep 17 00:00:00 2001 From: xhulz Date: Mon, 7 Nov 2022 20:52:36 +0000 Subject: [PATCH 2/8] fix: a url object was created to replace string url concatenation --- packages/core/lib/debug/cli.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/core/lib/debug/cli.js b/packages/core/lib/debug/cli.js index b62c7a80f56..c4948bad485 100644 --- a/packages/core/lib/debug/cli.js +++ b/packages/core/lib/debug/cli.js @@ -207,16 +207,17 @@ class CLIDebugger { } async openVSCodeDebug() { - // Sets the parameters - const params = new URLSearchParams({ - txHash: this.txHash, - workingDirectory: this.config.working_directory, - providerUrl: this.config.provider.host - }); + // 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); // Opens VSCode based on the OS const openCommand = process.platform === "win32" ? `start ""` : `open`; - const commandLine = `${openCommand} "vscode://trufflesuite-csi.truffle-vscode/debug?${params}"`; + const commandLine = `${openCommand} "${url.toString()}"`; // Defines the options for the child process. An abort signal is used to cancel the process, if necessary. const controller = new AbortController(); From c878daad1292e4593039f2737240b4baf8819ca9 Mon Sep 17 00:00:00 2001 From: xhulz Date: Wed, 9 Nov 2022 11:12:13 +0000 Subject: [PATCH 3/8] chore: new parameter (fetchExternal) has been added to the serch params object --- packages/core/lib/commands/debug/meta.js | 3 ++- packages/core/lib/debug/cli.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/core/lib/commands/debug/meta.js b/packages/core/lib/commands/debug/meta.js index cfd2a771f97..9358f4dc060 100644 --- a/packages/core/lib/commands/debug/meta.js +++ b/packages/core/lib/commands/debug/meta.js @@ -83,7 +83,8 @@ module.exports = { }, { option: "--vscode", - description: "Starts the debug session on Truffle VSCode extension." + description: + "Starts the debug session in VS Code using the Truffle for VS Code extension." } ], allowedGlobalOptions: ["config"] diff --git a/packages/core/lib/debug/cli.js b/packages/core/lib/debug/cli.js index c4948bad485..5777aaa483b 100644 --- a/packages/core/lib/debug/cli.js +++ b/packages/core/lib/debug/cli.js @@ -214,10 +214,11 @@ class CLIDebugger { url.searchParams.set("txHash", this.txHash); url.searchParams.set("workingDirectory", this.config.working_directory); url.searchParams.set("providerUrl", this.config.provider.host); + url.searchParams.set("fetchExternal", this.config.fetchExternal); // Opens VSCode based on the OS const openCommand = process.platform === "win32" ? `start ""` : `open`; - const commandLine = `${openCommand} "${url.toString()}"`; + 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(); @@ -235,7 +236,6 @@ class CLIDebugger { }); // Sends a message to the user - this.config.logger.log(commandLine); this.config.logger.log("Opening truffle debugger in VSCode..."); } } From 19ce3329cf6158b67b0fcea3db5fa8a34eee9bc6 Mon Sep 17 00:00:00 2001 From: xhulz Date: Wed, 9 Nov 2022 11:24:33 +0000 Subject: [PATCH 4/8] fix: some comments has been implemented / changed --- packages/core/lib/commands/debug/meta.js | 3 ++- packages/core/lib/commands/debug/run.js | 3 +++ packages/core/lib/debug/cli.js | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/lib/commands/debug/meta.js b/packages/core/lib/commands/debug/meta.js index 9358f4dc060..6698a4f72e5 100644 --- a/packages/core/lib/commands/debug/meta.js +++ b/packages/core/lib/commands/debug/meta.js @@ -33,7 +33,8 @@ module.exports = { default: false }, "vscode": { - describe: "Starts the debug session on Truffle VSCode extension", + describe: + "Starts the debug session in VS Code using the Truffle for VS Code extension", type: "boolean", default: false } diff --git a/packages/core/lib/commands/debug/run.js b/packages/core/lib/commands/debug/run.js index 033274cd186..a2e249f47e5 100644 --- a/packages/core/lib/commands/debug/run.js +++ b/packages/core/lib/commands/debug/run.js @@ -61,16 +61,19 @@ module.exports = async function (options) { throw new Error("Incompatible options passed regarding what to compile"); } + // Create a new CLIDebugger instance const cliDebugger = new CLIDebugger(config, { txHash, compilations }); + // Checks if the user wants to debug in vscode if (config.vscode) { await cliDebugger.openVSCodeDebug(); return; } + // Starts the cli debugger const interpreter = await cliDebugger.run(); return await promisify(interpreter.start.bind(interpreter))(); }; diff --git a/packages/core/lib/debug/cli.js b/packages/core/lib/debug/cli.js index 5777aaa483b..98bf8c004ef 100644 --- a/packages/core/lib/debug/cli.js +++ b/packages/core/lib/debug/cli.js @@ -206,6 +206,9 @@ class CLIDebugger { return contracts; } + /** + * This function is responsible for opening the debug in vscode. + */ async openVSCodeDebug() { // Sets the URL const url = new URL("/debug", "vscode://trufflesuite-csi.truffle-vscode"); @@ -216,7 +219,7 @@ class CLIDebugger { url.searchParams.set("providerUrl", this.config.provider.host); url.searchParams.set("fetchExternal", this.config.fetchExternal); - // Opens VSCode based on the OS + // Opens VSCode based on OS const openCommand = process.platform === "win32" ? `start ""` : `open`; const commandLine = `${openCommand} "${url}"`; From eff430c19b66817444234516fcca1947f5f7aea1 Mon Sep 17 00:00:00 2001 From: xhulz Date: Wed, 9 Nov 2022 11:28:14 +0000 Subject: [PATCH 5/8] fix: some comments has been changed --- packages/core/lib/commands/debug/run.js | 2 +- packages/core/lib/debug/cli.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/lib/commands/debug/run.js b/packages/core/lib/commands/debug/run.js index a2e249f47e5..027f7d7b2e1 100644 --- a/packages/core/lib/commands/debug/run.js +++ b/packages/core/lib/commands/debug/run.js @@ -67,7 +67,7 @@ module.exports = async function (options) { compilations }); - // Checks if the user wants to debug in vscode + // Checks if the user wants to open the debugger in vscode if (config.vscode) { await cliDebugger.openVSCodeDebug(); return; diff --git a/packages/core/lib/debug/cli.js b/packages/core/lib/debug/cli.js index 98bf8c004ef..ae050b4f9ca 100644 --- a/packages/core/lib/debug/cli.js +++ b/packages/core/lib/debug/cli.js @@ -207,7 +207,7 @@ class CLIDebugger { } /** - * This function is responsible for opening the debug in vscode. + * This function is responsible for opening the debugger in vscode. */ async openVSCodeDebug() { // Sets the URL From 889a667879b004c990311aae828adab909d7704f Mon Sep 17 00:00:00 2001 From: xhulz Date: Mon, 14 Nov 2022 09:17:47 +0000 Subject: [PATCH 6/8] chore: the vs code debug functions have been isolated in a new class --- packages/core/lib/commands/debug/run.js | 16 ++++----- packages/core/lib/debug/cli.js | 37 ------------------- packages/core/lib/debug/index.js | 4 ++- packages/core/lib/debug/vscode.js | 48 +++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 packages/core/lib/debug/vscode.js diff --git a/packages/core/lib/commands/debug/run.js b/packages/core/lib/commands/debug/run.js index 027f7d7b2e1..4c7446c0f9e 100644 --- a/packages/core/lib/commands/debug/run.js +++ b/packages/core/lib/commands/debug/run.js @@ -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 = @@ -61,19 +61,15 @@ module.exports = async function (options) { throw new Error("Incompatible options passed regarding what to compile"); } - // Create a new CLIDebugger instance - const cliDebugger = new CLIDebugger(config, { - txHash, - compilations - }); - // Checks if the user wants to open the debugger in vscode if (config.vscode) { - await cliDebugger.openVSCodeDebug(); + await new VSCodeDebugger(config, txHash).run(); return; } - // Starts the cli debugger - const interpreter = await cliDebugger.run(); + const interpreter = await new CLIDebugger(config, { + txHash, + compilations + }).run(); return await promisify(interpreter.start.bind(interpreter))(); }; diff --git a/packages/core/lib/debug/cli.js b/packages/core/lib/debug/cli.js index ae050b4f9ca..b5b57811c61 100644 --- a/packages/core/lib/debug/cli.js +++ b/packages/core/lib/debug/cli.js @@ -3,7 +3,6 @@ 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"); @@ -205,42 +204,6 @@ 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); - 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 = { diff --git a/packages/core/lib/debug/index.js b/packages/core/lib/debug/index.js index cd0d801a970..1dda86a07dd 100644 --- a/packages/core/lib/debug/index.js +++ b/packages/core/lib/debug/index.js @@ -1,5 +1,7 @@ const { CLIDebugger } = require("./cli"); +const { VSCodeDebugger } = require("./vscode"); module.exports = { - CLIDebugger + CLIDebugger, + VSCodeDebugger }; diff --git a/packages/core/lib/debug/vscode.js b/packages/core/lib/debug/vscode.js new file mode 100644 index 00000000000..988b5504445 --- /dev/null +++ b/packages/core/lib/debug/vscode.js @@ -0,0 +1,48 @@ +const childProcess = require("child_process"); + +class VSCodeDebugger { + constructor(config, txHash) { + this.config = config; + this.txHash = txHash; + } + + /** + * This function is responsible for opening the debugger in vscode. + */ + async run() { + // 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); + 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 = { + VSCodeDebugger +}; From 5b8c10aaa9040b50fca220587dc83f3d3f837cb1 Mon Sep 17 00:00:00 2001 From: xhulz Date: Wed, 16 Nov 2022 14:52:27 +0000 Subject: [PATCH 7/8] chore: network parameter has been added to search params on vscode debugger class --- packages/core/lib/debug/vscode.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/core/lib/debug/vscode.js b/packages/core/lib/debug/vscode.js index 988b5504445..55e08fd4656 100644 --- a/packages/core/lib/debug/vscode.js +++ b/packages/core/lib/debug/vscode.js @@ -12,12 +12,17 @@ class VSCodeDebugger { async run() { // Sets the URL const url = new URL("/debug", "vscode://trufflesuite-csi.truffle-vscode"); + const providerUrl = this.config.url + ? this.config.url + : this.config.provider.host; + 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.config.provider.host); - url.searchParams.set("fetchExternal", this.config.fetchExternal); + url.searchParams.set("providerUrl", providerUrl); + url.searchParams.set("network", this.config.network); + url.searchParams.set("disableFetchExternal", disableFetchExternal); // Opens VSCode based on OS const openCommand = process.platform === "win32" ? `start ""` : `open`; From b7616f1005efb60b262962fc1e627cc4b64c9dda Mon Sep 17 00:00:00 2001 From: xhulz Date: Mon, 21 Nov 2022 15:51:42 +0000 Subject: [PATCH 8/8] refactoring: a new method for retrieving the providerUrl was created --- packages/core/lib/commands/debug/run.js | 4 +++- packages/core/lib/debug/vscode.js | 28 +++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/core/lib/commands/debug/run.js b/packages/core/lib/commands/debug/run.js index 4c7446c0f9e..02346308bbe 100644 --- a/packages/core/lib/commands/debug/run.js +++ b/packages/core/lib/commands/debug/run.js @@ -63,7 +63,9 @@ module.exports = async function (options) { // Checks if the user wants to open the debugger in vscode if (config.vscode) { - await new VSCodeDebugger(config, txHash).run(); + // await new VSCodeDebugger(config, txHash).run(); + const vsDebugger = new VSCodeDebugger(config, txHash); + await vsDebugger.run(); return; } diff --git a/packages/core/lib/debug/vscode.js b/packages/core/lib/debug/vscode.js index 55e08fd4656..922bdc2eb11 100644 --- a/packages/core/lib/debug/vscode.js +++ b/packages/core/lib/debug/vscode.js @@ -12,15 +12,13 @@ class VSCodeDebugger { async run() { // Sets the URL const url = new URL("/debug", "vscode://trufflesuite-csi.truffle-vscode"); - const providerUrl = this.config.url - ? this.config.url - : this.config.provider.host; + 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", providerUrl); + url.searchParams.set("providerUrl", this.getProviderUrl()); url.searchParams.set("network", this.config.network); url.searchParams.set("disableFetchExternal", disableFetchExternal); @@ -46,6 +44,28 @@ class VSCodeDebugger { // 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(); + } + } } module.exports = {