Skip to content

Commit c9d189d

Browse files
committed
Auto merge of rust-lang#15993 - meowtec:fix/workspaces-debug-cwd, r=Veykril
Debug use cargo workspace root as `cwd` fixes rust-lang#13022
2 parents 1c51e25 + 4ca86ed commit c9d189d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

editors/code/src/debug.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from "vscode";
33
import * as path from "path";
44
import type * as ra from "./lsp_ext";
55

6-
import { Cargo, getRustcId, getSysroot } from "./toolchain";
6+
import { Cargo, type ExecutableInfo, getRustcId, getSysroot } from "./toolchain";
77
import type { Ctx } from "./ctx";
88
import { prepareEnv } from "./run";
99
import { unwrapUndefinable } from "./undefinable";
@@ -12,6 +12,7 @@ const debugOutput = vscode.window.createOutputChannel("Debug");
1212
type DebugConfigProvider = (
1313
config: ra.Runnable,
1414
executable: string,
15+
cargoWorkspace: string,
1516
env: Record<string, string>,
1617
sourceFileMap?: Record<string, string>,
1718
) => vscode.DebugConfiguration;
@@ -130,7 +131,7 @@ async function getDebugConfiguration(
130131
}
131132

132133
const env = prepareEnv(runnable, ctx.config.runnablesExtraEnv);
133-
const executable = await getDebugExecutable(runnable, env);
134+
const { executable, workspace: cargoWorkspace } = await getDebugExecutableInfo(runnable, env);
134135
let sourceFileMap = debugOptions.sourceFileMap;
135136
if (sourceFileMap === "auto") {
136137
// let's try to use the default toolchain
@@ -142,7 +143,13 @@ async function getDebugConfiguration(
142143
}
143144

144145
const provider = unwrapUndefinable(knownEngines[debugEngine.id]);
145-
const debugConfig = provider(runnable, simplifyPath(executable), env, sourceFileMap);
146+
const debugConfig = provider(
147+
runnable,
148+
simplifyPath(executable),
149+
cargoWorkspace,
150+
env,
151+
sourceFileMap,
152+
);
146153
if (debugConfig.type in debugOptions.engineSettings) {
147154
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
148155
for (var key in settingsMap) {
@@ -164,20 +171,21 @@ async function getDebugConfiguration(
164171
return debugConfig;
165172
}
166173

167-
async function getDebugExecutable(
174+
async function getDebugExecutableInfo(
168175
runnable: ra.Runnable,
169176
env: Record<string, string>,
170-
): Promise<string> {
177+
): Promise<ExecutableInfo> {
171178
const cargo = new Cargo(runnable.args.workspaceRoot || ".", debugOutput, env);
172-
const executable = await cargo.executableFromArgs(runnable.args.cargoArgs);
179+
const executableInfo = await cargo.executableInfoFromArgs(runnable.args.cargoArgs);
173180

174181
// if we are here, there were no compilation errors.
175-
return executable;
182+
return executableInfo;
176183
}
177184

178185
function getLldbDebugConfig(
179186
runnable: ra.Runnable,
180187
executable: string,
188+
cargoWorkspace: string,
181189
env: Record<string, string>,
182190
sourceFileMap?: Record<string, string>,
183191
): vscode.DebugConfiguration {
@@ -187,7 +195,7 @@ function getLldbDebugConfig(
187195
name: runnable.label,
188196
program: executable,
189197
args: runnable.args.executableArgs,
190-
cwd: runnable.args.workspaceRoot,
198+
cwd: cargoWorkspace || runnable.args.workspaceRoot,
191199
sourceMap: sourceFileMap,
192200
sourceLanguages: ["rust"],
193201
env,
@@ -197,6 +205,7 @@ function getLldbDebugConfig(
197205
function getCppvsDebugConfig(
198206
runnable: ra.Runnable,
199207
executable: string,
208+
cargoWorkspace: string,
200209
env: Record<string, string>,
201210
sourceFileMap?: Record<string, string>,
202211
): vscode.DebugConfiguration {
@@ -206,7 +215,7 @@ function getCppvsDebugConfig(
206215
name: runnable.label,
207216
program: executable,
208217
args: runnable.args.executableArgs,
209-
cwd: runnable.args.workspaceRoot,
218+
cwd: cargoWorkspace || runnable.args.workspaceRoot,
210219
sourceFileMap,
211220
env,
212221
};

editors/code/src/toolchain.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ import { unwrapUndefinable } from "./undefinable";
99

1010
interface CompilationArtifact {
1111
fileName: string;
12+
workspace: string;
1213
name: string;
1314
kind: string;
1415
isTest: boolean;
1516
}
1617

18+
export interface ExecutableInfo {
19+
executable: string;
20+
workspace: string;
21+
}
22+
1723
export interface ArtifactSpec {
1824
cargoArgs: string[];
1925
filter?: (artifacts: CompilationArtifact[]) => CompilationArtifact[];
@@ -68,6 +74,7 @@ export class Cargo {
6874
artifacts.push({
6975
fileName: message.executable,
7076
name: message.target.name,
77+
workspace: message.manifest_path.replace(/\/Cargo\.toml$/, ""),
7178
kind: message.target.kind[0],
7279
isTest: message.profile.test,
7380
});
@@ -86,7 +93,7 @@ export class Cargo {
8693
return spec.filter?.(artifacts) ?? artifacts;
8794
}
8895

89-
async executableFromArgs(args: readonly string[]): Promise<string> {
96+
async executableInfoFromArgs(args: readonly string[]): Promise<ExecutableInfo> {
9097
const artifacts = await this.getArtifacts(Cargo.artifactSpec(args));
9198

9299
if (artifacts.length === 0) {
@@ -96,7 +103,10 @@ export class Cargo {
96103
}
97104

98105
const artifact = unwrapUndefinable(artifacts[0]);
99-
return artifact.fileName;
106+
return {
107+
executable: artifact.fileName,
108+
workspace: artifact.workspace,
109+
};
100110
}
101111

102112
private async runCargo(

0 commit comments

Comments
 (0)