about summary refs log tree commit diff
path: root/editors/code
diff options
context:
space:
mode:
authorvsrs <vit@conrlab.com>2021-04-22 16:09:46 +0300
committervsrs <vit@conrlab.com>2021-04-22 16:09:46 +0300
commit1ebfe11730191e914dcf20297cdfdac5b7c297fc (patch)
treeae6caf9e8fd50e3eac826d0a3f0698c6e047544f /editors/code
parent8f781e782c7e16aa323672620753ec31526d2b90 (diff)
downloadrust-1ebfe11730191e914dcf20297cdfdac5b7c297fc.tar.gz
rust-1ebfe11730191e914dcf20297cdfdac5b7c297fc.zip
Add special `auto` value for `debug.sourceFileMap`
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/package.json5
-rw-r--r--editors/code/src/config.ts8
-rw-r--r--editors/code/src/debug.ts8
-rw-r--r--editors/code/src/toolchain.ts23
-rw-r--r--editors/code/src/util.ts21
5 files changed, 38 insertions, 27 deletions
diff --git a/editors/code/package.json b/editors/code/package.json
index fa5632f9007..5437a064869 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/<id>": "${env:USERPROFILE}/.rustup/toolchains/<toolchain-id>/lib/rustlib/src/rust"
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 82f0a0566a6..603ff930d1e 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -134,8 +134,12 @@ export class Config {
     }
 
     get debug() {
-        // "/rustc/<id>" used by suggestions only.
-        const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
+        let sourceFileMap = this.get<Record<string, string> | "auto">("debug.sourceFileMap");
+        if (sourceFileMap !== "auto") {
+            // "/rustc/<id>" used by suggestions only.
+            const { ["/rustc/<id>"]: _, ...trimmed } = this.get<Record<string, string>>("debug.sourceFileMap");
+            sourceFileMap = trimmed;
+        }
 
         return {
             engine: this.get<string>("debug.engine"),
diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts
index fe8ec1be454..8c6969dc670 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<v
     const executable = await getDebugExecutable(runnable);
     const env = prepareEnv(runnable, ctx.config.runnableEnv);
     let sourceFileMap = debugOptions.sourceFileMap;
-    if ( !sourceFileMap || Object.keys(sourceFileMap).length === 0 ) {
+    if (sourceFileMap === "auto") {
         // let's try to use the default toolchain
         const sysroot = await getSysroot(wsFolder);
-        const rustlib_src = path.normalize(sysroot + "/lib/rustlib/src/rust");
-        sourceFileMap = { "/rustc/*": rustlib_src };
+        const rustlib = path.normalize(sysroot + "/lib/rustlib/src/rust");
+        sourceFileMap = { "/rustc/*": rustlib };
     }
 
     const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, sourceFileMap);
diff --git a/editors/code/src/toolchain.ts b/editors/code/src/toolchain.ts
index b746da1d92c..5725bcafe2f 100644
--- a/editors/code/src/toolchain.ts
+++ b/editors/code/src/toolchain.ts
@@ -4,7 +4,7 @@ import * as path from 'path';
 import * as fs from 'fs';
 import * as readline from 'readline';
 import { OutputChannel } from 'vscode';
-import { log, memoize } from './util';
+import { execute, log, memoize } from './util';
 
 interface CompilationArtifact {
     fileName: string;
@@ -122,24 +122,11 @@ export class Cargo {
 }
 
 /** Mirrors `project_model::sysroot::discover_sysroot_dir()` implementation*/
-export function sysrootForDir(dir: string): Promise<string> {
-    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<string> {
+    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 53492a445c4..fc5c9e94e5e 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<Ret, TThis, Param extends string>(func: (this: TThis, ar
         return result;
     };
 }
+
+/** Awaitable wrapper around `child_process.exec` */
+export function execute(command: string, options: ExecOptions): Promise<string> {
+    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