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

Commit 2cb8b60

Browse files
bors[bot]vsrs
andauthored
Merge #5017
5017: Add custom cargo runners support. r=matklad a=vsrs This PR adds an option to delegate actual cargo commands building to another extension. For example, to use a different manager like [cross](https://github.com/rust-embedded/cross). https://github.com/vsrs/cross-rust-analyzer is an example of such extension. I'll publish it after the rust-analyzer release with this functionality. Fixes rust-lang/rust-analyzer#4902 Co-authored-by: vsrs <[email protected]>
2 parents 74ac621 + 7b7c185 commit 2cb8b60

File tree

6 files changed

+104
-83
lines changed

6 files changed

+104
-83
lines changed

rust-analyzer/editors/code/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@
336336
"default": null,
337337
"description": "List of features to activate. Defaults to `rust-analyzer.cargo.features`."
338338
},
339+
"rust-analyzer.cargoRunner": {
340+
"type": [
341+
"null",
342+
"string"
343+
],
344+
"default": null,
345+
"description": "Custom cargo runner extension ID."
346+
},
339347
"rust-analyzer.inlayHints.enable": {
340348
"type": "boolean",
341349
"default": true,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export function run(ctx: Ctx): Cmd {
394394

395395
item.detail = 'rerun';
396396
prevRunnable = item;
397-
const task = createTask(item.runnable);
397+
const task = await createTask(item.runnable, ctx.config);
398398
return await vscode.tasks.executeTask(task);
399399
};
400400
}
@@ -404,7 +404,7 @@ export function runSingle(ctx: Ctx): Cmd {
404404
const editor = ctx.activeRustEditor;
405405
if (!editor) return;
406406

407-
const task = createTask(runnable);
407+
const task = await createTask(runnable, ctx.config);
408408
task.group = vscode.TaskGroup.Build;
409409
task.presentationOptions = {
410410
reveal: vscode.TaskRevealKind.Always,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export class Config {
110110
};
111111
}
112112

113+
get cargoRunner() {
114+
return this.get<string | undefined>("cargoRunner");
115+
}
116+
113117
get debug() {
114118
// "/rustc/<id>" used by suggestions only.
115119
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export async function activate(context: vscode.ExtensionContext) {
114114
ctx.registerCommand('applyActionGroup', commands.applyActionGroup);
115115
ctx.registerCommand('gotoLocation', commands.gotoLocation);
116116

117-
ctx.pushCleanup(activateTaskProvider(workspaceFolder));
117+
ctx.pushCleanup(activateTaskProvider(workspaceFolder, ctx.config));
118118

119119
activateInlayHints(ctx);
120120

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

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
33
import * as ra from './lsp_ext';
4-
import * as toolchain from "./toolchain";
4+
import * as tasks from './tasks';
55

66
import { Ctx } from './ctx';
77
import { makeDebugConfig } from './debug';
8+
import { Config } from './config';
89

910
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
1011

@@ -95,52 +96,29 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
9596
}
9697
}
9798

98-
interface CargoTaskDefinition extends vscode.TaskDefinition {
99-
type: 'cargo';
100-
label: string;
101-
command: string;
102-
args: string[];
103-
env?: { [key: string]: string };
104-
}
105-
106-
export function createTask(runnable: ra.Runnable): vscode.Task {
107-
const TASK_SOURCE = 'Rust';
99+
export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
100+
if (runnable.kind !== "cargo") {
101+
// rust-analyzer supports only one kind, "cargo"
102+
// do not use tasks.TASK_TYPE here, these are completely different meanings.
108103

109-
let command;
110-
switch (runnable.kind) {
111-
case "cargo": command = toolchain.getPathForExecutable("cargo");
104+
throw `Unexpected runnable kind: ${runnable.kind}`;
112105
}
106+
113107
const args = [...runnable.args.cargoArgs]; // should be a copy!
114108
if (runnable.args.executableArgs.length > 0) {
115109
args.push('--', ...runnable.args.executableArgs);
116110
}
117-
const definition: CargoTaskDefinition = {
118-
type: 'cargo',
119-
label: runnable.label,
120-
command,
121-
args,
111+
const definition: tasks.CargoTaskDefinition = {
112+
type: tasks.TASK_TYPE,
113+
command: args[0], // run, test, etc...
114+
args: args.slice(1),
115+
cwd: runnable.args.workspaceRoot,
122116
env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }),
123117
};
124118

125-
const execOption: vscode.ShellExecutionOptions = {
126-
cwd: runnable.args.workspaceRoot || '.',
127-
env: definition.env,
128-
};
129-
const exec = new vscode.ShellExecution(
130-
definition.command,
131-
definition.args,
132-
execOption,
133-
);
134-
135-
const f = vscode.workspace.workspaceFolders![0];
136-
const t = new vscode.Task(
137-
definition,
138-
f,
139-
definition.label,
140-
TASK_SOURCE,
141-
exec,
142-
['$rustc'],
143-
);
144-
t.presentationOptions.clear = true;
145-
return t;
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);
121+
cargoTask.presentationOptions.clear = true;
122+
123+
return cargoTask;
146124
}
Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as vscode from 'vscode';
22
import * as toolchain from "./toolchain";
3+
import { Config } from './config';
4+
import { log } from './util';
35

