diff --git a/editors/code/package.json b/editors/code/package.json index 97d92e43c8..35b50e669d 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -353,8 +353,9 @@ "Use [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)" ] }, - "rust-analyzer.debug.sourceFileMap": { - "type": "object", + "rust-analyzer.debug.sourceFileMap": { + "type": ["object", "string"], + "const": "auto", "description": "Optional source file mappings passed to the debug engine.", "default": { "/rustc/": "${env:USERPROFILE}/.rustup/toolchains//lib/rustlib/src/rust" diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 03f7d7cc34..e858f80bcc 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -135,8 +135,12 @@ export class Config { } get debug() { - // "/rustc/" used by suggestions only. - const { ["/rustc/"]: _, ...sourceFileMap } = this.get>("debug.sourceFileMap"); + let sourceFileMap = this.get | "auto">("debug.sourceFileMap"); + if (sourceFileMap !== "auto") { + // "/rustc/" used by suggestions only. + const { ["/rustc/"]: _, ...trimmed } = this.get>("debug.sourceFileMap"); + sourceFileMap = trimmed; + } return { engine: this.get("debug.engine"), diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index 3889a27730..830980f968 100644 --- a/editors/code/src/debug.ts +++ b/editors/code/src/debug.ts @@ -3,7 +3,7 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as ra from './lsp_ext'; -import { Cargo } from './toolchain'; +import { Cargo, getRustcId, getSysroot } from './toolchain'; import { Ctx } from "./ctx"; import { prepareEnv } from "./run"; @@ -104,7 +104,17 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise { + const rustcPath = getPathForExecutable("rustc"); + + // do not memoize the result because the toolchain may change between runs + return execute(`${rustcPath} --print sysroot`, { cwd: dir }); +} + +export async function getRustcId(dir: string): Promise { + const rustcPath = getPathForExecutable("rustc"); + + // do not memoize the result because the toolchain may change between runs + const data = await execute(`${rustcPath} -V -v`, { cwd: dir }); + const rx = /commit-hash:\s(.*)$/m.compile(); + + return rx.exec(data)![1]; +} + /** Mirrors `toolchain::cargo()` implementation */ export function cargoPath(): string { return getPathForExecutable("cargo"); diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 53492a445c..56e0e439e9 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -1,7 +1,7 @@ import * as lc from "vscode-languageclient/node"; import * as vscode from "vscode"; import { strict as nativeAssert } from "assert"; -import { spawnSync } from "child_process"; +import { exec, ExecOptions, spawnSync } from "child_process"; import { inspect } from "util"; export function assert(condition: boolean, explanation: string): asserts condition { @@ -141,3 +141,22 @@ export function memoize(func: (this: TThis, ar return result; }; } + +/** Awaitable wrapper around `child_process.exec` */ +export function execute(command: string, options: ExecOptions): Promise { + return new Promise((resolve, reject) => { + exec(command, options, (err, stdout, stderr) => { + if (err) { + reject(err); + return; + } + + if (stderr) { + reject(new Error(stderr)); + return; + } + + resolve(stdout.trimEnd()); + }); + }); +}