Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jekyll/vscode-extension.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,14 @@ This command would generate the following configuration:
"type": "ruby_lsp",
"name": "Debug",
"request": "launch",
"program": "ruby ${file}",
"command": "ruby",
},
{
"type": "ruby_lsp",
"request": "launch",
"name": "Debug test file",
"program": "ruby -Itest ${relativeFile}",
"command": "ruby -Itest",
"file": "${relativeFile}",
},
{
"type": "ruby_lsp",
Expand Down
5 changes: 3 additions & 2 deletions vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,14 @@ This command would generate the following configuration:
"type": "ruby_lsp",
"name": "Debug",
"request": "launch",
"program": "ruby ${file}",
"command": "ruby",
},
{
"type": "ruby_lsp",
"request": "launch",
"name": "Debug test file",
"program": "ruby -Itest ${relativeFile}",
"command": "ruby -Itest",
"file": "${relativeFile}",
},
{
"type": "ruby_lsp",
Expand Down
28 changes: 24 additions & 4 deletions vscode/src/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ export class Debugger implements vscode.DebugAdapterDescriptorFactory, vscode.De
type: "ruby_lsp",
name: "Debug script",
request: "launch",
// eslint-disable-next-line no-template-curly-in-string
program: "ruby ${file}",
command: "ruby",
},
{
type: "ruby_lsp",
name: "Debug test",
request: "launch",
command: "ruby -Itest",
// eslint-disable-next-line no-template-curly-in-string
program: "ruby -Itest ${relativeFile}",
file: "${relativeFile}",
},
{
type: "ruby_lsp",
Expand All @@ -98,6 +98,11 @@ export class Debugger implements vscode.DebugAdapterDescriptorFactory, vscode.De
throw new Error(`Couldn't find a workspace for URI: ${uri?.toString()}`);
}

if (!debugConfiguration.program && debugConfiguration.command && !debugConfiguration.file) {
// eslint-disable-next-line no-template-curly-in-string
debugConfiguration.file = "${file}";
}

if (debugConfiguration.env) {
// If the user has their own debug launch configurations, we still need to inject the Ruby environment
debugConfiguration.env = Object.assign(workspace.ruby.env, debugConfiguration.env);
Expand Down Expand Up @@ -225,12 +230,23 @@ export class Debugger implements vscode.DebugAdapterDescriptorFactory, vscode.De
return new Promise((resolve, reject) => {
const args = ["exec", "rdbg"];

const program =
configuration.program ??
(configuration.command
? `${configuration.command} ${this.quoteForShell(String(configuration.file))}`
: undefined);

if (!program) {
reject(new Error("Either `program` or `command` must be configured in launch.json"));
return;
}

// On Windows, we spawn the debugger with any available port. On Linux and macOS, we spawn it with a UNIX socket
if (port) {
args.push("--port", port.toString());
}

args.push("--open", "--command", "--", configuration.program);
args.push("--open", "--command", "--", program);

this.logDebuggerMessage(`Spawning debugger in directory ${cwd}`);
this.logDebuggerMessage(` Command bundle ${args.join(" ")}`);
Expand Down Expand Up @@ -324,4 +340,8 @@ export class Debugger implements vscode.DebugAdapterDescriptorFactory, vscode.De
// so we preserve the original message format including any newlines
this.console.append(message);
}

private quoteForShell(argument: string): string {
return `"${argument.replace(/["\\$`]/g, "\\$&")}"`;
}
}
39 changes: 34 additions & 5 deletions vscode/src/test/suite/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ suite("Debugger", () => {
type: "ruby_lsp",
name: "Debug script",
request: "launch",
// eslint-disable-next-line no-template-curly-in-string
program: "ruby ${file}",
command: "ruby",
},
{
type: "ruby_lsp",
name: "Debug test",
request: "launch",
command: "ruby -Itest",
// eslint-disable-next-line no-template-curly-in-string
program: "ruby -Itest ${relativeFile}",
file: "${relativeFile}",
},
{
type: "ruby_lsp",
Expand Down Expand Up @@ -99,6 +99,34 @@ suite("Debugger", () => {
context.subscriptions.forEach((subscription) => subscription.dispose());
});

test("Resolve configuration defaults file to active file when command is used", async () => {
const ruby = {
env: { bogus: "hello!" },
} as unknown as Ruby;
const workspaceFolder = {
name: "fake",
uri: vscode.Uri.file("fake"),
index: 0,
};
const debug = new Debugger(context, () => {
return {
ruby,
workspaceFolder,
} as Workspace;
});
const configs: any = await debug.resolveDebugConfiguration!(workspaceFolder, {
type: "ruby_lsp",
name: "Debug",
request: "launch",
command: "ruby",
});

// eslint-disable-next-line no-template-curly-in-string
assert.strictEqual(configs.file, "${file}");
debug.dispose();
context.subscriptions.forEach((subscription) => subscription.dispose());
});

test("Resolve configuration injects Ruby environment and allows users custom environment", async () => {
const ruby = { env: { bogus: "hello!" } } as unknown as Ruby;
const workspaceFolder = {
Expand Down Expand Up @@ -174,7 +202,7 @@ suite("Debugger", () => {
createRubySymlinks();
}

const tmpPath = fs.mkdtempSync(path.join(os.tmpdir(), "ruby-lsp-test-debugger"));
const tmpPath = fs.mkdtempSync(path.join(os.tmpdir(), "ruby-lsp test debugger with spaces-"));
fs.writeFileSync(path.join(tmpPath, "test.rb"), "1 + 1");
fs.writeFileSync(path.join(tmpPath, ".ruby-version"), RUBY_VERSION);
fs.writeFileSync(path.join(tmpPath, "Gemfile"), 'source "https://rubygems.org"\ngem "debug"');
Expand Down Expand Up @@ -210,7 +238,8 @@ suite("Debugger", () => {
type: "ruby_lsp",
name: "Debug",
request: "launch",
program: `ruby ${path.join(tmpPath, "test.rb")}`,
command: "ruby",
file: path.join(tmpPath, "test.rb"),
});
} catch (error: any) {
assert.fail(`Failed to launch debugger: ${error.message}`);
Expand Down
Loading