Skip to content

Commit 28a279c

Browse files
authored
[lldb-dap] expand tilde in dap executable path (#162635)
Users may have multiple devices and would like to resolve the homepath based on the machine they are on. expands the tilde `~` character at the front of the given file path.
1 parent 00171b3 commit 28a279c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as fs from "node:fs/promises";
66
import { ConfigureButton, OpenSettingsButton } from "./ui/show-error-message";
77
import { ErrorWithNotification } from "./ui/error-with-notification";
88
import { LogFilePathProvider, LogType } from "./logging";
9+
import { expandUser } from "./utils";
910

1011
const exec = util.promisify(child_process.execFile);
1112

@@ -116,8 +117,9 @@ async function getDAPExecutable(
116117
configuration: vscode.DebugConfiguration,
117118
): Promise<string> {
118119
// Check if the executable was provided in the launch configuration.
119-
const launchConfigPath = configuration["debugAdapterExecutable"];
120+
let launchConfigPath = configuration["debugAdapterExecutable"];
120121
if (typeof launchConfigPath === "string" && launchConfigPath.length !== 0) {
122+
launchConfigPath = expandUser(launchConfigPath);
121123
if (!(await isExecutable(launchConfigPath))) {
122124
throw new ErrorWithNotification(
123125
`Debug adapter path "${launchConfigPath}" is not a valid file. The path comes from your launch configuration.`,
@@ -129,7 +131,7 @@ async function getDAPExecutable(
129131

130132
// Check if the executable was provided in the extension's configuration.
131133
const config = vscode.workspace.getConfiguration("lldb-dap", workspaceFolder);
132-
const configPath = config.get<string>("executable-path");
134+
const configPath = expandUser(config.get<string>("executable-path") ?? "");
133135
if (configPath && configPath.length !== 0) {
134136
if (!(await isExecutable(configPath))) {
135137
throw new ErrorWithNotification(
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as os from "os";
2+
import * as path from "path";
3+
4+
/**
5+
* Expands the character `~` to the user's home directory
6+
*/
7+
export function expandUser(file_path: string): string {
8+
if (os.platform() == "win32") {
9+
return file_path;
10+
}
11+
12+
if (!file_path) {
13+
return "";
14+
}
15+
16+
if (!file_path.startsWith("~")) {
17+
return file_path;
18+
}
19+
20+
const path_len = file_path.length;
21+
if (path_len == 1) {
22+
return os.homedir();
23+
}
24+
25+
if (file_path.charAt(1) == path.sep) {
26+
return path.join(os.homedir(), file_path.substring(1));
27+
}
28+
29+
const sep_index = file_path.indexOf(path.sep);
30+
const user_name_end = sep_index == -1 ? file_path.length : sep_index;
31+
const user_name = file_path.substring(1, user_name_end);
32+
try {
33+
if (user_name == os.userInfo().username) {
34+
return path.join(os.homedir(), file_path.substring(user_name_end));
35+
}
36+
} catch (err) {
37+
return file_path;
38+
}
39+
40+
return file_path;
41+
}

0 commit comments

Comments
 (0)