From c43d59c709fc3ac25dca36d666c9085fe64b8821 Mon Sep 17 00:00:00 2001 From: roife Date: Wed, 22 May 2024 14:25:22 +0800 Subject: [PATCH 1/5] Add cwd to CargoRunnable --- crates/rust-analyzer/src/handlers/request.rs | 7 +++++++ crates/rust-analyzer/src/lsp/ext.rs | 2 ++ crates/rust-analyzer/src/lsp/to_proto.rs | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 41996db32e..1e24bf3aae 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -860,6 +860,11 @@ pub(crate) fn handle_runnables( if cmd == "run" && spec.target_kind != TargetKind::Bin { continue; } + let cwd = if cmd != "test" || spec.target_kind == TargetKind::Bin { + spec.workspace_root.clone() + } else { + spec.cargo_toml.parent().to_path_buf() + }; let mut cargo_args = vec![cmd.to_owned(), "--package".to_owned(), spec.package.clone()]; let all_targets = cmd != "run" && !is_crate_no_std; @@ -876,6 +881,7 @@ pub(crate) fn handle_runnables( kind: lsp_ext::RunnableKind::Cargo, args: lsp_ext::CargoRunnable { workspace_root: Some(spec.workspace_root.clone().into()), + cwd: Some(cwd.into()), override_cargo: config.override_cargo.clone(), cargo_args, cargo_extra_args: config.cargo_extra_args.clone(), @@ -893,6 +899,7 @@ pub(crate) fn handle_runnables( kind: lsp_ext::RunnableKind::Cargo, args: lsp_ext::CargoRunnable { workspace_root: None, + cwd: None, override_cargo: config.override_cargo, cargo_args: vec!["check".to_owned(), "--workspace".to_owned()], cargo_extra_args: config.cargo_extra_args, diff --git a/crates/rust-analyzer/src/lsp/ext.rs b/crates/rust-analyzer/src/lsp/ext.rs index 03bd83aab5..aa75633ac3 100644 --- a/crates/rust-analyzer/src/lsp/ext.rs +++ b/crates/rust-analyzer/src/lsp/ext.rs @@ -441,6 +441,8 @@ pub struct CargoRunnable { pub override_cargo: Option, #[serde(skip_serializing_if = "Option::is_none")] pub workspace_root: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub cwd: Option, // command, --package and --lib stuff pub cargo_args: Vec, // user-specified additional cargo args, like `--release`. diff --git a/crates/rust-analyzer/src/lsp/to_proto.rs b/crates/rust-analyzer/src/lsp/to_proto.rs index 03daccc99c..1d7062c989 100644 --- a/crates/rust-analyzer/src/lsp/to_proto.rs +++ b/crates/rust-analyzer/src/lsp/to_proto.rs @@ -1360,6 +1360,10 @@ pub(crate) fn runnable( let config = snap.config.runnables(); let spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id)?; let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone()); + let cwd = match runnable.kind { + ide::RunnableKind::Bin { .. } => workspace_root.clone().map(|it| it.into()), + _ => spec.as_ref().map(|it| it.cargo_toml.parent().into()), + }; let target = spec.as_ref().map(|s| s.target.clone()); let (cargo_args, executable_args) = CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg); @@ -1372,6 +1376,7 @@ pub(crate) fn runnable( kind: lsp_ext::RunnableKind::Cargo, args: lsp_ext::CargoRunnable { workspace_root: workspace_root.map(|it| it.into()), + cwd, override_cargo: config.override_cargo, cargo_args, cargo_extra_args: config.cargo_extra_args, From 89843ba083614a70f24282bb50bc0d82c3c6239e Mon Sep 17 00:00:00 2001 From: roife Date: Wed, 22 May 2024 14:27:06 +0800 Subject: [PATCH 2/5] Revert "Debug use cargo workspace root as cwd. fixes #13022" This reverts commit 4ca86edac97d47eecb78b16abf255950c10b67ca. --- editors/code/src/debug.ts | 30 ++++++++++-------------------- editors/code/src/toolchain.ts | 14 ++------------ 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index bad1f48de8..0f90ed34ef 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 type * as ra from "./lsp_ext"; -import { Cargo, type ExecutableInfo, getRustcId, getSysroot } from "./toolchain"; +import { Cargo, getRustcId, getSysroot } from "./toolchain"; import type { Ctx } from "./ctx"; import { prepareEnv } from "./run"; import { unwrapUndefinable } from "./undefinable"; @@ -12,7 +12,6 @@ const debugOutput = vscode.window.createOutputChannel("Debug"); type DebugConfigProvider = ( config: ra.Runnable, executable: string, - cargoWorkspace: string, env: Record, sourceFileMap?: Record, ) => vscode.DebugConfiguration; @@ -134,7 +133,7 @@ async function getDebugConfiguration( } const env = prepareEnv(runnable, ctx.config.runnablesExtraEnv); - const { executable, workspace: cargoWorkspace } = await getDebugExecutableInfo(runnable, env); + const executable = await getDebugExecutable(runnable, env); let sourceFileMap = debugOptions.sourceFileMap; if (sourceFileMap === "auto") { // let's try to use the default toolchain @@ -148,13 +147,7 @@ async function getDebugConfiguration( } const provider = unwrapUndefinable(knownEngines[debugEngine.id]); - const debugConfig = provider( - runnable, - simplifyPath(executable), - cargoWorkspace, - env, - sourceFileMap, - ); + const debugConfig = provider(runnable, simplifyPath(executable), env, sourceFileMap); if (debugConfig.type in debugOptions.engineSettings) { const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; for (var key in settingsMap) { @@ -176,21 +169,20 @@ async function getDebugConfiguration( return debugConfig; } -async function getDebugExecutableInfo( +async function getDebugExecutable( runnable: ra.Runnable, env: Record, -): Promise { +): Promise { const cargo = new Cargo(runnable.args.workspaceRoot || ".", debugOutput, env); - const executableInfo = await cargo.executableInfoFromArgs(runnable.args.cargoArgs); + const executable = await cargo.executableFromArgs(runnable.args.cargoArgs); // if we are here, there were no compilation errors. - return executableInfo; + return executable; } function getCCppDebugConfig( runnable: ra.Runnable, executable: string, - cargoWorkspace: string, env: Record, sourceFileMap?: Record, ): vscode.DebugConfiguration { @@ -200,7 +192,7 @@ function getCCppDebugConfig( name: runnable.label, program: executable, args: runnable.args.executableArgs, - cwd: cargoWorkspace || runnable.args.workspaceRoot, + cwd: runnable.args.workspaceRoot, sourceFileMap, env, // See https://github.com/rust-lang/rust-analyzer/issues/16901#issuecomment-2024486941 @@ -213,7 +205,6 @@ function getCCppDebugConfig( function getCodeLldbDebugConfig( runnable: ra.Runnable, executable: string, - cargoWorkspace: string, env: Record, sourceFileMap?: Record, ): vscode.DebugConfiguration { @@ -223,7 +214,7 @@ function getCodeLldbDebugConfig( name: runnable.label, program: executable, args: runnable.args.executableArgs, - cwd: cargoWorkspace || runnable.args.workspaceRoot, + cwd: runnable.args.workspaceRoot, sourceMap: sourceFileMap, sourceLanguages: ["rust"], env, @@ -233,7 +224,6 @@ function getCodeLldbDebugConfig( function getNativeDebugConfig( runnable: ra.Runnable, executable: string, - cargoWorkspace: string, env: Record, _sourceFileMap?: Record, ): vscode.DebugConfiguration { @@ -244,7 +234,7 @@ function getNativeDebugConfig( target: executable, // See https://github.com/WebFreak001/code-debug/issues/359 arguments: quote(runnable.args.executableArgs), - cwd: cargoWorkspace || runnable.args.workspaceRoot, + cwd: runnable.args.workspaceRoot, env, valuesFormatting: "prettyPrinters", }; diff --git a/editors/code/src/toolchain.ts b/editors/code/src/toolchain.ts index a0b34406c1..58e5fc747a 100644 --- a/editors/code/src/toolchain.ts +++ b/editors/code/src/toolchain.ts @@ -9,17 +9,11 @@ import { unwrapUndefinable } from "./undefinable"; interface CompilationArtifact { fileName: string; - workspace: string; name: string; kind: string; isTest: boolean; } -export interface ExecutableInfo { - executable: string; - workspace: string; -} - export interface ArtifactSpec { cargoArgs: string[]; filter?: (artifacts: CompilationArtifact[]) => CompilationArtifact[]; @@ -74,7 +68,6 @@ export class Cargo { artifacts.push({ fileName: message.executable, name: message.target.name, - workspace: path.dirname(message.manifest_path), kind: message.target.kind[0], isTest: message.profile.test, }); @@ -93,7 +86,7 @@ export class Cargo { return spec.filter?.(artifacts) ?? artifacts; } - async executableInfoFromArgs(args: readonly string[]): Promise { + async executableFromArgs(args: readonly string[]): Promise { const artifacts = await this.getArtifacts(Cargo.artifactSpec(args)); if (artifacts.length === 0) { @@ -103,10 +96,7 @@ export class Cargo { } const artifact = unwrapUndefinable(artifacts[0]); - return { - executable: artifact.fileName, - workspace: artifact.workspace, - }; + return artifact.fileName; } private async runCargo( From f4d3547bc68f51061c8e48e4a3a41e2177c1d283 Mon Sep 17 00:00:00 2001 From: roife Date: Wed, 22 May 2024 14:29:34 +0800 Subject: [PATCH 3/5] Use cwd from runnable.args for debugger --- editors/code/src/debug.ts | 6 +++--- editors/code/src/lsp_ext.ts | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index 0f90ed34ef..855e8b082a 100644 --- a/editors/code/src/debug.ts +++ b/editors/code/src/debug.ts @@ -192,7 +192,7 @@ function getCCppDebugConfig( name: runnable.label, program: executable, args: runnable.args.executableArgs, - cwd: runnable.args.workspaceRoot, + cwd: runnable.args.cwd || runnable.args.workspaceRoot || ".", sourceFileMap, env, // See https://github.com/rust-lang/rust-analyzer/issues/16901#issuecomment-2024486941 @@ -214,7 +214,7 @@ function getCodeLldbDebugConfig( name: runnable.label, program: executable, args: runnable.args.executableArgs, - cwd: runnable.args.workspaceRoot, + cwd: runnable.args.cwd || runnable.args.workspaceRoot || ".", sourceMap: sourceFileMap, sourceLanguages: ["rust"], env, @@ -234,7 +234,7 @@ function getNativeDebugConfig( target: executable, // See https://github.com/WebFreak001/code-debug/issues/359 arguments: quote(runnable.args.executableArgs), - cwd: runnable.args.workspaceRoot, + cwd: runnable.args.cwd || runnable.args.workspaceRoot || ".", env, valuesFormatting: "prettyPrinters", }; diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 9a7a4aae95..8e48aeef15 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -226,6 +226,7 @@ export type Runnable = { kind: "cargo"; args: { workspaceRoot?: string; + cwd?: string; cargoArgs: string[]; cargoExtraArgs: string[]; executableArgs: string[]; From ef59b49f7e69ba237e1985270f423d3e104d5247 Mon Sep 17 00:00:00 2001 From: roife Date: Wed, 22 May 2024 14:48:55 +0800 Subject: [PATCH 4/5] Update docs --- docs/dev/lsp-extensions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index c8728c33ea..1c91e856e7 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@