From 1ebfe11730191e914dcf20297cdfdac5b7c297fc Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 22 Apr 2021 16:09:46 +0300 Subject: Add special `auto` value for `debug.sourceFileMap` --- editors/code/package.json | 5 +++-- editors/code/src/config.ts | 8 ++++++-- editors/code/src/debug.ts | 8 ++++---- editors/code/src/toolchain.ts | 23 +++++------------------ editors/code/src/util.ts | 21 ++++++++++++++++++++- 5 files changed, 38 insertions(+), 27 deletions(-) (limited to 'editors') diff --git a/editors/code/package.json b/editors/code/package.json index fa5632f90..5437a0648 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 82f0a0566..603ff930d 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -134,8 +134,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 fe8ec1be4..8c6969dc6 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, sysrootForDir as getSysroot } from './toolchain'; +import { Cargo, getSysroot } from './toolchain'; import { Ctx } from "./ctx"; import { prepareEnv } from "./run"; @@ -105,11 +105,11 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise { - const rustc_path = getPathForExecutable("rustc"); - - return new Promise((resolve, reject) => { - cp.exec(`${rustc_path} --print sysroot`, { cwd: dir }, (err, stdout, stderr) => { - if (err) { - reject(err); - return; - } - - if (stderr) { - reject(new Error(stderr)); - return; - } +export function getSysroot(dir: string): Promise { + const rustcPath = getPathForExecutable("rustc"); - resolve(stdout.trimEnd()); - }); - }); + // do not memoize the result because the toolchain may change between runs + return execute(`${rustcPath} --print sysroot`, { cwd: dir }); } /** Mirrors `toolchain::cargo()` implementation */ diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 53492a445..fc5c9e94e 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()); + }); + }); +} \ No newline at end of file -- cgit v1.2.3