Open History to see the last commit
1export interface WslDistro {
2 readonly name: string;
3 readonly isDefault: boolean;
4 readonly version: 1 | 2;
5}
6
7export interface WslConfig {
8 readonly distro: string | null;
9}
10
11// Literal space — \s would also match \n/\t/\r and corrupt UNC paths like \\wsl.localhost\<distro>\...
12// Trailing char must also be \w so hand-edited config like "Ubuntu " / "Ubuntu-" / "Ubuntu." rejects.
13export const DISTRO_NAME_PATTERN = /^\w(?:[\w \-.]*\w)?$/;
14
15export function parseWslDistroList(stdout: Buffer): readonly WslDistro[] {
16 const hasUtf16Bom = stdout.length >= 2 && stdout[0] === 0xff && stdout[1] === 0xfe;
17 const likelyUtf16Le =
18 hasUtf16Bom ||
19 Array.from(stdout.subarray(0, Math.min(stdout.length, 80))).filter(
20 (byte, index) => index % 2 === 1 && byte === 0,
21 ).length > 10;
22 let text = stdout.toString(likelyUtf16Le ? "utf16le" : "utf8");
23 if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
24
25 const lines = text.split(/\r?\n/).filter((line) => line.trim().length > 0);
26 const distros: WslDistro[] = [];
27 for (let i = 1; i < lines.length; i++) {
28 const line = lines[i]!;
29 const isDefault = line.startsWith("*");
30 const cleaned = isDefault ? line.slice(1).trim() : line.trim();
31 const fields = cleaned.split(/\s{2,}/);
32 if (fields.length < 3) continue;
33 const name = fields[0]!.trim();
34 const versionNum = parseInt(fields[2]!, 10);
35 if (!name || (versionNum !== 1 && versionNum !== 2)) continue;
36 distros.push({ name, isDefault, version: versionNum as 1 | 2 });
37 }
38 return distros;
39}
40
41// Recognizes \\wsl.localhost\<distro>\... and the legacy \\wsl$\<distro>\... so
42// `wslpath` can be invoked inside the distro that actually owns the path,
43// rather than whichever distro is configured for the desktop backend.
44export function extractDistroFromUncPath(windowsPath: string): string | null {
45 const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)/i.exec(windowsPath);
46 if (!match) return null;
47 const candidate = match[1]!;
48 return DISTRO_NAME_PATTERN.test(candidate) ? candidate : null;
49}
50
51export function wslUncPathToLinuxPath(windowsPath: string): string | null {
52 const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)(?:\\(.*))?$/i.exec(windowsPath.trim());
53 if (!match) return null;
54
55 const distro = match[1]!;
56 if (!DISTRO_NAME_PATTERN.test(distro)) return null;
57
58 const rest = match[2] ?? "";
59 if (rest.length === 0) return "/";
60
61 return `/${rest.split("\\").filter(Boolean).join("/")}`;
62}
63
64export function resolveWslHomeUncPath(
65 config: WslConfig,
66 distros: readonly WslDistro[],
67): string | null {
68 const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
69 return distroName ? `\\\\wsl.localhost\\${distroName}\\home` : null;
70}
71
72export function resolveWslPickFolderDefaultPath(
73 rawOptions: unknown,
74 config: WslConfig,
75 distros: readonly WslDistro[],
76 // Absolute Linux path of the user's home dir inside the chosen distro
77 // (e.g. "/home/josh"). When known, `~` and `~/...` expand against this so
78 // we don't open the picker at a non-existent `/home/<rest>`. When null we
79 // fall back to the `/home` parent — wrong directory but at least it exists.
80 userHome: string | null = null,
81): string | null {
82 const homePath = resolveWslHomeUncPath(config, distros);
83 if (typeof rawOptions !== "object" || rawOptions === null) {
84 return homePath;
85 }
86
87 const { initialPath } = rawOptions as { initialPath?: unknown };
88 if (typeof initialPath !== "string") {
89 return homePath;
90 }
91
92 const trimmedPath = initialPath.trim();
93 if (trimmedPath.length === 0) {
94 return homePath;
95 }
96 if (trimmedPath.startsWith("\\\\")) {
97 return trimmedPath;
98 }
99
100 const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
101 if (!distroName) {
102 return homePath;
103 }
104
105 const toUncPath = (linuxPath: string) =>
106 `\\\\wsl.localhost\\${distroName}${linuxPath.replaceAll("/", "\\")}`;
107
108 const normalizedUserHome = userHome && userHome.startsWith("/") ? userHome : null;
109
110 if (trimmedPath === "~") {
111 return normalizedUserHome ? toUncPath(normalizedUserHome) : homePath;
112 }
113 if (trimmedPath.startsWith("~/")) {
114 const remainder = trimmedPath.slice(2);
115 if (normalizedUserHome) {
116 return toUncPath(`${normalizedUserHome}/${remainder}`);
117 }
118 return homePath ? `${homePath}\\${remainder.replaceAll("/", "\\")}` : null;
119 }
120 if (trimmedPath.startsWith("/")) {
121 return toUncPath(trimmedPath);
122 }
123
124 return homePath;
125}
126
127export function isValidDistroName(value: string): boolean {
128 return DISTRO_NAME_PATTERN.test(value);
129}