Skip to content

Commit

Permalink
fix: cargo run arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Jul 11, 2024
1 parent a6a8f15 commit 1410329
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 75 deletions.
72 changes: 42 additions & 30 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,37 +275,40 @@ export function debugSingle(ctx: Ctx): Cmd {
const { document } = await workspace.getCurrentState();
if (!runnable || !isRustDocument(document)) return;

const args = [...runnable.args.cargoArgs];
if (runnable.args.cargoExtraArgs.length > 0) {
args.push(...runnable.args.cargoExtraArgs);
let args: string[] = [];
if (runnable.kind === 'cargo') {
// TODO: runnable.args.overrideCargo?
args = [...runnable.args.cargoArgs];
if (runnable.args.executableArgs.length > 0) {
runnable.args['executableArgs'][0] = `'${runnable.args['executableArgs'][0]}'`;
args.push('--', ...runnable.args.executableArgs);
}
} else {
args = [...runnable.args.args];
}

// do not run tests, we will run through gdb
if (args[0] === 'test') {
args.push('--no-run');
}

// output as json
args.push('--message-format=json');

if (runnable.args.executableArgs.length > 0) {
args.push('--', ...runnable.args.executableArgs);
}

if (args[0] === 'run') {
args[0] = 'build';
}

console.debug(`${runnable.kind} ${args}`);
// output as json
args.push('--message-format=json');

console.debug(`${runnable.kind} ${args}`);
// We can extract a list of generated executables from the output of cargo,
// but if multiple executables are generated we need a way to find out which
// one should be used for debugging.
// From the arguments given to cargo, we can infer the kind and name of the executable
// and filter the list of executables accordingly.
let expectedKind: string | undefined;
let expectedName: string | undefined;
for (const arg of runnable.args.cargoArgs) {
const cargoArgs = runnable.kind === 'cargo' ? runnable.args.cargoArgs : [];
for (const arg of cargoArgs) {
// Find the argument indicating the kind of the executable.
if (expectedKind === undefined) {
switch (arg) {
Expand Down Expand Up @@ -341,7 +344,7 @@ export function debugSingle(ctx: Ctx): Cmd {
// --package argument we can get the name of the executable from it.
if (expectedName === undefined) {
let foundPackageArgument = false;
for (const arg of runnable.args.cargoArgs) {
for (const arg of cargoArgs) {
if (foundPackageArgument) {
expectedName = arg;
break;
Expand Down Expand Up @@ -418,7 +421,7 @@ export function debugSingle(ctx: Ctx): Cmd {
throw new Error('Could not find executable');
}

const executableArgs = runnable.args.executableArgs.join(' ');
const executableArgs = runnable.kind === 'cargo' ? runnable.args.executableArgs.join(' ') : '';

console.info(`Debugging executable: ${executable} ${executableArgs}`);

Expand Down Expand Up @@ -456,18 +459,22 @@ export function runSingle(ctx: Ctx): Cmd {
const { document } = await workspace.getCurrentState();
if (!runnable || !isRustDocument(document)) return;

const args = [...runnable.args.cargoArgs];
if (runnable.args.cargoExtraArgs.length > 0) {
args.push(...runnable.args.cargoExtraArgs);
}
if (runnable.args.executableArgs.length > 0) {
runnable.args['executableArgs'][0] = `'${runnable.args['executableArgs'][0]}'`;
args.push('--', ...runnable.args.executableArgs);
let args: string[] = [];
if (runnable.kind === 'cargo') {
// TODO: runnable.args.overrideCargo?
args = [...runnable.args.cargoArgs];
if (runnable.args.executableArgs.length > 0) {
runnable.args['executableArgs'][0] = `'${runnable.args['executableArgs'][0]}'`;
args.push('--', ...runnable.args.executableArgs);
}
} else {
args = [...runnable.args.args];
}

const cmd = `${runnable.kind} ${args.join(' ')}`;
const opt: TerminalOptions = {
name: runnable.label,
cwd: runnable.args.workspaceRoot,
cwd: runnable.args.cwd,
};
if (terminal) {
terminal.dispose();
Expand Down Expand Up @@ -689,8 +696,8 @@ export function openDocs(ctx: Ctx): Cmd {
position,
};
const doclink = await ctx.client.sendRequest(ra.openDocs, param);
if (doclink) {
await commands.executeCommand('vscode.open', Uri.parse(doclink));
if (doclink?.web) {
await commands.executeCommand('vscode.open', Uri.parse(doclink.web));
}
};
}
Expand Down Expand Up @@ -782,12 +789,17 @@ export function echoRunCommandLine(ctx: Ctx) {
return async () => {
const runnable = await pickRunnable(ctx);
if (!runnable) return;
const args = [...runnable.args.cargoArgs];
if (runnable.args.cargoExtraArgs) {
args.push(...runnable.args.cargoExtraArgs);
}
if (runnable.args.executableArgs.length > 0) {
args.push('--', ...runnable.args.executableArgs);

let args: string[] = [];
if (runnable.kind === 'cargo') {
// TODO: runnable.args.overrideCargo?
args = [...runnable.args.cargoArgs];
if (runnable.args.executableArgs.length > 0) {
runnable.args['executableArgs'][0] = `'${runnable.args['executableArgs'][0]}'`;
args.push('--', ...runnable.args.executableArgs);
}
} else {
args = [...runnable.args.args];
}
const commandLine = ['cargo', ...args].join(' ');
window.echoLines([commandLine]);
Expand Down
Loading

0 comments on commit 1410329

Please sign in to comment.