From 8f781e782c7e16aa323672620753ec31526d2b90 Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 22 Apr 2021 15:27:56 +0300 Subject: Autodetect rust library source file map --- editors/code/src/debug.ts | 12 ++++++++++-- editors/code/src/toolchain.ts | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'editors') diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index 3889a2773..fe8ec1be4 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, sysrootForDir as getSysroot } from './toolchain'; import { Ctx } from "./ctx"; import { prepareEnv } from "./run"; @@ -104,7 +104,15 @@ 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; + } + + resolve(stdout.trimEnd()); + }); + }); +} + /** Mirrors `toolchain::cargo()` implementation */ export function cargoPath(): string { return getPathForExecutable("cargo"); -- cgit v1.2.3 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 From 1b4197cb3520e4a71f118aac61a83bab1a6f5931 Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 22 Apr 2021 18:30:44 +0300 Subject: Use explicit rustc commit-hash Required for lldb on mac --- editors/code/src/debug.ts | 6 ++++-- editors/code/src/toolchain.ts | 10 ++++++++++ editors/code/src/util.ts | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) (limited to 'editors') diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index 8c6969dc6..830980f96 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, getSysroot } from './toolchain'; +import { Cargo, getRustcId, getSysroot } from './toolchain'; import { Ctx } from "./ctx"; import { prepareEnv } from "./run"; @@ -107,9 +107,11 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise { 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 fc5c9e94e..56e0e439e 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -159,4 +159,4 @@ export function execute(command: string, options: ExecOptions): Promise resolve(stdout.trimEnd()); }); }); -} \ No newline at end of file +} -- cgit v1.2.3