Skip to content

Commit 4422a90

Browse files
committed
refactor: Store the CLI command directly in RustTargetDefinition
1 parent 2e109c7 commit 4422a90

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

editors/code/src/run.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import type * as lc from "vscode-languageclient";
33
import * as ra from "./lsp_ext";
44
import * as tasks from "./tasks";
5+
import * as toolchain from "./toolchain";
56

67
import type { CtxInit } from "./ctx";
78
import { makeDebugConfig } from "./debug";
@@ -111,10 +112,21 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
111112
throw `Unexpected runnable kind: ${runnable.kind}`;
112113
}
113114

114-
const args = createArgs(runnable);
115+
let program: string;
116+
let args = createArgs(runnable);
117+
if (runnable.args.overrideCargo) {
118+
// Split on spaces to allow overrides like "wrapper cargo".
119+
const cargoParts = runnable.args.overrideCargo.split(" ");
120+
121+
program = unwrapUndefinable(cargoParts[0]);
122+
args = [...cargoParts.slice(1), ...args];
123+
} else {
124+
program = await toolchain.cargoPath();
125+
}
115126

116127
const definition: tasks.RustTargetDefinition = {
117128
type: tasks.TASK_TYPE,
129+
program,
118130
args,
119131
cwd: runnable.args.workspaceRoot || ".",
120132
env: prepareEnv(runnable, config.runnablesExtraEnv),

editors/code/src/tasks.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import * as vscode from "vscode";
22
import * as toolchain from "./toolchain";
33
import type { Config } from "./config";
44
import { log } from "./util";
5-
import { unwrapUndefinable } from "./undefinable";
65

76
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
87
// our configuration should be compatible with it so use the same key.
98
export const TASK_TYPE = "cargo";
109
export const TASK_SOURCE = "rust";
1110

1211
export interface RustTargetDefinition extends vscode.TaskDefinition {
12+
program: string;
1313
args: string[];
1414
cwd?: string;
1515
env?: { [key: string]: string };
16-
overrideCargo?: string;
1716
}
1817

1918
class RustTaskProvider implements vscode.TaskProvider {
@@ -38,12 +37,14 @@ class RustTaskProvider implements vscode.TaskProvider {
3837
{ command: "run", group: undefined },
3938
];
4039

40+
const cargoPath = await toolchain.cargoPath();
41+
4142
const tasks: vscode.Task[] = [];
4243
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
4344
for (const def of defs) {
4445
const vscodeTask = await buildRustTask(
4546
workspaceTarget,
46-
{ type: TASK_TYPE, args: [def.command] },
47+
{ type: TASK_TYPE, program: cargoPath, args: [def.command] },
4748
`cargo ${def.command}`,
4849
this.config.problemMatcher,
4950
this.config.cargoRunner,
@@ -113,16 +114,7 @@ export async function buildRustTask(
113114
}
114115

115116
if (!exec) {
116-
// Check whether we must use a user-defined substitute for cargo.
117-
// Split on spaces to allow overrides like "wrapper cargo".
118-
const overrideCargo = definition.overrideCargo ?? definition.overrideCargo;
119-
const cargoPath = await toolchain.cargoPath();
120-
const cargoCommand = overrideCargo?.split(" ") ?? [cargoPath];
121-
122-
const fullCommand = [...cargoCommand, ...definition.args];
123-
124-
const processName = unwrapUndefinable(fullCommand[0]);
125-
exec = new vscode.ProcessExecution(processName, fullCommand.slice(1), definition);
117+
exec = new vscode.ProcessExecution(definition.program, definition.args, definition);
126118
}
127119

128120
return new vscode.Task(

0 commit comments

Comments
 (0)