mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
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 https://github.com/rust-analyzer/rust-analyzer/issues/4902 Co-authored-by: vsrs <vit@conrlab.com>
This commit is contained in:
commit
e07826b199
6 changed files with 103 additions and 82 deletions
|
@ -336,6 +336,14 @@
|
||||||
"default": null,
|
"default": null,
|
||||||
"description": "List of features to activate. Defaults to `rust-analyzer.cargo.features`."
|
"description": "List of features to activate. Defaults to `rust-analyzer.cargo.features`."
|
||||||
},
|
},
|
||||||
|
"rust-analyzer.cargoRunner": {
|
||||||
|
"type": [
|
||||||
|
"null",
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"default": null,
|
||||||
|
"description": "Custom cargo runner extension ID."
|
||||||
|
},
|
||||||
"rust-analyzer.inlayHints.enable": {
|
"rust-analyzer.inlayHints.enable": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": true,
|
"default": true,
|
||||||
|
|
|
@ -394,7 +394,7 @@ export function run(ctx: Ctx): Cmd {
|
||||||
|
|
||||||
item.detail = 'rerun';
|
item.detail = 'rerun';
|
||||||
prevRunnable = item;
|
prevRunnable = item;
|
||||||
const task = createTask(item.runnable);
|
const task = await createTask(item.runnable, ctx.config);
|
||||||
return await vscode.tasks.executeTask(task);
|
return await vscode.tasks.executeTask(task);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -404,7 +404,7 @@ export function runSingle(ctx: Ctx): Cmd {
|
||||||
const editor = ctx.activeRustEditor;
|
const editor = ctx.activeRustEditor;
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|
||||||
const task = createTask(runnable);
|
const task = await createTask(runnable, ctx.config);
|
||||||
task.group = vscode.TaskGroup.Build;
|
task.group = vscode.TaskGroup.Build;
|
||||||
task.presentationOptions = {
|
task.presentationOptions = {
|
||||||
reveal: vscode.TaskRevealKind.Always,
|
reveal: vscode.TaskRevealKind.Always,
|
||||||
|
|
|
@ -110,6 +110,10 @@ export class Config {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get cargoRunner() {
|
||||||
|
return this.get<string | undefined>("cargoRunner");
|
||||||
|
}
|
||||||
|
|
||||||
get debug() {
|
get debug() {
|
||||||
// "/rustc/<id>" used by suggestions only.
|
// "/rustc/<id>" used by suggestions only.
|
||||||
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
|
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
|
||||||
|
|
|
@ -114,7 +114,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('applyActionGroup', commands.applyActionGroup);
|
ctx.registerCommand('applyActionGroup', commands.applyActionGroup);
|
||||||
ctx.registerCommand('gotoLocation', commands.gotoLocation);
|
ctx.registerCommand('gotoLocation', commands.gotoLocation);
|
||||||
|
|
||||||
ctx.pushCleanup(activateTaskProvider(workspaceFolder));
|
ctx.pushCleanup(activateTaskProvider(workspaceFolder, ctx.config));
|
||||||
|
|
||||||
activateInlayHints(ctx);
|
activateInlayHints(ctx);
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
import * as ra from './lsp_ext';
|
import * as ra from './lsp_ext';
|
||||||
import * as toolchain from "./toolchain";
|
import * as tasks from './tasks';
|
||||||
|
|
||||||
import { Ctx } from './ctx';
|
import { Ctx } from './ctx';
|
||||||
import { makeDebugConfig } from './debug';
|
import { makeDebugConfig } from './debug';
|
||||||
|
import { Config } from './config';
|
||||||
|
|
||||||
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
|
const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }];
|
||||||
|
|
||||||
|
@ -95,52 +96,29 @@ export class RunnableQuickPick implements vscode.QuickPickItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CargoTaskDefinition extends vscode.TaskDefinition {
|
export async function createTask(runnable: ra.Runnable, config: Config): Promise<vscode.Task> {
|
||||||
type: 'cargo';
|
if (runnable.kind !== "cargo") {
|
||||||
label: string;
|
// rust-analyzer supports only one kind, "cargo"
|
||||||
command: string;
|
// do not use tasks.TASK_TYPE here, these are completely different meanings.
|
||||||
args: string[];
|
|
||||||
env?: { [key: string]: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createTask(runnable: ra.Runnable): vscode.Task {
|
throw `Unexpected runnable kind: ${runnable.kind}`;
|
||||||
const TASK_SOURCE = 'Rust';
|
|
||||||
|
|
||||||
let command;
|
|
||||||
switch (runnable.kind) {
|
|
||||||
case "cargo": command = toolchain.getPathForExecutable("cargo");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = [...runnable.args.cargoArgs]; // should be a copy!
|
const args = [...runnable.args.cargoArgs]; // should be a copy!
|
||||||
if (runnable.args.executableArgs.length > 0) {
|
if (runnable.args.executableArgs.length > 0) {
|
||||||
args.push('--', ...runnable.args.executableArgs);
|
args.push('--', ...runnable.args.executableArgs);
|
||||||
}
|
}
|
||||||
const definition: CargoTaskDefinition = {
|
const definition: tasks.CargoTaskDefinition = {
|
||||||
type: 'cargo',
|
type: tasks.TASK_TYPE,
|
||||||
label: runnable.label,
|
command: args[0], // run, test, etc...
|
||||||
command,
|
args: args.slice(1),
|
||||||
args,
|
cwd: runnable.args.workspaceRoot,
|
||||||
env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }),
|
env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }),
|
||||||
};
|
};
|
||||||
|
|
||||||
const execOption: vscode.ShellExecutionOptions = {
|
const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
|
||||||
cwd: runnable.args.workspaceRoot || '.',
|
const cargoTask = await tasks.buildCargoTask(target, definition, runnable.label, args, config.cargoRunner, true);
|
||||||
env: definition.env,
|
cargoTask.presentationOptions.clear = true;
|
||||||
};
|
|
||||||
const exec = new vscode.ShellExecution(
|
|
||||||
definition.command,
|
|
||||||
definition.args,
|
|
||||||
execOption,
|
|
||||||
);
|
|
||||||
|
|
||||||
const f = vscode.workspace.workspaceFolders![0];
|
return cargoTask;
|
||||||
const t = new vscode.Task(
|
|
||||||
definition,
|
|
||||||
f,
|
|
||||||
definition.label,
|
|
||||||
TASK_SOURCE,
|
|
||||||
exec,
|
|
||||||
['$rustc'],
|
|
||||||
);
|
|
||||||
t.presentationOptions.clear = true;
|
|
||||||
return t;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as toolchain from "./toolchain";
|
import * as toolchain from "./toolchain";
|
||||||
|
import { Config } from './config';
|
||||||
|
import { log } from './util';
|
||||||
|
|
||||||
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
|
// 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.
|
// our configuration should be compatible with it so use the same key.
|
||||||
const TASK_TYPE = 'cargo';
|
export const TASK_TYPE = 'cargo';
|
||||||
|
export const TASK_SOURCE = 'rust';
|
||||||
|
|
||||||
interface CargoTaskDefinition extends vscode.TaskDefinition {
|
export interface CargoTaskDefinition extends vscode.TaskDefinition {
|
||||||
command?: string;
|
command?: string;
|
||||||
args?: string[];
|
args?: string[];
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
|
@ -14,73 +17,101 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
|
||||||
|
|
||||||
class CargoTaskProvider implements vscode.TaskProvider {
|
class CargoTaskProvider implements vscode.TaskProvider {
|
||||||
private readonly target: vscode.WorkspaceFolder;
|
private readonly target: vscode.WorkspaceFolder;
|
||||||
|
private readonly config: Config;
|
||||||
|
|
||||||
constructor(target: vscode.WorkspaceFolder) {
|
constructor(target: vscode.WorkspaceFolder, config: Config) {
|
||||||
this.target = target;
|
this.target = target;
|
||||||
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
provideTasks(): vscode.Task[] {
|
async provideTasks(): Promise<vscode.Task[]> {
|
||||||
// Detect Rust tasks. Currently we do not do any actual detection
|
// Detect Rust tasks. Currently we do not do any actual detection
|
||||||
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
|
// 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
|
// set of tasks that always exist. These tasks cannot be removed in
|
||||||
// tasks.json - only tweaked.
|
// tasks.json - only tweaked.
|
||||||
|
|
||||||
const cargoPath = toolchain.cargoPath();
|
const defs = [
|
||||||
|
|
||||||
return [
|
|
||||||
{ command: 'build', group: vscode.TaskGroup.Build },
|
{ command: 'build', group: vscode.TaskGroup.Build },
|
||||||
{ command: 'check', group: vscode.TaskGroup.Build },
|
{ command: 'check', group: vscode.TaskGroup.Build },
|
||||||
{ command: 'test', group: vscode.TaskGroup.Test },
|
{ command: 'test', group: vscode.TaskGroup.Test },
|
||||||
{ command: 'clean', group: vscode.TaskGroup.Clean },
|
{ command: 'clean', group: vscode.TaskGroup.Clean },
|
||||||
{ command: 'run', group: undefined },
|
{ command: 'run', group: undefined },
|
||||||
]
|
];
|
||||||
.map(({ command, group }) => {
|
|
||||||
const vscodeTask = new vscode.Task(
|
const tasks: vscode.Task[] = [];
|
||||||
// The contents of this object end up in the tasks.json entries.
|
for (const def of defs) {
|
||||||
{
|
const vscodeTask = await buildCargoTask(this.target, { type: TASK_TYPE, command: def.command }, `cargo ${def.command}`, [def.command], this.config.cargoRunner);
|
||||||
type: TASK_TYPE,
|
vscodeTask.group = def.group;
|
||||||
command,
|
tasks.push(vscodeTask);
|
||||||
},
|
}
|
||||||
// The scope of the task - workspace or specific folder (global
|
|
||||||
// is not supported).
|
return tasks;
|
||||||
this.target,
|
|
||||||
// The task name, and task source. These are shown in the UI as
|
|
||||||
// `${source}: ${name}`, e.g. `rust: cargo build`.
|
|
||||||
`cargo ${command}`,
|
|
||||||
'rust',
|
|
||||||
// What to do when this command is executed.
|
|
||||||
new vscode.ShellExecution(cargoPath, [command]),
|
|
||||||
// Problem matchers.
|
|
||||||
['$rustc'],
|
|
||||||
);
|
|
||||||
vscodeTask.group = group;
|
|
||||||
return vscodeTask;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveTask(task: vscode.Task): vscode.Task | undefined {
|
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
|
||||||
// VSCode calls this for every cargo task in the user's tasks.json,
|
// 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
|
// we need to inform VSCode how to execute that command by creating
|
||||||
// a ShellExecution for it.
|
// a ShellExecution for it.
|
||||||
|
|
||||||
const definition = task.definition as CargoTaskDefinition;
|
const definition = task.definition as CargoTaskDefinition;
|
||||||
|
|
||||||
if (definition.type === 'cargo' && definition.command) {
|
if (definition.type === TASK_TYPE && definition.command) {
|
||||||
const args = [definition.command].concat(definition.args ?? []);
|
const args = [definition.command].concat(definition.args ?? []);
|
||||||
|
|
||||||
return new vscode.Task(
|
return await buildCargoTask(this.target, definition, task.name, args, this.config.cargoRunner);
|
||||||
definition,
|
|
||||||
task.name,
|
|
||||||
'rust',
|
|
||||||
new vscode.ShellExecution('cargo', args, definition),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
|
export async function buildCargoTask(
|
||||||
const provider = new CargoTaskProvider(target);
|
target: vscode.WorkspaceFolder,
|
||||||
|
definition: CargoTaskDefinition,
|
||||||
|
name: string,
|
||||||
|
args: string[],
|
||||||
|
customRunner?: string,
|
||||||
|
throwOnError: boolean = false
|
||||||
|
): Promise<vscode.Task> {
|
||||||
|
|
||||||
|
let exec: vscode.ShellExecution | undefined = undefined;
|
||||||
|
|
||||||
|
if (customRunner) {
|
||||||
|
const runnerCommand = `${customRunner}.buildShellExecution`;
|
||||||
|
try {
|
||||||
|
const runnerArgs = { kind: TASK_TYPE, args, cwd: definition.cwd, env: definition.env };
|
||||||
|
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
||||||
|
if (customExec) {
|
||||||
|
if (customExec instanceof vscode.ShellExecution) {
|
||||||
|
exec = customExec;
|
||||||
|
} else {
|
||||||
|
log.debug("Invalid cargo ShellExecution", customExec);
|
||||||
|
throw "Invalid cargo ShellExecution.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// fallback to default processing
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
|
||||||
|
// fallback to default processing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exec) {
|
||||||
|
exec = new vscode.ShellExecution(toolchain.cargoPath(), args, definition);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new vscode.Task(
|
||||||
|
definition,
|
||||||
|
target,
|
||||||
|
name,
|
||||||
|
TASK_SOURCE,
|
||||||
|
exec,
|
||||||
|
['$rustc']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function activateTaskProvider(target: vscode.WorkspaceFolder, config: Config): vscode.Disposable {
|
||||||
|
const provider = new CargoTaskProvider(target, config);
|
||||||
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue