Skip to content

Commit 8f781e7

Browse files
committed
Autodetect rust library source file map
1 parent d1c9bd1 commit 8f781e7

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

editors/code/src/debug.ts

Lines changed: 10 additions & 2 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 } from './toolchain';
6+
import { Cargo, sysrootForDir as getSysroot } from './toolchain';
77
import { Ctx } from "./ctx";
88
import { prepareEnv } from "./run";
99

@@ -104,7 +104,15 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
104104

105105
const executable = await getDebugExecutable(runnable);
106106
const env = prepareEnv(runnable, ctx.config.runnableEnv);
107-
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, debugOptions.sourceFileMap);
107+
let sourceFileMap = debugOptions.sourceFileMap;
108+
if ( !sourceFileMap || Object.keys(sourceFileMap).length === 0 ) {
109+
// let's try to use the default toolchain
110+
const sysroot = await getSysroot(wsFolder);
111+
const rustlib_src = path.normalize(sysroot + "/lib/rustlib/src/rust");
112+
sourceFileMap = { "/rustc/*": rustlib_src };
113+
}
114+
115+
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, sourceFileMap);
108116
if (debugConfig.type in debugOptions.engineSettings) {
109117
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
110118
for (var key in settingsMap) {

editors/code/src/toolchain.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ export class Cargo {
121121
}
122122
}
123123

124+
/** 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+
}
139+
140+
resolve(stdout.trimEnd());
141+
});
142+
});
143+
}
144+
124145
/** Mirrors `toolchain::cargo()` implementation */
125146
export function cargoPath(): string {
126147
return getPathForExecutable("cargo");

0 commit comments

Comments
 (0)