Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit 62d2f58

Browse files
committed
Switch to ShellExecution instead of full Task
1 parent d6b0e54 commit 62d2f58

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

rust-analyzer/editors/code/src/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
116116
env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }),
117117
};
118118

119-
const cargoTask = await tasks.buildCargoTask(definition, runnable.label, args, config.cargoRunner);
119+
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
120+
const cargoTask = await tasks.buildCargoTask(target, definition, runnable.label, args, config.cargoRunner, true);
120121
cargoTask.presentationOptions.clear = true;
121122

122123
return cargoTask;

rust-analyzer/editors/code/src/tasks.ts

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,28 @@ class CargoTaskProvider implements vscode.TaskProvider {
2424
this.config = config;
2525
}
2626

27-
provideTasks(): vscode.Task[] {
27+
async provideTasks(): Promise<vscode.Task[]> {
2828
// Detect Rust tasks. Currently we do not do any actual detection
2929
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
3030
// set of tasks that always exist. These tasks cannot be removed in
3131
// tasks.json - only tweaked.
3232

33-
const cargoPath = toolchain.cargoPath();
34-
35-
return [
33+
const defs = [
3634
{ command: 'build', group: vscode.TaskGroup.Build },
3735
{ command: 'check', group: vscode.TaskGroup.Build },
3836
{ command: 'test', group: vscode.TaskGroup.Test },
3937
{ command: 'clean', group: vscode.TaskGroup.Clean },
4038
{ command: 'run', group: undefined },
41-
]
42-
.map(({ command, group }) => {
43-
const vscodeTask = new vscode.Task(
44-
// The contents of this object end up in the tasks.json entries.
45-
{
46-
type: TASK_TYPE,
47-
command,
48-
},
49-
// The scope of the task - workspace or specific folder (global
50-
// is not supported).
51-
this.target,
52-
// The task name, and task source. These are shown in the UI as
53-
// `${source}: ${name}`, e.g. `rust: cargo build`.
54-
`cargo ${command}`,
55-
'rust',
56-
// What to do when this command is executed.
57-
new vscode.ShellExecution(cargoPath, [command]),
58-
// Problem matchers.
59-
['$rustc'],
60-
);
61-
vscodeTask.group = group;
62-
return vscodeTask;
63-
});
39+
];
40+
41+
const tasks: vscode.Task[] = [];
42+
for (const def of defs) {
43+
const vscodeTask = await buildCargoTask(this.target, { type: TASK_TYPE, command: def.command }, `cargo ${def.command}`, [def.command], this.config.cargoRunner);
44+
vscodeTask.group = def.group;
45+
tasks.push(vscodeTask);
46+
}
47+
48+
return tasks;
6449
}
6550

6651
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
@@ -73,38 +58,56 @@ class CargoTaskProvider implements vscode.TaskProvider {
7358
if (definition.type === TASK_TYPE && definition.command) {
7459
const args = [definition.command].concat(definition.args ?? []);
7560

76-
return await buildCargoTask(definition, task.name, args, this.config.cargoRunner);
61+
return await buildCargoTask(this.target, definition, task.name, args, this.config.cargoRunner);
7762
}
7863

7964
return undefined;
8065
}
8166
}
8267

83-
export async function buildCargoTask(definition: CargoTaskDefinition, name: string, args: string[], customRunner?: string): Promise<vscode.Task> {
68+
export async function buildCargoTask(
69+
target: vscode.WorkspaceFolder,
70+
definition: CargoTaskDefinition,
71+
name: string,
72+
args: string[],
73+
customRunner?: string,
74+
throwOnError: boolean = false
75+
): Promise<vscode.Task> {
76+
77+
let exec: vscode.ShellExecution | undefined = undefined;
78+
8479
if (customRunner) {
85-
const runnerCommand = `${customRunner}.createCargoTask`;
80+
const runnerCommand = `${customRunner}.buildShellExecution`;
8681
try {
87-
const runnerArgs = { name, args, cwd: definition.cwd, env: definition.env, source: TASK_SOURCE };
88-
const task = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
89-
90-
if (task instanceof vscode.Task) {
91-
return task;
92-
} else if (task) {
93-
log.debug("Invalid cargo task", task);
94-
throw `Invalid task!`;
82+
const runnerArgs = { kind: TASK_TYPE, args, cwd: definition.cwd, env: definition.env };
83+
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
84+
if (customExec) {
85+
if (customExec instanceof vscode.ShellExecution) {
86+
exec = customExec as vscode.ShellExecution;
87+
} else {
88+
log.debug("Invalid cargo ShellExecution", customExec);
89+
throw "Invalid cargo ShellExecution.";
90+
}
9591
}
9692
// fallback to default processing
9793

9894
} catch (e) {
99-
throw `Cargo runner '${customRunner}' failed! ${e}`;
95+
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
96+
// fallback to default processing
10097
}
10198
}
10299

100+
if (!exec) {
101+
exec = new vscode.ShellExecution(toolchain.cargoPath(), args, definition)
102+
}
103+
103104
return new vscode.Task(
104105
definition,
106+
target,
105107
name,
106108
TASK_SOURCE,
107-
new vscode.ShellExecution(toolchain.cargoPath(), args, definition),
109+
exec,
110+
['$rustc']
108111
);
109112
}
110113

0 commit comments

Comments
 (0)