From fe77570b5b30e77da49402b47bbdf9f65ff49f8e Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Sun, 7 Jun 2026 12:56:48 -0400 Subject: [PATCH] fix(desktop): surface WSL preflight failures cleanly --- .../desktop/src/backend/DesktopBackendPool.ts | 15 +++++++---- apps/desktop/src/ipc/methods/wsl.ts | 10 ++++++- apps/desktop/src/wsl/DesktopWslBackend.ts | 27 ++++++++++++++++++- .../settings/ConnectionsSettings.tsx | 4 +++ apps/web/src/localApi.test.ts | 4 +++ packages/contracts/src/ipc.ts | 6 +++++ 6 files changed, 59 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/backend/DesktopBackendPool.ts b/apps/desktop/src/backend/DesktopBackendPool.ts index 976b788c41..20aaf70143 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.ts @@ -221,12 +221,17 @@ export const layer = Layer.effect( }); yield* electronDialog.showErrorBox( "WSL backend couldn't start", - `${reason}\n\nFalling back to the Windows backend so T3 Code can open. Re-enable "Run in WSL only" from Settings > Connections once the WSL distro is fixed.`, + `${reason}\n\nFalling back to the Windows backend so T3 Code can open. Re-enable the WSL backend from Settings > Connections once the WSL distro is fixed.`, ); - // Persist Windows mode so the manager's next restart re-resolves the - // primary as Windows (describePrimary reads wslOnly) and a window opens. - // Swallow a write failure — we still logged and surfaced the reason. - yield* appSettings.setWslOnly(false).pipe( + // Fully disable the WSL backend — both flags, matching the "Switch to + // Windows" recovery path — so the manager's next restart re-resolves the + // primary as Windows and reconcile won't register a secondary WSL backend + // against the same broken setup. Clearing wslBackendEnabled alone would + // leave a stale wslOnly:true that silently re-traps the user in wsl-only + // mode the next time they enable WSL. Swallow write failures — we still + // logged and surfaced the reason. + yield* appSettings.setWslBackendEnabled(false).pipe( + Effect.andThen(appSettings.setWslOnly(false)), Effect.catch((error) => logBackendPoolWarning("failed to persist Windows fallback after WSL preflight failure", { error: error.message, diff --git a/apps/desktop/src/ipc/methods/wsl.ts b/apps/desktop/src/ipc/methods/wsl.ts index 93c3434650..ce8c2db635 100644 --- a/apps/desktop/src/ipc/methods/wsl.ts +++ b/apps/desktop/src/ipc/methods/wsl.ts @@ -1,5 +1,6 @@ import { DesktopWslStateSchema, type DesktopWslState } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts"; @@ -12,21 +13,28 @@ import { makeIpcMethod } from "../DesktopIpc.ts"; const readWslState: Effect.Effect< DesktopWslState, never, - DesktopAppSettings.DesktopAppSettings | DesktopWslEnvironment.DesktopWslEnvironment + | DesktopAppSettings.DesktopAppSettings + | DesktopWslEnvironment.DesktopWslEnvironment + | DesktopWslBackend.DesktopWslBackend > = Effect.gen(function* () { const appSettings = yield* DesktopAppSettings.DesktopAppSettings; const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment; + const wslBackend = yield* DesktopWslBackend.DesktopWslBackend; const settings = yield* appSettings.get; const available = yield* wslEnvironment.isAvailable; // Only enumerate distros when WSL is actually available — listDistros on a // non-WSL host would spawn wsl.exe and hit the timeout for nothing. const distros = available ? yield* wslEnvironment.listDistros : []; + const preflightError = yield* wslBackend.lastPreflightError; return { enabled: settings.wslBackendEnabled, distro: settings.wslDistro, available, wslOnly: settings.wslOnly, distros, + // Only the dual-mode secondary records this; a wsl-only failure surfaces via + // a dialog + Windows fallback, so it stays null there. + preflightError: settings.wslOnly ? null : Option.getOrNull(preflightError), }; }); diff --git a/apps/desktop/src/wsl/DesktopWslBackend.ts b/apps/desktop/src/wsl/DesktopWslBackend.ts index 104bf6294a..d4dd165dcc 100644 --- a/apps/desktop/src/wsl/DesktopWslBackend.ts +++ b/apps/desktop/src/wsl/DesktopWslBackend.ts @@ -25,6 +25,7 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Ref from "effect/Ref"; import * as Semaphore from "effect/Semaphore"; import * as NetService from "@t3tools/shared/Net"; @@ -49,6 +50,11 @@ export interface DesktopWslBackendShape { // Idempotent. Never fails (errors are logged); callers can chain it // after persisting settings without an error-handling dance. readonly reconcile: Effect.Effect; + // Reason the dual-mode WSL secondary last failed preflight (no node, wrong + // version, missing build tools), or None. Read by the getWslState IPC so + // Connections settings can show it inline. None in wsl-only mode (that path + // surfaces via a dialog + Windows fallback). + readonly lastPreflightError: Effect.Effect>; } export class DesktopWslBackend extends Context.Service()( @@ -102,6 +108,13 @@ export const layer = Layer.effect( // with different distros, leaving the loser stranded. const reconcileMutex = yield* Semaphore.make(1); + // Last fatal preflight failure from the dual-mode WSL *secondary*, surfaced + // inline in Connections settings. The primary's failure is handled by the + // pool (dialog + Windows fallback) instead; here the app stays usable on + // Windows, so we record the reason rather than interrupting. Cleared on any + // reconcile state change so it reflects the current attempt. + const preflightErrorRef = yield* Ref.make(Option.none()); + const findExistingWslInstance = pool.list.pipe( Effect.map((instances) => instances.find((instance) => isWslInstanceId(instance.id))), Effect.map(Option.fromNullishOr), @@ -150,6 +163,10 @@ export const layer = Layer.effect( id: targetId, label: Effect.succeed(buildLabel(input.distro)), configResolve: configuration.resolveWsl({ port: allocatedPort, distro: input.distro }), + // Dual-mode secondary: record a fatal preflight failure so Connections + // settings can show why the WSL backend never appeared. No dialog or + // fallback — Windows is the primary and keeps working. + onPreflightFailed: (reason) => Ref.set(preflightErrorRef, Option.some(reason)), }) .pipe( Effect.map((registered) => Option.some(registered)), @@ -195,6 +212,11 @@ export const layer = Layer.effect( return; } + // A real state change is happening (start, stop, or distro swap). Clear + // any stale secondary preflight error so it reflects this fresh attempt; + // onPreflightFailed re-sets it only if the new secondary exhausts retries. + yield* Ref.set(preflightErrorRef, Option.none()); + if (Option.isSome(existingId)) { yield* logWslBackendInfo("tearing down WSL backend", { id: existingId.value }); yield* stopExisting(existingId.value); @@ -227,6 +249,9 @@ export const layer = Layer.effect( Effect.withSpan("desktop.wslBackend.reconcile"), ); - return DesktopWslBackend.of({ reconcile }); + return DesktopWslBackend.of({ + reconcile, + lastPreflightError: Ref.get(preflightErrorRef), + }); }), ); diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index f0c941a563..65af16e54c 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -3031,6 +3031,10 @@ export function ConnectionsSettings() { status={ desktopWslError ? ( {desktopWslError} + ) : desktopWslState.preflightError ? ( + + WSL backend couldn't start: {desktopWslState.preflightError} + ) : null } control={ diff --git a/apps/web/src/localApi.test.ts b/apps/web/src/localApi.test.ts index 2fe41b3bb3..381965e7d9 100644 --- a/apps/web/src/localApi.test.ts +++ b/apps/web/src/localApi.test.ts @@ -235,6 +235,7 @@ function makeDesktopBridge(overrides: Partial = {}): DesktopBridg available: false, wslOnly: false, distros: [], + preflightError: null, }), setWslBackendEnabled: async () => ({ enabled: false, @@ -242,6 +243,7 @@ function makeDesktopBridge(overrides: Partial = {}): DesktopBridg available: false, wslOnly: false, distros: [], + preflightError: null, }), setWslDistro: async () => ({ enabled: false, @@ -249,6 +251,7 @@ function makeDesktopBridge(overrides: Partial = {}): DesktopBridg available: false, wslOnly: false, distros: [], + preflightError: null, }), setWslOnly: async () => ({ enabled: false, @@ -256,6 +259,7 @@ function makeDesktopBridge(overrides: Partial = {}): DesktopBridg available: false, wslOnly: false, distros: [], + preflightError: null, }), pickFolder: async () => null, confirm: async () => true, diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 7a6ac9eeda..78826f7d6f 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -421,6 +421,11 @@ export interface DesktopWslState { // primary backend's spec is captured once at layer init. wslOnly: boolean; distros: readonly DesktopWslDistro[]; + // Reason the dual-mode WSL backend last failed preflight (no node, wrong + // version, missing build tools), or null. Surfaced inline in Connections + // settings. Always null in wsl-only mode — that path shows a dialog and + // falls back to Windows instead. + preflightError: string | null; } export const DesktopWslStateSchema = Schema.Struct({ @@ -429,6 +434,7 @@ export const DesktopWslStateSchema = Schema.Struct({ available: Schema.Boolean, wslOnly: Schema.Boolean, distros: Schema.Array(DesktopWslDistroSchema), + preflightError: Schema.NullOr(Schema.String), }); export const DesktopCloudAuthFetchInputSchema = Schema.Struct({