Skip to content

Commit 1ebfe11

Browse files
committed
Add special auto value for debug.sourceFileMap
1 parent 8f781e7 commit 1ebfe11

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

editors/code/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,9 @@
353353
"Use [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)"
354354
]
355355
},
356-
"rust-analyzer.debug.sourceFileMap": {
357-
"type": "object",
356+
"rust-analyzer.debug.sourceFileMap": {
357+
"type": ["object", "string"],
358+
"const": "auto",
358359
"description": "Optional source file mappings passed to the debug engine.",
359360
"default": {
360361
"/rustc/<id>": "${env:USERPROFILE}/.rustup/toolchains/<toolchain-id>/lib/rustlib/src/rust"

editors/code/src/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ export class Config {
134134
}
135135

136136
get debug() {
137-
// "/rustc/<id>" used by suggestions only.
138-
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
137+
let sourceFileMap = this.get<Record<string, string> | "auto">("debug.sourceFileMap");
138+
if (sourceFileMap !== "auto") {
139+
// "/rustc/<id>" used by suggestions only.
140+
const { ["/rustc/<id>"]: _, ...trimmed } = this.get<Record<string, string>>("debug.sourceFileMap");
141+
sourceFileMap = trimmed;
142+
}
139143

140144
return {
141145
engine: this.get<string>("debug.engine"),

editors/code/src/debug.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from 'vscode';
33
import * as path from 'path';
44
import * as ra from './lsp_ext';
55

6-
import { Cargo, sysrootForDir as getSysroot } from './toolchain';
6+
import { Cargo, getSysroot } from './toolchain';
77
import { Ctx } from "./ctx";
88
import { prepareEnv } from "./run";
99

@@ -105,11 +105,11 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
105105
const executable = await getDebugExecutable(runnable);
106106
const env = prepareEnv(runnable, ctx.config.runnableEnv);
107107
let sourceFileMap = debugOptions.sourceFileMap;
108-
if ( !sourceFileMap || Object.keys(sourceFileMap).length === 0 ) {
108+
if (sourceFileMap === "auto") {
109109
// let's try to use the default toolchain
110110
const sysroot = await getSysroot(wsFolder);
111-
const rustlib_src = path.normalize(sysroot + "/lib/rustlib/src/rust");
112-
sourceFileMap = { "/rustc/*": rustlib_src };
111+
const rustlib = path.normalize(sysroot + "/lib/rustlib/src/rust");
112+
sourceFileMap = { "/rustc/*": rustlib };
113113
}
114114

115115
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, sourceFileMap);

editors/code/src/toolchain.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import * as fs from 'fs';
55
import * as readline from 'readline';
66
import { OutputChannel } from 'vscode';
7-
import { log, memoize } from './util';
7+
import { execute, log, memoize } from './util';
88

99
interface CompilationArtifact {
1010
fileName: string;
@@ -122,24 +122,11 @@ export class Cargo {
122122
}
123123

124124
/** Mirrors `project_model::sysroot::discover_sysroot_dir()` implementation*/
125-
export function sysrootForDir(dir: string): Promise<string> {
126-
const rustc_path = getPathForExecutable("rustc");
127-
128-
return new Promise((resolve, reject) => {
129-
cp.exec(`${rustc_path} --print sysroot`, { cwd: dir }, (err, stdout, stderr) => {
130-
if (err) {
131-
reject(err);
132-
return;
133-
}
134-
135-
if (stderr) {
136-
reject(new Error(stderr));
137-
return;
138-
}
125+
export function getSysroot(dir: string): Promise<string> {
126+
const rustcPath = getPathForExecutable("rustc");
139127

140-
resolve(stdout.trimEnd());
141-
});
142-
});
128+
// do not memoize the result because the toolchain may change between runs
129+
return execute(`${rustcPath} --print sysroot`, { cwd: dir });
143130
}
144131

145132
/** Mirrors `toolchain::cargo()` implementation */

editors/code/src/util.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as lc from "vscode-languageclient/node";
22
import * as vscode from "vscode";
33
import { strict as nativeAssert } from "assert";
4-
import { spawnSync } from "child_process";
4+
import { exec, ExecOptions, spawnSync } from "child_process";
55
import { inspect } from "util";
66

77
export function assert(condition: boolean, explanation: string): asserts condition {
@@ -141,3 +141,22 @@ export function memoize<Ret, TThis, Param extends string>(func: (this: TThis, ar
141141
return result;
142142
};
143143
}
144+
145+
/** Awaitable wrapper around `child_process.exec` */
146+
export function execute(command: string, options: ExecOptions): Promise<string> {
147+
return new Promise((resolve, reject) => {
148+
exec(command, options, (err, stdout, stderr) => {
149+
if (err) {
150+
reject(err);
151+
return;
152+
}
153+
154+
if (stderr) {
155+
reject(new Error(stderr));
156+
return;
157+
}
158+
159+
resolve(stdout.trimEnd());
160+
});
161+
});
162+
}

0 commit comments

Comments
 (0)