46
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
57
// our configuration should be compatible with it so use the same key.
6-
const TASK_TYPE = 'cargo';
8+
export const TASK_TYPE = 'cargo';
9+
export const TASK_SOURCE = 'rust';
710

8-
interface CargoTaskDefinition extends vscode.TaskDefinition {
11+
export interface CargoTaskDefinition extends vscode.TaskDefinition {
912
command?: string;
1013
args?: string[];
1114
cwd?: string;
@@ -14,73 +17,101 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
1417

1518
class CargoTaskProvider implements vscode.TaskProvider {
1619
private readonly target: vscode.WorkspaceFolder;
20+
private readonly config: Config;
1721

18-
constructor(target: vscode.WorkspaceFolder) {
22+
constructor(target: vscode.WorkspaceFolder, config: Config) {
1923
this.target = target;
24+
this.config = config;
2025
}
2126

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

28-
const cargoPath = toolchain.cargoPath();
29-
30-
return [
33+
const defs = [
3134
{ command: 'build', group: vscode.TaskGroup.Build },
3235
{ command: 'check', group: vscode.TaskGroup.Build },
3336
{ command: 'test', group: vscode.TaskGroup.Test },
3437
{ command: 'clean', group: vscode.TaskGroup.Clean },
3538
{ command: 'run', group: undefined },
36-
]
37-
.map(({ command, group }) => {
38-
const vscodeTask = new vscode.Task(
39-
// The contents of this object end up in the tasks.json entries.
40-
{
41-
type: TASK_TYPE,
42-
command,
43-
},
44-
// The scope of the task - workspace or specific folder (global
45-
// is not supported).
46-
this.target,
47-
// The task name, and task source. These are shown in the UI as
48-
// `${source}: ${name}`, e.g. `rust: cargo build`.
49-
`cargo ${command}`,
50-
'rust',
51-
// What to do when this command is executed.
52-
new vscode.ShellExecution(cargoPath, [command]),
53-
// Problem matchers.
54-
['$rustc'],
55-
);
56-
vscodeTask.group = group;
57-
return vscodeTask;
58-
});
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;
5949
}
6050

61-
resolveTask(task: vscode.Task): vscode.Task | undefined {
51+
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
6252
// VSCode calls this for every cargo task in the user's tasks.json,
6353
// we need to inform VSCode how to execute that command by creating
6454
// a ShellExecution for it.
6555

6656
const definition = task.definition as CargoTaskDefinition;
6757

68-
if (definition.type === 'cargo' && definition.command) {
58+
if (definition.type === TASK_TYPE && definition.command) {
6959
const args = [definition.command].concat(definition.args ?? []);
7060

71-
return new vscode.Task(
72-
definition,
73-
task.name,
74-
'rust',
75-
new vscode.ShellExecution('cargo', args, definition),
76-
);
61+
return await buildCargoTask(this.target, definition, task.name, args, this.config.cargoRunner);
7762
}
7863

7964
return undefined;
8065
}
8166
}
8267

83-
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
84-
const provider = new CargoTaskProvider(target);
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+
79+
if (customRunner) {
80+
const runnerCommand = `${customRunner}.buildShellExecution`;
81+
try {
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;
87+
} else {
88+
log.debug("Invalid cargo ShellExecution", customExec);
89+
throw "Invalid cargo ShellExecution.";
90+
}
91+
}
92+
// fallback to default processing
93+
94+
} catch (e) {
95+
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
96+
// fallback to default processing
97+
}
98+
}
99+
100+
if (!exec) {
101+
exec = new vscode.ShellExecution(toolchain.cargoPath(), args, definition);
102+
}
103+
104+
return new vscode.Task(
105+
definition,
106+
target,
107+
name,
108+
TASK_SOURCE,
109+
exec,
110+
['$rustc']
111+
);
112+
}
113+
114+
export function activateTaskProvider(target: vscode.WorkspaceFolder, config: Config): vscode.Disposable {
115+
const provider = new CargoTaskProvider(target, config);
85116
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
86117
}

0 commit comments

Comments
 (0)