fix(desktop): resolve WSL user home for tilde paths, fix select value mapping, surface failed rollback

- map null distro to the actual default distro name in the backend select
  so the dropdown highlights a real option instead of an orphan "__default__"
  with no matching item when distros are listed
- add getUserHome to DesktopWslEnvironment (cached per distro) and pass the
  resolved /home/<user> into the picker helper so ~/path expands correctly
  instead of producing /home/<rest>
- surface a clearer error when the rollback backend also fails to start, so
  the user knows the app is degraded rather than seeing the misleading
  "Rolled back to the previous mode" message
This commit is contained in:
Josh Gratton committed 2026-05-16 11:38:36 -04:00
1 parent 80eddbbef9
commit 3b18e6dfe2
6 files changed
+101 -10

No files matched your search

+1
View File
@@ -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);
+5 -5
View File
@@ -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.`,
});
}
@@ -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<Option.Option<string>>;
// 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<Option.Option<string>>;
readonly ensureNodePty: (
distro: string | null,
windowsRepoRoot: string,
@@ -375,6 +379,44 @@ const windowsToWslPathImpl = (
);
};
const getUserHomeImpl = (
distro: string | null,
): Effect.Effect<Option.Option<string>, 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<string>();
const home = decodeUtf8(concatChunks(stdoutBytes)).trim();
return home.startsWith("/") ? Option.some(home) : Option.none<string>();
}),
).pipe(
Effect.timeoutOption(USER_HOME_TIMEOUT),
Effect.map(Option.flatten),
Effect.catch(() => Effect.succeed(Option.none<string>())),
);
const makeIsAvailable = (
platform: NodeJS.Platform,
windir: string,
@@ -391,6 +433,7 @@ export interface DesktopWslEnvironmentTestStub {
readonly isAvailable?: boolean;
readonly distros?: ReadonlyArray<WslDistro>;
readonly windowsToWslPath?: (distro: string | null, windowsPath: string) => Option.Option<string>;
readonly getUserHome?: (distro: string | null) => Option.Option<string>;
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<string>()),
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<string, string>();
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"),
+13 -1
View File
@@ -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",
);
+13 -2
View File
@@ -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/<rest>`. 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);
@@ -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)"