about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/editors/code
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-07-16 11:49:27 +0000
committerbors <bors@rust-lang.org>2024-07-16 11:49:27 +0000
commite5b1a2b3cd1e3a53b939e81a3eef1f4da924eb0f (patch)
tree32777f5902970e2a095804a0bdc062150b15c345 /src/tools/rust-analyzer/editors/code
parent064e6ed163b4b695bd669983b57ad74949e15026 (diff)
parent4f031d976812aba0105300b8f77f73835d97c530 (diff)
Auto merge of #17605 - Veykril:runnable-sysroot, r=Veykril
Set `RUSTC_TOOLCHAIN` for runnables

With this the client doesn't necessarily need to guess the sysroot anymore
Diffstat (limited to 'src/tools/rust-analyzer/editors/code')
-rw-r--r--src/tools/rust-analyzer/editors/code/src/debug.ts31
-rw-r--r--src/tools/rust-analyzer/editors/code/src/run.ts7
-rw-r--r--src/tools/rust-analyzer/editors/code/src/tasks.ts2
-rw-r--r--src/tools/rust-analyzer/editors/code/src/toolchain.ts37
4 files changed, 37 insertions, 40 deletions
diff --git a/src/tools/rust-analyzer/editors/code/src/debug.ts b/src/tools/rust-analyzer/editors/code/src/debug.ts
index f23e3680933..d9622b4a0d2 100644
--- a/src/tools/rust-analyzer/editors/code/src/debug.ts
+++ b/src/tools/rust-analyzer/editors/code/src/debug.ts
@@ -3,10 +3,10 @@ import * as vscode from "vscode";
 import * as path from "path";
 import type * as ra from "./lsp_ext";
 
-import { Cargo, getRustcId, getSysroot } from "./toolchain";
+import { Cargo } from "./toolchain";
 import type { Ctx } from "./ctx";
 import { prepareEnv } from "./run";
