mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Fix and cleanup VSCode task building
This commit is contained in:
parent
6b8b8ff4c5
commit
8846b5cf4a
5 changed files with 85 additions and 72 deletions
|
@ -1,27 +1,30 @@
|
|||
import * as vscode from "vscode";
|
||||
import type { Config } from "./config";
|
||||
import { log } from "./util";
|
||||
import { expectNotUndefined, unwrapUndefinable } from "./undefinable";
|
||||
import { unwrapUndefinable } from "./undefinable";
|
||||
import * as toolchain from "./toolchain";
|
||||
|
||||
// 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 CARGO_TASK_TYPE = "cargo";
|
||||
export const SHELL_TASK_TYPE = "shell";
|
||||
|
||||
export const TASK_SOURCE = "rust";
|
||||
export const RUST_TASK_SOURCE = "rust";
|
||||
|
||||
export interface RustTargetDefinition extends vscode.TaskDefinition {
|
||||
// The cargo command, such as "run" or "check".
|
||||
export type RustTargetDefinition = {
|
||||
readonly type: typeof CARGO_TASK_TYPE | typeof SHELL_TASK_TYPE;
|
||||
} & vscode.TaskDefinition &
|
||||
RustTarget;
|
||||
export type RustTarget = {
|
||||
// The command to run, usually `cargo`.
|
||||
command: string;
|
||||
// Additional arguments passed to the cargo command.
|
||||
// Additional arguments passed to the command.
|
||||
args?: string[];
|
||||
// The working directory to run the cargo command in.
|
||||
// The working directory to run the command in.
|
||||
cwd?: string;
|
||||
// The shell environment.
|
||||
env?: { [key: string]: string };
|
||||
// Override the cargo executable name, such as
|
||||
// "my_custom_cargo_bin".
|
||||
overrideCargo?: string;
|
||||
}
|
||||
};
|
||||
|
||||
class RustTaskProvider implements vscode.TaskProvider {
|
||||
private readonly config: Config;
|
||||
|
@ -31,6 +34,10 @@ class RustTaskProvider implements vscode.TaskProvider {
|
|||
}
|
||||
|
||||
async provideTasks(): Promise<vscode.Task[]> {
|
||||
if (!vscode.workspace.workspaceFolders) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Detect Rust tasks. Currently we do not do any actual detection
|
||||
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
|
||||
// set of tasks that always exist. These tasks cannot be removed in
|
||||
|
@ -45,15 +52,23 @@ class RustTaskProvider implements vscode.TaskProvider {
|
|||
{ command: "run", group: undefined },
|
||||
];
|
||||
|
||||
// FIXME: The server should provide this
|
||||
const cargo = await toolchain.cargoPath();
|
||||
|
||||
const tasks: vscode.Task[] = [];
|
||||
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
|
||||
for (const workspaceTarget of vscode.workspace.workspaceFolders) {
|
||||
for (const def of defs) {
|
||||
const definition = {
|
||||
command: cargo,
|
||||
args: [def.command],
|
||||
};
|
||||
const exec = await targetToExecution(definition, this.config.cargoRunner);
|
||||
const vscodeTask = await buildRustTask(
|
||||
workspaceTarget,
|
||||
{ type: TASK_TYPE, command: def.command },
|
||||
{ ...definition, type: CARGO_TASK_TYPE },
|
||||
`cargo ${def.command}`,
|
||||
this.config.problemMatcher,
|
||||
this.config.cargoRunner,
|
||||
exec,
|
||||
);
|
||||
vscodeTask.group = def.group;
|
||||
tasks.push(vscodeTask);
|
||||
|
@ -67,16 +82,24 @@ class RustTaskProvider implements vscode.TaskProvider {
|
|||
// VSCode calls this for every cargo task in the user's tasks.json,
|
||||
// we need to inform VSCode how to execute that command by creating
|
||||
// a ShellExecution for it.
|
||||
|
||||
const definition = task.definition as RustTargetDefinition;
|
||||
|
||||
if (definition.type === TASK_TYPE) {
|
||||
if (task.definition.type === CARGO_TASK_TYPE) {
|
||||
const taskDefinition = task.definition as RustTargetDefinition;
|
||||
const cargo = await toolchain.cargoPath();
|
||||
const exec = await targetToExecution(
|
||||
{
|
||||
command: cargo,
|
||||
args: [taskDefinition.command].concat(taskDefinition.args || []),
|
||||
cwd: taskDefinition.cwd,
|
||||
env: taskDefinition.env,
|
||||
},
|
||||
this.config.cargoRunner,
|
||||
);
|
||||
return await buildRustTask(
|
||||
task.scope,
|
||||
definition,
|
||||
taskDefinition,
|
||||
task.name,
|
||||
this.config.problemMatcher,
|
||||
this.config.cargoRunner,
|
||||
exec,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -89,34 +112,31 @@ export async function buildRustTask(
|
|||
definition: RustTargetDefinition,
|
||||
name: string,
|
||||
problemMatcher: string[],
|
||||
customRunner?: string,
|
||||
throwOnError: boolean = false,
|
||||
exec: vscode.ProcessExecution | vscode.ShellExecution,
|
||||
): Promise<vscode.Task> {
|
||||
const exec = await cargoToExecution(definition, customRunner, throwOnError);
|
||||
|
||||
return new vscode.Task(
|
||||
definition,
|
||||
// scope can sometimes be undefined. in these situations we default to the workspace taskscope as
|
||||
// recommended by the official docs: https://code.visualstudio.com/api/extension-guides/task-provider#task-provider)
|
||||
scope ?? vscode.TaskScope.Workspace,
|
||||
name,
|
||||
TASK_SOURCE,
|
||||
RUST_TASK_SOURCE,
|
||||
exec,
|
||||
problemMatcher,
|
||||
);
|
||||
}
|
||||
|
||||
async function cargoToExecution(
|
||||
definition: RustTargetDefinition,
|
||||
customRunner: string | undefined,
|
||||
throwOnError: boolean,
|
||||
export async function targetToExecution(
|
||||
definition: RustTarget,
|
||||
customRunner?: string,
|
||||
throwOnError: boolean = false,
|
||||
): Promise<vscode.ProcessExecution | vscode.ShellExecution> {
|
||||
if (customRunner) {
|
||||
const runnerCommand = `${customRunner}.buildShellExecution`;
|
||||
|
||||
try {
|
||||
const runnerArgs = {
|
||||
kind: TASK_TYPE,
|
||||
kind: CARGO_TASK_TYPE,
|
||||
args: definition.args,
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
|
@ -136,37 +156,14 @@ async function cargoToExecution(
|
|||
// fallback to default processing
|
||||
}
|
||||
}
|
||||
|
||||
// this is a cargo task; do Cargo-esque processing
|
||||
if (definition.type === TASK_TYPE) {
|
||||
// Check whether we must use a user-defined substitute for cargo.
|
||||
// Split on spaces to allow overrides like "wrapper cargo".
|
||||
const cargoCommand = definition.overrideCargo?.split(" ") ?? [definition.command];
|
||||
|
||||
const definitionArgs = expectNotUndefined(
|
||||
definition.args,
|
||||
"args were not provided via runnables; this is a bug.",
|
||||
);
|
||||
const args = [...cargoCommand.slice(1), ...definitionArgs];
|
||||
const processName = unwrapUndefinable(cargoCommand[0]);
|
||||
|
||||
return new vscode.ProcessExecution(processName, args, {
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
});
|
||||
} else {
|
||||
// we've been handed a process definition by rust-analyzer, trust all its inputs
|
||||
// and make a shell execution.
|
||||
const args = unwrapUndefinable(definition.args);
|
||||
|
||||
return new vscode.ProcessExecution(definition.command, args, {
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
});
|
||||
}
|
||||
const args = unwrapUndefinable(definition.args);
|
||||
return new vscode.ProcessExecution(definition.command, args, {
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
});
|
||||
}
|
||||
|
||||
export function activateTaskProvider(config: Config): vscode.Disposable {
|
||||
const provider = new RustTaskProvider(config);
|
||||
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
||||
return vscode.tasks.registerTaskProvider(CARGO_TASK_TYPE, provider);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue