diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index 031ddbb327..6391003987 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -86,6 +86,7 @@ export const pickFolder = makeIpcMethod({ options, { enabled: true, distro: settings.wslDistro }, yield* wslEnvironment.listDistros, + Option.getOrNull(yield* wslEnvironment.getUserHome(settings.wslDistro)), ), ) : environment.resolvePickFolderDefaultPath(options); diff --git a/apps/desktop/src/ipc/methods/wsl.ts b/apps/desktop/src/ipc/methods/wsl.ts index 68fe19bbe3..4bbd70248a 100644 --- a/apps/desktop/src/ipc/methods/wsl.ts +++ b/apps/desktop/src/ipc/methods/wsl.ts @@ -97,12 +97,12 @@ export const setWslBackend = makeIpcMethod({ }); yield* backendManager.stop(); yield* backendManager.start; - yield* backendManager.waitForReady(SWAP_READINESS_TIMEOUT); + const rolledBack = yield* backendManager.waitForReady(SWAP_READINESS_TIMEOUT); + const failedTarget = input.mode === "wsl" ? "WSL backend" : "local backend"; return yield* new WslBackendSwapError({ - message: - input.mode === "wsl" - ? "The WSL backend didn't come up. Rolled back to the previous mode — check that the chosen distro is healthy and try again." - : "The local backend didn't come up. Rolled back to the previous mode.", + message: rolledBack + ? `The ${failedTarget} didn't come up. Rolled back to the previous mode — check that the chosen distro is healthy and try again.` + : `The ${failedTarget} didn't come up, and the rollback also failed to start. The app is in a degraded state — restart T3 Code to recover.`, }); } diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.ts index 75f45d8a69..a501b969f3 100644 --- a/apps/desktop/src/wsl/DesktopWslEnvironment.ts +++ b/apps/desktop/src/wsl/DesktopWslEnvironment.ts @@ -20,6 +20,7 @@ const WSLPATH_TIMEOUT = Duration.seconds(10); const PROBE_TIMEOUT = Duration.seconds(10); const TOOLCHAIN_TIMEOUT = Duration.seconds(10); const BUILD_TIMEOUT = Duration.minutes(5); +const USER_HOME_TIMEOUT = Duration.seconds(5); export interface EnsureWslNodePtyOptions { readonly allowBuild?: boolean; @@ -38,6 +39,9 @@ export interface DesktopWslEnvironmentShape { distro: string | null, windowsPath: string, ) => Effect.Effect>; + // Resolves the user's Linux home dir inside the chosen distro (e.g. + // "/home/josh"). Used by the folder picker to expand `~` correctly. + readonly getUserHome: (distro: string | null) => Effect.Effect>; readonly ensureNodePty: ( distro: string | null, windowsRepoRoot: string, @@ -375,6 +379,44 @@ const windowsToWslPathImpl = ( ); }; +const getUserHomeImpl = ( + distro: string | null, +): Effect.Effect, never, ChildProcessSpawner.ChildProcessSpawner> => + Effect.scoped( + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const command = ChildProcess.make( + "wsl.exe", + // printf so there's no trailing newline noise; getent so we get the + // real home from /etc/passwd even if $HOME is unset for some reason. + [ + ...buildDistroArgs(distro), + "--", + "sh", + "-c", + 'printf "%s" "$(getent passwd "$(id -un)" | cut -d: -f6)"', + ], + { + stdin: "ignore", + stdout: "pipe", + stderr: "ignore", + killSignal: "SIGTERM", + forceKillAfter: PROCESS_TERMINATE_GRACE, + }, + ); + const handle = yield* spawner.spawn(command); + const stdoutBytes = yield* Stream.runCollect(handle.stdout); + const exitCode = yield* handle.exitCode; + if ((exitCode as unknown as number) !== 0) return Option.none(); + const home = decodeUtf8(concatChunks(stdoutBytes)).trim(); + return home.startsWith("/") ? Option.some(home) : Option.none(); + }), + ).pipe( + Effect.timeoutOption(USER_HOME_TIMEOUT), + Effect.map(Option.flatten), + Effect.catch(() => Effect.succeed(Option.none())), + ); + const makeIsAvailable = ( platform: NodeJS.Platform, windir: string, @@ -391,6 +433,7 @@ export interface DesktopWslEnvironmentTestStub { readonly isAvailable?: boolean; readonly distros?: ReadonlyArray; readonly windowsToWslPath?: (distro: string | null, windowsPath: string) => Option.Option; + readonly getUserHome?: (distro: string | null) => Option.Option; readonly ensureNodePty?: ( distro: string | null, windowsRepoRoot: string, @@ -407,6 +450,7 @@ export const layerTest = (stub: DesktopWslEnvironmentTestStub = {}) => preWarm: () => Effect.void, windowsToWslPath: (distro, windowsPath) => Effect.succeed(stub.windowsToWslPath?.(distro, windowsPath) ?? Option.none()), + getUserHome: (distro) => Effect.succeed(stub.getUserHome?.(distro) ?? Option.none()), ensureNodePty: (distro, windowsRepoRoot, options) => Effect.succeed( stub.ensureNodePty?.(distro, windowsRepoRoot, options) ?? { @@ -441,12 +485,28 @@ export const layer = Layer.effect( Effect.withSpan("desktop.wsl.windowsToWslPath"), ); + // Cache user-home results per distro key — folder picker can be opened + // many times in a session and the value is stable for the life of the + // distro. Negative results aren't cached so a transient wsl.exe failure + // doesn't permanently disable tilde expansion. + const userHomeCache = new Map(); + const getUserHome = (distro: string | null) => + Effect.gen(function* () { + const key = distro ?? "__default__"; + const cached = userHomeCache.get(key); + if (cached !== undefined) return Option.some(cached); + const resolved = yield* provideSpawner(getUserHomeImpl(distro)); + if (Option.isSome(resolved)) userHomeCache.set(key, resolved.value); + return resolved; + }).pipe(Effect.withSpan("desktop.wsl.getUserHome")); + return DesktopWslEnvironment.of({ isAvailable, listDistros: provideSpawner(listDistrosImpl).pipe(Effect.withSpan("desktop.wsl.listDistros")), preWarm: (distro) => provideSpawner(preWarmImpl(distro)).pipe(Effect.withSpan("desktop.wsl.preWarm")), windowsToWslPath, + getUserHome, ensureNodePty: (distro, windowsRepoRoot, options) => provideSpawner(ensureNodePtyImpl(distro, windowsRepoRoot, windowsToWslPath, options)).pipe( Effect.withSpan("desktop.wsl.ensureNodePty"), diff --git a/apps/desktop/src/wsl/wslPathParsing.test.ts b/apps/desktop/src/wsl/wslPathParsing.test.ts index ed1d65eb16..b8d23ed2ec 100644 --- a/apps/desktop/src/wsl/wslPathParsing.test.ts +++ b/apps/desktop/src/wsl/wslPathParsing.test.ts @@ -136,7 +136,19 @@ describe("resolveWslPickFolderDefaultPath", () => { ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project"); }); - it("maps tilde initial paths under the WSL home UNC path", () => { + it("expands ~/path against the user's home dir when known", () => { + expect( + resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros, "/home/josh"), + ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project"); + }); + + it("resolves bare ~ to the user's home dir when known", () => { + expect( + resolveWslPickFolderDefaultPath({ initialPath: "~" }, config, distros, "/home/josh"), + ).toBe("\\\\wsl.localhost\\Debian\\home\\josh"); + }); + + it("falls back to /home parent when the user's home dir isn't known", () => { expect(resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros)).toBe( "\\\\wsl.localhost\\Debian\\home\\project", ); diff --git a/apps/desktop/src/wsl/wslPathParsing.ts b/apps/desktop/src/wsl/wslPathParsing.ts index e0b995fd76..d1ecc484cf 100644 --- a/apps/desktop/src/wsl/wslPathParsing.ts +++ b/apps/desktop/src/wsl/wslPathParsing.ts @@ -63,6 +63,11 @@ export function resolveWslPickFolderDefaultPath( rawOptions: unknown, config: WslConfig, distros: readonly WslDistro[], + // Absolute Linux path of the user's home dir inside the chosen distro + // (e.g. "/home/josh"). When known, `~` and `~/...` expand against this so + // we don't open the picker at a non-existent `/home/`. When null we + // fall back to the `/home` parent — wrong directory but at least it exists. + userHome: string | null = null, ): string | null { const homePath = resolveWslHomeUncPath(config, distros); if (typeof rawOptions !== "object" || rawOptions === null) { @@ -90,11 +95,17 @@ export function resolveWslPickFolderDefaultPath( const toUncPath = (linuxPath: string) => `\\\\wsl.localhost\\${distroName}${linuxPath.replaceAll("/", "\\")}`; + const normalizedUserHome = userHome && userHome.startsWith("/") ? userHome : null; + if (trimmedPath === "~") { - return homePath; + return normalizedUserHome ? toUncPath(normalizedUserHome) : homePath; } if (trimmedPath.startsWith("~/")) { - return homePath ? `${homePath}\\${trimmedPath.slice(2).replaceAll("/", "\\")}` : null; + const remainder = trimmedPath.slice(2); + if (normalizedUserHome) { + return toUncPath(`${normalizedUserHome}/${remainder}`); + } + return homePath ? `${homePath}\\${remainder.replaceAll("/", "\\")}` : null; } if (trimmedPath.startsWith("/")) { return toUncPath(trimmedPath); diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 9e93b06797..96538087ee 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -2474,8 +2474,15 @@ export function ConnectionsSettings() { const renderWslRow = () => { if (!desktopWslState || !desktopWslState.available) return null; - const currentValue = - desktopWslState.mode === "wsl" ? (desktopWslState.distro ?? "__default__") : "__local__"; + // When distro is null ("track the WSL default"), map to the actual default + // distro's name if we know one so the Select highlights a real option + // instead of an orphan "__default__" with no matching item. Falls back to + // "__default__" only when no distros are listed yet — in that case the + // dropdown renders a single placeholder option that matches. + const defaultDistroName = + desktopWslState.distros.find((distro) => distro.isDefault)?.name ?? null; + const wslSelectValue = desktopWslState.distro ?? defaultDistroName ?? "__default__"; + const currentValue = desktopWslState.mode === "wsl" ? wslSelectValue : "__local__"; const currentLabel = currentValue === "__local__" ? "Local (Windows)"