rust-analyzer/editors/code/src/debug.ts

263 lines
9 KiB
TypeScript
Raw Normal View History

2020-05-11 13:06:57 +00:00
import * as os from "os";
2022-05-17 17:15:06 +00:00
import * as vscode from "vscode";
import * as path from "path";
import type * as ra from "./lsp_ext";
2020-05-11 13:06:57 +00:00
import { Cargo, type ExecutableInfo, getRustcId, getSysroot } from "./toolchain";
import type { Ctx } from "./ctx";
2020-07-02 16:47:40 +00:00
import { prepareEnv } from "./run";
import { unwrapUndefinable } from "./undefinable";
2020-05-11 13:06:57 +00:00
const debugOutput = vscode.window.createOutputChannel("Debug");
2022-05-17 17:15:06 +00:00
type DebugConfigProvider = (
config: ra.Runnable,
executable: string,
cargoWorkspace: string,
2022-05-17 17:15:06 +00:00
env: Record<string, string>,
2023-07-11 13:35:10 +00:00
sourceFileMap?: Record<string, string>,
2022-05-17 17:15:06 +00:00
) => vscode.DebugConfiguration;
2020-05-11 13:06:57 +00:00
2020-06-02 12:33:47 +00:00
export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
const scope = ctx.activeRustEditor?.document.uri;
if (!scope) return;
2020-05-11 13:06:57 +00:00
2020-06-02 12:33:47 +00:00
const debugConfig = await getDebugConfiguration(ctx, runnable);
if (!debugConfig) return;
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
2022-05-17 17:15:06 +00:00
const index = configurations.findIndex((c) => c.name === debugConfig.name);
2020-06-02 12:33:47 +00:00
if (index !== -1) {
2022-05-17 17:15:06 +00:00
const answer = await vscode.window.showErrorMessage(
`Launch configuration '${debugConfig.name}' already exists!`,
"Cancel",
2023-07-11 13:35:10 +00:00
"Update",
2022-05-17 17:15:06 +00:00
);
2020-06-02 12:33:47 +00:00
if (answer === "Cancel") return;
configurations[index] = debugConfig;
} else {
configurations.push(debugConfig);
}
await wsLaunchSection.update("configurations", configurations);
2020-05-11 13:06:57 +00:00
}
2020-06-02 12:33:47 +00:00
export async function startDebugSession(ctx: Ctx, runnable: ra.Runnable): Promise<boolean> {
let debugConfig: vscode.DebugConfiguration | undefined = undefined;
let message = "";
2020-05-11 13:06:57 +00:00
2020-06-02 12:33:47 +00:00
const wsLaunchSection = vscode.workspace.getConfiguration("launch");
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
2022-05-17 17:15:06 +00:00
const index = configurations.findIndex((c) => c.name === runnable.label);
2020-06-02 12:33:47 +00:00
if (-1 !== index) {
debugConfig = configurations[index];
message = " (from launch.json)";
debugOutput.clear();
} else {
debugConfig = await getDebugConfiguration(ctx, runnable);
}
if (!debugConfig) return false;
debugOutput.appendLine(`Launching debug configuration${message}:`);
debugOutput.appendLine(JSON.stringify(debugConfig, null, 2));
return vscode.debug.startDebugging(undefined, debugConfig);
2020-05-11 13:06:57 +00:00
}
function createCommandLink(extensionId: string): string {
// do not remove the second quotes inside
// encodeURIComponent or it won't work
return `extension.open?${encodeURIComponent(`"${extensionId}"`)}`;
}
2022-05-17 17:15:06 +00:00
async function getDebugConfiguration(
ctx: Ctx,
2023-07-11 13:35:10 +00:00
runnable: ra.Runnable,
2022-05-17 17:15:06 +00:00
): Promise<vscode.DebugConfiguration | undefined> {
2020-05-11 13:06:57 +00:00
const editor = ctx.activeRustEditor;
if (!editor) return;
const knownEngines: Record<string, DebugConfigProvider> = {
2024-02-29 14:00:29 +00:00
"vadimcn.vscode-lldb": getCodeLldbDebugConfig,
2024-03-28 05:57:53 +00:00
"ms-vscode.cpptools": getCCppDebugConfig,
2024-02-29 14:00:29 +00:00
"webfreak.debug": getNativeDebugConfig,
2020-05-11 13:06:57 +00:00
};
const debugOptions = ctx.config.debug;
let debugEngine = null;
if (debugOptions.engine === "auto") {
for (var engineId in knownEngines) {
debugEngine = vscode.extensions.getExtension(engineId);
if (debugEngine) break;
}
} else if (debugOptions.engine) {
2020-05-11 13:06:57 +00:00
debugEngine = vscode.extensions.getExtension(debugOptions.engine);
}
if (!debugEngine) {
2024-02-29 14:00:29 +00:00
const commandCCpp: string = createCommandLink("ms-vscode.cpptools");
const commandCodeLLDB: string = createCommandLink("vadimcn.vscode-lldb");
2024-02-29 14:00:29 +00:00
const commandNativeDebug: string = createCommandLink("webfreak.debug");
2022-05-17 17:15:06 +00:00
await vscode.window.showErrorMessage(
`Install [CodeLLDB](command:${commandCodeLLDB} "Open CodeLLDB")` +
2024-02-29 14:00:29 +00:00
`, [C/C++](command:${commandCCpp} "Open C/C++") ` +
`or [Native Debug](command:${commandNativeDebug} "Open Native Debug") for debugging.`,
2022-05-17 17:15:06 +00:00
);
2020-05-11 13:06:57 +00:00
return;
}
debugOutput.clear();
if (ctx.config.debug.openDebugPane) {
2020-05-11 13:06:57 +00:00
debugOutput.show(true);
}
2021-02-07 18:36:16 +00:00
// folder exists or RA is not active.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const workspaceFolders = vscode.workspace.workspaceFolders!;
const isMultiFolderWorkspace = workspaceFolders.length > 1;
const firstWorkspace = workspaceFolders[0];
const maybeWorkspace =
2022-05-17 17:15:06 +00:00
!isMultiFolderWorkspace || !runnable.args.workspaceRoot
? firstWorkspace
: workspaceFolders.find((w) => runnable.args.workspaceRoot?.includes(w.uri.fsPath)) ||
firstWorkspace;
const workspace = unwrapUndefinable(maybeWorkspace);
const wsFolder = path.normalize(workspace.uri.fsPath);
2022-05-17 17:15:06 +00:00
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
function simplifyPath(p: string): string {
2022-07-08 13:44:49 +00:00
// see https://github.com/rust-lang/rust-analyzer/pull/5513#issuecomment-663458818 for why this is needed
2022-05-17 17:15:06 +00:00
return path.normalize(p).replace(wsFolder, "${workspaceFolder" + workspaceQualifier + "}");
}
const env = prepareEnv(runnable, ctx.config.runnablesExtraEnv);
const { executable, workspace: cargoWorkspace } = await getDebugExecutableInfo(runnable, env);
let sourceFileMap = debugOptions.sourceFileMap;
if (sourceFileMap === "auto") {
// let's try to use the default toolchain
2023-12-19 07:46:55 +00:00
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 provider = unwrapUndefinable(knownEngines[debugEngine.id]);
const debugConfig = provider(
runnable,
simplifyPath(executable),
cargoWorkspace,
env,
sourceFileMap,
);
2020-05-11 13:06:57 +00:00
if (debugConfig.type in debugOptions.engineSettings) {
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
for (var key in settingsMap) {
debugConfig[key] = settingsMap[key];
}
}
if (debugConfig.name === "run binary") {
// The LSP side: crates\rust-analyzer\src\main_loop\handlers.rs,
// fn to_lsp_runnable(...) with RunnableKind::Bin
debugConfig.name = `run ${path.basename(executable)}`;
}
const cwd = debugConfig["cwd"];
if (cwd) {
debugConfig["cwd"] = simplifyPath(cwd);
}
2020-05-11 13:06:57 +00:00
return debugConfig;
}
async function getDebugExecutableInfo(
runnable: ra.Runnable,
2023-07-11 13:35:10 +00:00
env: Record<string, string>,
): Promise<ExecutableInfo> {
const cargo = new Cargo(runnable.args.workspaceRoot || ".", debugOutput, env);
const executableInfo = await cargo.executableInfoFromArgs(runnable.args.cargoArgs);
2020-06-02 12:33:47 +00:00
// if we are here, there were no compilation errors.
return executableInfo;
2020-06-02 12:33:47 +00:00
}
2024-02-29 14:00:29 +00:00
function getCCppDebugConfig(
2022-05-17 17:15:06 +00:00
runnable: ra.Runnable,
executable: string,
cargoWorkspace: string,
2022-05-17 17:15:06 +00:00
env: Record<string, string>,
2023-07-11 13:35:10 +00:00
sourceFileMap?: Record<string, string>,
2022-05-17 17:15:06 +00:00
): vscode.DebugConfiguration {
2020-06-02 12:33:47 +00:00
return {
2024-02-29 14:00:29 +00:00
type: os.platform() === "win32" ? "cppvsdbg" : "cppdbg",
2020-06-02 12:33:47 +00:00
request: "launch",
name: runnable.label,
program: executable,
2020-06-02 15:22:23 +00:00
args: runnable.args.executableArgs,
cwd: cargoWorkspace || runnable.args.workspaceRoot,
2024-02-29 14:00:29 +00:00
sourceFileMap,
2022-05-17 17:15:06 +00:00
env,
2020-06-02 12:33:47 +00:00
};
}
2020-05-11 13:06:57 +00:00
2024-02-29 14:00:29 +00:00
function getCodeLldbDebugConfig(
2022-05-17 17:15:06 +00:00
runnable: ra.Runnable,
executable: string,
cargoWorkspace: string,
2022-05-17 17:15:06 +00:00
env: Record<string, string>,
2023-07-11 13:35:10 +00:00
sourceFileMap?: Record<string, string>,
2022-05-17 17:15:06 +00:00
): vscode.DebugConfiguration {
2020-06-02 12:33:47 +00:00
return {
2024-02-29 14:00:29 +00:00
type: "lldb",
2020-06-02 12:33:47 +00:00
request: "launch",
name: runnable.label,
program: executable,
2020-06-02 15:22:23 +00:00
args: runnable.args.executableArgs,
cwd: cargoWorkspace || runnable.args.workspaceRoot,
2024-02-29 14:00:29 +00:00
sourceMap: sourceFileMap,
sourceLanguages: ["rust"],
2020-07-02 16:47:40 +00:00
env,
2020-06-02 12:33:47 +00:00
};
2020-05-11 13:06:57 +00:00
}
2024-02-29 14:00:29 +00:00
function getNativeDebugConfig(
runnable: ra.Runnable,
executable: string,
cargoWorkspace: string,
env: Record<string, string>,
_sourceFileMap?: Record<string, string>,
): vscode.DebugConfiguration {
return {
type: "gdb",
request: "launch",
name: runnable.label,
target: executable,
// See https://github.com/WebFreak001/code-debug/issues/359
arguments: quote(runnable.args.executableArgs),
cwd: cargoWorkspace || runnable.args.workspaceRoot,
env,
valuesFormatting: "prettyPrinters",
};
}
// Based on https://github.com/ljharb/shell-quote/blob/main/quote.js
function quote(xs: string[]) {
return xs
.map(function (s) {
if (/["\s]/.test(s) && !/'/.test(s)) {
return "'" + s.replace(/(['\\])/g, "\\$1") + "'";
}
if (/["'\s]/.test(s)) {
return '"' + s.replace(/(["\\$`!])/g, "\\$1") + '"';
}
return s.replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, "$1\\$2");
})
.join(" ");
}