Skip to content

Commit 5bbfea0

Browse files
committed
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!
1 parent 61504c8 commit 5bbfea0

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

editors/code/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,15 @@
13011301
"endsPattern": "^\\[Finished running\\b"
13021302
},
13031303
"pattern": "$rustc"
1304+
},
1305+
{
1306+
"name": "rustc-run",
1307+
"base": "$rustc",
1308+
"fileLocation": [
1309+
"autoDetect",
1310+
"${command:rust-analyzer.cargoWorkspaceRootForCurrentRun}"
1311+
],
1312+
"pattern": "$rustc-run"
13041313
}
13051314
],
13061315
"colors": [

editors/code/src/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ export function run(ctx: Ctx): Cmd {
842842
item.detail = "rerun";
843843
prevRunnable = item;
844844
const task = await createTask(item.runnable, ctx.config);
845+
ctx.cargoWorkspaceRootForCurrentRun = item.cargoWorkspaceRoot;
845846
return await vscode.tasks.executeTask(task);
846847
};
847848
}
@@ -946,3 +947,6 @@ export function linkToCommand(ctx: Ctx): Cmd {
946947
}
947948
};
948949
}
950+
export function getCargoWorkspaceDir(ctx: Ctx): Cmd {
951+
return async () => ctx.cargoWorkspaceRootForCurrentRun;
952+
}

editors/code/src/ctx.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export type Workspace =
1717
};
1818

1919
export class Ctx {
20+
// Helps VS Code to correctly link problems from runnables. This is used by
21+
// `rust-analyzer.cargoWorkspaceRootForCurrentRun` command of $rustc-run problem matcher.
22+
cargoWorkspaceRootForCurrentRun?: string = undefined;
23+
2024
private constructor(
2125
readonly config: Config,
2226
private readonly extCtx: vscode.ExtensionContext,

editors/code/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) {
189189
ctx.registerCommand("resolveCodeAction", commands.resolveCodeAction);
190190
ctx.registerCommand("applyActionGroup", commands.applyActionGroup);
191191
ctx.registerCommand("gotoLocation", commands.gotoLocation);
192+
ctx.registerCommand("cargoWorkspaceRootForCurrentRun", commands.getCargoWorkspaceDir);
192193

193194
ctx.registerCommand("linkToCommand", commands.linkToCommand);
194195
}

editors/code/src/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ export async function selectRunnable(
8989

9090
export class RunnableQuickPick implements vscode.QuickPickItem {
9191
public label: string;
92+
public cargoWorkspaceRoot?: string;
9293
public description?: string | undefined;
9394
public detail?: string | undefined;
9495
public picked?: boolean | undefined;
9596

9697
constructor(public runnable: ra.Runnable) {
9798
this.label = runnable.label;
99+
this.cargoWorkspaceRoot = runnable.args.workspaceRoot;
98100
}
99101
}
100102

editors/code/src/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export async function buildCargoTask(
128128
name,
129129
TASK_SOURCE,
130130
exec,
131-
["$rustc"]
131+
["$rustc-run"]
132132
);
133133
}
134134

0 commit comments

Comments
 (0)