-import { isCargoRunnableArgs, unwrapUndefinable } from "./util";
+import { execute, isCargoRunnableArgs, unwrapUndefinable } from "./util";
 
 const debugOutput = vscode.window.createOutputChannel("Debug");
 type DebugConfigProvider = (
@@ -142,18 +142,29 @@ async function getDebugConfiguration(
     const executable = await getDebugExecutable(runnableArgs, env);
     let sourceFileMap = debugOptions.sourceFileMap;
     if (sourceFileMap === "auto") {
-        // let's try to use the default toolchain
-        const [commitHash, sysroot] = await Promise.all([
-            getRustcId(wsFolder),
-            getSysroot(wsFolder),
-        ]);
-        const rustlib = path.normalize(sysroot + "/lib/rustlib/src/rust");
         sourceFileMap = {};
-        sourceFileMap[`/rustc/${commitHash}/`] = rustlib;
+        const sysroot = env["RUSTC_TOOLCHAIN"];
+        if (sysroot) {
+            // let's try to use the default toolchain
+            const data = await execute(`rustc -V -v`, { cwd: wsFolder, env });
+            const rx = /commit-hash:\s(.*)$/m;
+
+            const commitHash = rx.exec(data)?.[1];
+            if (commitHash) {
+                const rustlib = path.normalize(sysroot + "/lib/rustlib/src/rust");
+                sourceFileMap[`/rustc/${commitHash}/`] = rustlib;
+            }
+        }
     }
 
     const provider = unwrapUndefinable(knownEngines[debugEngine.id]);
-    const debugConfig = provider(runnable, runnableArgs, simplifyPath(executable), env);
+    const debugConfig = provider(
+        runnable,
+        runnableArgs,
+        simplifyPath(executable),
+        env,
+        sourceFileMap,
+    );
     if (debugConfig.type in debugOptions.engineSettings) {
         const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
         for (var key in settingsMap) {
diff --git a/src/tools/rust-analyzer/editors/code/src/run.ts b/src/tools/rust-analyzer/editors/code/src/run.ts
index 783bbc1607d..7179eb37447 100644
--- a/src/tools/rust-analyzer/editors/code/src/run.ts
+++ b/src/tools/rust-analyzer/editors/code/src/run.ts
@@ -8,7 +8,6 @@ import { makeDebugConfig } from "./debug";
 import type { Config, RunnableEnvCfg, RunnableEnvCfgItem } from "./config";
 import type { LanguageClient } from "vscode-languageclient/node";
 import { unwrapUndefinable, type RustEditor } from "./util";
-import * as toolchain from "./toolchain";
 
 const quickPickButtons = [
     { iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configuration." },
@@ -115,7 +114,7 @@ export async function createTaskFromRunnable(
 
     let definition: tasks.TaskDefinition;
     let options;
-    let cargo;
+    let cargo = "cargo";
     if (runnable.kind === "cargo") {
         const runnableArgs = runnable.args;
         let args = createCargoArgs(runnableArgs);
@@ -126,8 +125,6 @@ export async function createTaskFromRunnable(
 
             cargo = unwrapUndefinable(cargoParts[0]);
             args = [...cargoParts.slice(1), ...args];
-        } else {
-            cargo = await toolchain.cargoPath();
         }
 
         definition = {
@@ -200,7 +197,7 @@ async function getRunnables(
             continue;
         }
 
-        if (debuggeeOnly && (r.label.startsWith("doctest") || r.label.startsWith("cargo"))) {
+        if (debuggeeOnly && r.label.startsWith("doctest")) {
             continue;
         }
         items.push(new RunnableQuickPick(r));
diff --git a/src/tools/rust-analyzer/editors/code/src/tasks.ts b/src/tools/rust-analyzer/editors/code/src/tasks.ts
index fac1cc6394f..730ec6d1e90 100644
--- a/src/tools/rust-analyzer/editors/code/src/tasks.ts
+++ b/src/tools/rust-analyzer/editors/code/src/tasks.ts
@@ -125,7 +125,7 @@ export async function targetToExecution(
     let command, args;
     if (isCargoTask(definition)) {
         // FIXME: The server should provide cargo
-        command = cargo || (await toolchain.cargoPath());
+        command = cargo || (await toolchain.cargoPath(options?.env));
         args = [definition.command].concat(definition.args || []);
     } else {
         command = definition.command;
diff --git a/src/tools/rust-analyzer/editors/code/src/toolchain.ts b/src/tools/rust-analyzer/editors/code/src/toolchain.ts
index 6a0b5c26d82..850a6a55616 100644
--- a/src/tools/rust-analyzer/editors/code/src/toolchain.ts
+++ b/src/tools/rust-analyzer/editors/code/src/toolchain.ts
@@ -3,7 +3,7 @@ import * as os from "os";
 import * as path from "path";
 import * as readline from "readline";
 import * as vscode from "vscode";
-import { execute, log, memoizeAsync, unwrapNullable, unwrapUndefinable } from "./util";
+import { log, memoizeAsync, unwrapUndefinable } from "./util";
 import type { CargoRunnableArgs } from "./lsp_ext";
 
 interface CompilationArtifact {
@@ -55,7 +55,10 @@ export class Cargo {
         return result;
     }
 
-    private async getArtifacts(spec: ArtifactSpec): Promise<CompilationArtifact[]> {
+    private async getArtifacts(
+        spec: ArtifactSpec,
+        env?: Record<string, string>,
+    ): Promise<CompilationArtifact[]> {
         const artifacts: CompilationArtifact[] = [];
 
         try {
@@ -78,6 +81,7 @@ export class Cargo {
                     }
                 },
                 (stderr) => this.output.append(stderr),
+                env,
             );
         } catch (err) {
             this.output.show(true);
@@ -90,6 +94,7 @@ export class Cargo {
     async executableFromArgs(runnableArgs: CargoRunnableArgs): Promise<string> {
         const artifacts = await this.getArtifacts(
             Cargo.artifactSpec(runnableArgs.cargoArgs, runnableArgs.executableArgs),
+            runnableArgs.environment,
         );
 
         if (artifacts.length === 0) {
@@ -106,8 +111,9 @@ export class Cargo {
         cargoArgs: string[],
         onStdoutJson: (obj: any) => void,
         onStderrString: (data: string) => void,
+        env?: Record<string, string>,
     ): Promise<number> {
-        const path = await cargoPath();
+        const path = await cargoPath(env);
         return await new Promise((resolve, reject) => {
             const cargo = cp.spawn(path, cargoArgs, {
                 stdio: ["ignore", "pipe", "pipe"],
@@ -133,29 +139,12 @@ export class Cargo {
     }
 }
 
-/** Mirrors `project_model::sysroot::discover_sysroot_dir()` implementation*/
-export async function getSysroot(dir: string): Promise<string> {
-    const rustcPath = await getPathForExecutable("rustc");
-
-    // do not memoize the result because the toolchain may change between runs
-    return await execute(`${rustcPath} --print sysroot`, { cwd: dir });
-}
-
-export async function getRustcId(dir: string): Promise<string> {
-    const rustcPath = await 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;
-
-    const result = unwrapNullable(rx.exec(data));
-    const first = unwrapUndefinable(result[1]);
-    return first;
-}
-
 /** Mirrors `toolchain::cargo()` implementation */
 // FIXME: The server should provide this
-export function cargoPath(): Promise<string> {
+export function cargoPath(env?: Record<string, string>): Promise<string> {
+    if (env?.["RUSTC_TOOLCHAIN"]) {
+        return Promise.resolve("cargo");
+    }
     return getPathForExecutable("cargo");
 }