Skip to content

Commit c4ca6e2

Browse files
committed
Uniformed way to get Debug Lens target executable.
1 parent 1116c9a commit c4ca6e2

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

editors/code/src/commands/runnables.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,19 @@ export function runSingle(ctx: Ctx): Cmd {
6464
};
6565
}
6666

67-
function getLldbDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): vscode.DebugConfiguration {
67+
function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
6868
return {
6969
type: "lldb",
7070
request: "launch",
7171
name: config.label,
72-
cargo: {
73-
args: config.args,
74-
},
72+
program: executable,
7573
args: config.extraArgs,
7674
cwd: config.cwd,
7775
sourceMap: sourceFileMap
7876
};
7977
}
8078

81-
const debugOutput = vscode.window.createOutputChannel("Debug");
82-
83-
async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): Promise<vscode.DebugConfiguration> {
84-
debugOutput.clear();
85-
86-
const cargo = new Cargo(config.cwd || '.', debugOutput);
87-
const executable = await cargo.executableFromArgs(config.args);
88-
89-
// if we are here, there were no compilation errors.
79+
function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
9080
return {
9181
type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg',
9282
request: "launch",
@@ -98,36 +88,53 @@ async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record<st
9888
};
9989
}
10090

91+
const debugOutput = vscode.window.createOutputChannel("Debug");
92+
93+
async function getDebugExecutable(config: ra.Runnable): Promise<string> {
94+
debugOutput.clear();
95+
96+
const cargo = new Cargo(config.cwd || '.', debugOutput);
97+
const executable = await cargo.executableFromArgs(config.args);
98+
99+
// if we are here, there were no compilation errors.
100+
return executable;
101+
}
102+
103+
type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
104+
101105
export function debugSingle(ctx: Ctx): Cmd {
102106
return async (config: ra.Runnable) => {
103107
const editor = ctx.activeRustEditor;
104108
if (!editor) return;
105109

106-
const lldbId = "vadimcn.vscode-lldb";
107-
const cpptoolsId = "ms-vscode.cpptools";
110+
const knownEngines: Record<string, DebugConfigProvider> = {
111+
"vadimcn.vscode-lldb": getLldbDebugConfig,
112+
"ms-vscode.cpptools": getCppvsDebugConfig
113+
};
114+
const debugOptions = ctx.config.debug;
108115

109-
const debugEngineId = ctx.config.debug.engine;
110116
let debugEngine = null;
111-
if (debugEngineId === "auto") {
112-
debugEngine = vscode.extensions.getExtension(lldbId);
113-
if (!debugEngine) {
114-
debugEngine = vscode.extensions.getExtension(cpptoolsId);
117+
if (debugOptions.engine === "auto") {
118+
for (var engineId in knownEngines) {
119+
debugEngine = vscode.extensions.getExtension(engineId);
120+
if (debugEngine) break;
115121
}
116122
}
117123
else {
118-
debugEngine = vscode.extensions.getExtension(debugEngineId);
124+
debugEngine = vscode.extensions.getExtension(debugOptions.engine);
119125
}
120126

121127
if (!debugEngine) {
122-
vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId})`
123-
+ ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) extension for debugging.`);
128+
vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)`
129+
+ ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`);
124130
return;
125131
}
126132

127-
const debugConfig = lldbId === debugEngine.id
128-
? getLldbDebugConfig(config, ctx.config.debug.sourceFileMap)
129-
: await getCppvsDebugConfig(config, ctx.config.debug.sourceFileMap);
133+
const executable = await getDebugExecutable(config);
134+
const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap);
130135

136+
debugOutput.appendLine("Launching debug configuration:");
137+
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
131138
return vscode.debug.startDebugging(undefined, debugConfig);
132139
};
133140
}

editors/code/src/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ export class Config {
108108
}
109109

110110
get debug() {
111+
// "/rustc/<id>" used by suggestions only.
112+
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
113+
111114
return {
112115
engine: this.get<string>("debug.engine"),
113-
sourceFileMap: this.get<Record<string, string>>("debug.sourceFileMap"),
116+
sourceFileMap: sourceFileMap,
114117
};
115118
}
116-
117119
}

0 commit comments

Comments
 (0)