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 spaces only; \s would accept controls and corrupt UNC distro paths.
12const DISTRO_NAME_PATTERN = /^\w(?:[\w \-.]*\w)?$/;
13
14export function parseWslDistroList(stdout: Buffer): readonly WslDistro[] {
15 const hasUtf16Bom = stdout.length >= 2 && stdout[0] === 0xff && stdout[1] === 0xfe;
16 const likelyUtf16Le =
17 hasUtf16Bom ||
18 Array.from(stdout.subarray(0, Math.min(stdout.length, 80))).filter(
19 (byte, index) => index % 2 === 1 && byte === 0,
20 ).length > 10;
21 let text = stdout.toString(likelyUtf16Le ? "utf16le" : "utf8");
22 if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
23
24 const lines = text.split(/\r?\n/).filter((line) => line.trim().length > 0);
25 const distros: WslDistro[] = [];
26 for (let i = 1; i < lines.length; i++) {
27 const line = lines[i]!;
28 const isDefault = line.startsWith("*");
29 const cleaned = isDefault ? line.slice(1).trim() : line.trim();
30 const fields = cleaned.split(/\s{2,}/);
31 if (fields.length < 3) continue;
32 const name = fields[0]!.trim();
33 const versionNum = parseInt(fields[2]!, 10);
34 if (!name || (versionNum !== 1 && versionNum !== 2)) continue;
35 distros.push({ name, isDefault, version: versionNum as 1 | 2 });
36 }
37 return distros;
38}
39
40// Resolve UNC paths in their owning distro, which can differ from the configured desktop backend.
41export function extractDistroFromUncPath(windowsPath: string): string | null {
42 const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)/i.exec(windowsPath);
43 if (!match) return null;
44 const candidate = match[1]!;
45 return DISTRO_NAME_PATTERN.test(candidate) ? candidate : null;
46}
47
48export function wslUncPathToLinuxPath(windowsPath: string): string | null {
49 const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)(?:\\(.*))?$/i.exec(windowsPath.trim());
50 if (!match) return null;
51
52 const distro = match[1]!;
53 if (!DISTRO_NAME_PATTERN.test(distro)) return null;
54
55 const rest = match[2] ?? "";
56 if (rest.length === 0) return "/";
57
58 return `/${rest.split("\\").filter(Boolean).join("/")}`;
59}
60
61function resolveWslHomeUncPath(config: WslConfig, distros: readonly WslDistro[]): string | null {
62 const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
63 return distroName ? `\\\\wsl.localhost\\${distroName}\\home` : null;
64}
65
66export function resolveWslPickFolderDefaultPath(
67 rawOptions: unknown,
68 config: WslConfig,
69 distros: readonly WslDistro[],
70 userHome: string | null = null,
71): string | null {
72 const homePath = resolveWslHomeUncPath(config, distros);
73 if (typeof rawOptions !== "object" || rawOptions === null) {
74 return homePath;
75 }
76
77 const { initialPath } = rawOptions as { initialPath?: unknown };
78 if (typeof initialPath !== "string") {
79 return homePath;
80 }
81
82 const trimmedPath = initialPath.trim();
83 if (trimmedPath.length === 0) {
84 return homePath;
85 }
86 if (trimmedPath.startsWith("\\\\")) {
87 return trimmedPath;
88 }
89
90 const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
91 if (!distroName) {
92 return homePath;
93 }
94
95 const toUncPath = (linuxPath: string) =>
96 `\\\\wsl.localhost\\${distroName}${linuxPath.replaceAll("/", "\\")}`;
97
98 const normalizedUserHome = userHome && userHome.startsWith("/") ? userHome : null;
99
100 if (trimmedPath === "~") {
101 return normalizedUserHome ? toUncPath(normalizedUserHome) : homePath;
102 }
103 if (trimmedPath.startsWith("~/")) {
104 const remainder = trimmedPath.slice(2);
105 if (normalizedUserHome) {
106 return toUncPath(`${normalizedUserHome}/${remainder}`);
107 }
108 return homePath ? `${homePath}\\${remainder.replaceAll("/", "\\")}` : null;
109 }
110 if (trimmedPath.startsWith("/")) {
111 return toUncPath(trimmedPath);
112 }
113
114 return homePath;
115}
116
117export function isValidDistroName(value: string): boolean {
118 return DISTRO_NAME_PATTERN.test(value);
119}