From 5bbfea03ccada14bbaca6df6c0ef3760ac44a9a5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 Oct 2022 23:18:11 +0100 Subject: [PATCH] fix: in VSCode, correctly resolve relative paths to errors VS Code problem matcher are restricted to be static "regexes". You can't create a problem matcher dynamically, and you can't use custom code in lieu of problem matcher. This creates a problem for rust/cargo compiler errors. They use paths relative to the root of the Cargo workspace, but VS Code doesn't necessary know where that root is. Luckily, there's a way out: our current problem matcher is defined like this: "fileLocation": [ "autoDetect", "${workspaceRoot}" ], That means that relative pahts would be resoleved relative to workspace root. VS Code allows to specify a command inside `${}`. So we can plug custom logic there to fetch Cargo's workspace root! And that's exactly what this PR is doing! --- editors/code/package.json | 9 +++++++++ editors/code/src/commands.ts | 4 ++++ editors/code/src/ctx.ts | 4 ++++ editors/code/src/main.ts | 1 + editors/code/src/run.ts | 2 ++ editors/code/src/tasks.ts | 2 +- 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/editors/code/package.json b/editors/code/package.json index f1dd3aa79ff..ccf62d002bb 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1301,6 +1301,15 @@ "endsPattern": "^\\[Finished running\\b" }, "pattern": "$rustc" + }, + { + "name": "rustc-run", + "base": "$rustc", + "fileLocation": [ + "autoDetect", + "${command:rust-analyzer.cargoWorkspaceRootForCurrentRun}" + ], + "pattern": "$rustc-run" } ], "colors": [ diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index b9ad525e361..6b10073aa86 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -842,6 +842,7 @@ export function run(ctx: Ctx): Cmd { item.detail = "rerun"; prevRunnable = item; const task = await createTask(item.runnable, ctx.config); + ctx.cargoWorkspaceRootForCurrentRun = item.cargoWorkspaceRoot; return await vscode.tasks.executeTask(task); }; } @@ -946,3 +947,6 @@ export function linkToCommand(ctx: Ctx): Cmd { } }; } +export function getCargoWorkspaceDir(ctx: Ctx): Cmd { + return async () => ctx.cargoWorkspaceRootForCurrentRun; +} diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 26510011d43..b6c0eedfb16 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -17,6 +17,10 @@ export type Workspace = }; export class Ctx { + // Helps VS Code to correctly link problems from runnables. This is used by + // `rust-analyzer.cargoWorkspaceRootForCurrentRun` command of $rustc-run problem matcher. + cargoWorkspaceRootForCurrentRun?: string = undefined; + private constructor( readonly config: Config, private readonly extCtx: vscode.ExtensionContext, diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 41bde4195e0..6e6c2513dd9 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -189,6 +189,7 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) { ctx.registerCommand("resolveCodeAction", commands.resolveCodeAction); ctx.registerCommand("applyActionGroup", commands.applyActionGroup); ctx.registerCommand("gotoLocation", commands.gotoLocation); + ctx.registerCommand("cargoWorkspaceRootForCurrentRun", commands.getCargoWorkspaceDir); ctx.registerCommand("linkToCommand", commands.linkToCommand); } diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts index 22e5eda6827..100c0fe2d8c 100644 --- a/editors/code/src/run.ts +++ b/editors/code/src/run.ts @@ -89,12 +89,14 @@ export async function selectRunnable( export class RunnableQuickPick implements vscode.QuickPickItem { public label: string; + public cargoWorkspaceRoot?: string; public description?: string | undefined; public detail?: string | undefined; public picked?: boolean | undefined; constructor(public runnable: ra.Runnable) { this.label = runnable.label; + this.cargoWorkspaceRoot = runnable.args.workspaceRoot; } } diff --git a/editors/code/src/tasks.ts b/editors/code/src/tasks.ts index e6239deeb21..44697f95bab 100644 --- a/editors/code/src/tasks.ts +++ b/editors/code/src/tasks.ts @@ -128,7 +128,7 @@ export async function buildCargoTask( name, TASK_SOURCE, exec, - ["$rustc"] + ["$rustc-run"] ); }