Skip to content

Commit 91c0bef

Browse files
committed
Auto merge of #18320 - davidbarsky:davidbarsky/fix-lldb-dap-calling-rustc, r=davidbarsky
internal: fix lldb-dap unconditionally calling rustc Fixes #18318. I also took the opportunity to refactor how `discoverSourceFileMap` worked—it now returns a type instead of mutating a map in place. I tested this change using the LLDB DAP extension. I needed to set `"lldb-dap.executable-path": "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-dap"` for everything to work, however, but once I did, was able to successfully debug a test.
2 parents 8dd53a3 + f25cb19 commit 91c0bef

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

editors/code/src/debug.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type * as ra from "./lsp_ext";
66
import { Cargo } from "./toolchain";
77
import type { Ctx } from "./ctx";
88
import { createTaskFromRunnable, prepareEnv } from "./run";
9-
import { execSync } from "node:child_process";
109
import { execute, isCargoRunnableArgs, unwrapUndefinable } from "./util";
1110
import type { Config } from "./config";
1211

@@ -152,9 +151,24 @@ async function getDebugConfiguration(
152151
const env = prepareEnv(inheritEnv, runnable.label, runnableArgs, config.runnablesExtraEnv);
153152
const executable = await getDebugExecutable(runnableArgs, env);
154153
let sourceFileMap = debugOptions.sourceFileMap;
154+
155155
if (sourceFileMap === "auto") {
156156
sourceFileMap = {};
157-
await discoverSourceFileMap(sourceFileMap, env, wsFolder);
157+
const computedSourceFileMap = await discoverSourceFileMap(env, wsFolder);
158+
159+
if (computedSourceFileMap) {
160+
// lldb-dap requires passing the source map as an array of two element arrays.
161+
// the two element array contains a source and destination pathname.
162+
// TODO: remove lldb-dap-specific post-processing once
163+
// https://github.com/llvm/llvm-project/pull/106919/ is released in the extension.
164+
if (provider.type === "lldb-dap") {
165+
provider.additional["sourceMap"] = [
166+
[computedSourceFileMap?.source, computedSourceFileMap?.destination],
167+
];
168+
} else {
169+
sourceFileMap[computedSourceFileMap.source] = computedSourceFileMap.destination;
170+
}
171+
}
158172
}
159173

160174
const debugConfig = getDebugConfig(
@@ -189,11 +203,15 @@ async function getDebugConfiguration(
189203
return debugConfig;
190204
}
191205

206+
type SourceFileMap = {
207+
source: string;
208+
destination: string;
209+
};
210+
192211
async function discoverSourceFileMap(
193-
sourceFileMap: Record<string, string>,
194212
env: Record<string, string>,
195213
cwd: string,
196-
) {
214+
): Promise<SourceFileMap | undefined> {
197215
const sysroot = env["RUSTC_TOOLCHAIN"];
198216
if (sysroot) {
199217
// let's try to use the default toolchain
@@ -203,9 +221,11 @@ async function discoverSourceFileMap(
203221
const commitHash = rx.exec(data)?.[1];
204222
if (commitHash) {
205223
const rustlib = path.normalize(sysroot + "/lib/rustlib/src/rust");
206-
sourceFileMap[`/rustc/${commitHash}/`] = rustlib;
224+
return { source: rustlib, destination: rustlib };
207225
}
208226
}
227+
228+
return;
209229
}
210230

211231
type PropertyFetcher<Config, Input, Key extends keyof Config> = (
@@ -218,7 +238,7 @@ type DebugConfigProvider<Type extends string, DebugConfig extends BaseDebugConfi
218238
runnableArgsProperty: PropertyFetcher<DebugConfig, ra.CargoRunnableArgs, keyof DebugConfig>;
219239
sourceFileMapProperty?: keyof DebugConfig;
220240
type: Type;
221-
additional?: Record<string, unknown>;
241+
additional: Record<string, unknown>;
222242
};
223243

224244
type KnownEnginesType = (typeof knownEngines)[keyof typeof knownEngines];
@@ -236,16 +256,7 @@ const knownEngines: {
236256
"args",
237257
runnableArgs.executableArgs,
238258
],
239-
additional: {
240-
sourceMap: [
241-
[
242-
`/rustc/${/commit-hash:\s(.*)$/m.exec(
243-
execSync("rustc -V -v", {}).toString(),
244-
)?.[1]}/library`,
245-
"${config:rust-analyzer.cargo.sysroot}/lib/rustlib/src/rust/library",
246-
],
247-
],
248-
},
259+
additional: {},
249260
},
250261
"vadimcn.vscode-lldb": {
251262
type: "lldb",

0 commit comments

Comments
 (0)