Skip to content

Commit

Permalink
refactor: Store the CLI command directly in RustTargetDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Mar 14, 2024
1 parent 2e109c7 commit 4422a90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
14 changes: 13 additions & 1 deletion editors/code/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import type * as lc from "vscode-languageclient";
import * as ra from "./lsp_ext";
import * as tasks from "./tasks";
import * as toolchain from "./toolchain";

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

const args = createArgs(runnable);
let program: string;
let args = createArgs(runnable);
if (runnable.args.overrideCargo) {
// Split on spaces to allow overrides like "wrapper cargo".
const cargoParts = runnable.args.overrideCargo.split(" ");

program = unwrapUndefinable(cargoParts[0]);
args = [...cargoParts.slice(1), ...args];
} else {
program = await toolchain.cargoPath();
}

const definition: tasks.RustTargetDefinition = {
type: tasks.TASK_TYPE,
program,
args,
cwd: runnable.args.workspaceRoot || ".",
env: prepareEnv(runnable, config.runnablesExtraEnv),
Expand Down
18 changes: 5 additions & 13 deletions editors/code/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import * as vscode from "vscode";
import * as toolchain from "./toolchain";
import type { Config } from "./config";
import { log } from "./util";
import { unwrapUndefinable } from "./undefinable";

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

export interface RustTargetDefinition extends vscode.TaskDefinition {
program: string;
args: string[];
cwd?: string;
env?: { [key: string]: string };
overrideCargo?: string;
}

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

const cargoPath = await toolchain.cargoPath();

const tasks: vscode.Task[] = [];
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
for (const def of defs) {
const vscodeTask = await buildRustTask(
workspaceTarget,
{ type: TASK_TYPE, args: [def.command] },
{ type: TASK_TYPE, program: cargoPath, args: [def.command] },
`cargo ${def.command}`,
this.config.problemMatcher,
this.config.cargoRunner,
Expand Down Expand Up @@ -113,16 +114,7 @@ export async function buildRustTask(
}

if (!exec) {
// Check whether we must use a user-defined substitute for cargo.
// Split on spaces to allow overrides like "wrapper cargo".
const overrideCargo = definition.overrideCargo ?? definition.overrideCargo;
const cargoPath = await toolchain.cargoPath();
const cargoCommand = overrideCargo?.split(" ") ?? [cargoPath];

const fullCommand = [...cargoCommand, ...definition.args];

const processName = unwrapUndefinable(fullCommand[0]);
exec = new vscode.ProcessExecution(processName, fullCommand.slice(1), definition);
exec = new vscode.ProcessExecution(definition.program, definition.args, definition);
}

return new vscode.Task(
Expand Down

0 comments on commit 4422a90

Please sign in to comment.