diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts index d3c419798c..eee2aef34d 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts @@ -242,7 +242,7 @@ describe("DesktopBackendConfiguration", () => { Layer.provideMerge( DesktopAppSettings.layerTest({ ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS, - wslMode: "wsl", + wslBackendEnabled: true, }), ), Layer.provideMerge( diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.ts index 678603b476..0041e9767e 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.ts @@ -143,7 +143,7 @@ const getOrCreateBootstrapToken = Effect.fn("desktop.backendConfiguration.bootst interface ResolveBackendStartConfigInput { readonly bootstrapToken: string; readonly observabilitySettings: BackendObservabilitySettings; - readonly wslMode: "local" | "wsl"; + readonly wslBackendEnabled: boolean; readonly wslDistro: string | null; } @@ -222,7 +222,7 @@ const resolveBackendStartConfig = Effect.fn("desktop.backendConfiguration.resolv const serverExposure = yield* DesktopServerExposure.DesktopServerExposure; const backendExposure = yield* serverExposure.backendConfig; - const useWsl = input.wslMode === "wsl" && environment.platform === "win32"; + const useWsl = input.wslBackendEnabled && environment.platform === "win32"; const bootstrap = { mode: "desktop" as const, @@ -369,7 +369,7 @@ export const layer = Layer.effect( return yield* resolveBackendStartConfig({ bootstrapToken, observabilitySettings, - wslMode: settings.wslMode, + wslBackendEnabled: settings.wslBackendEnabled, wslDistro: settings.wslDistro, }).pipe( Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment), diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index ca10b1c9cd..1721ddac3f 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -80,7 +80,7 @@ export const pickFolder = makeIpcMethod({ const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment; const settings = yield* appSettings.get; const wslAvailable = yield* wslEnvironment.isAvailable; - const useWsl = settings.wslMode === "wsl" && wslAvailable; + const useWsl = settings.wslBackendEnabled && wslAvailable; const defaultPath = useWsl ? Option.fromNullishOr( resolveWslPickFolderDefaultPath( diff --git a/apps/desktop/src/ipc/methods/wsl.ts b/apps/desktop/src/ipc/methods/wsl.ts index 0e6bb0499c..9990fb6dbb 100644 --- a/apps/desktop/src/ipc/methods/wsl.ts +++ b/apps/desktop/src/ipc/methods/wsl.ts @@ -42,7 +42,7 @@ const readWslState: Effect.Effect< // non-WSL host would spawn wsl.exe and hit the timeout for nothing. const distros = available ? yield* wslEnvironment.listDistros : []; return { - mode: settings.wslMode, + mode: settings.wslBackendEnabled ? "wsl" : "local", distro: settings.wslDistro, available, distros, @@ -75,31 +75,31 @@ export const setWslBackend = makeIpcMethod({ } const previousSettings = yield* appSettings.get; - const change = yield* appSettings.setWslMode({ mode: input.mode, distro: input.distro }); + const targetEnabled = input.mode === "wsl"; + const enabledChange = yield* appSettings.setWslBackendEnabled(targetEnabled); + const distroChange = yield* appSettings.setWslDistro(input.distro); - if (!change.changed) { + if (!enabledChange.changed && !distroChange.changed) { return yield* readWslState; } // In-process swap: stop the running backend, then start it again. The - // backend instance re-resolves config on start, so the new wslMode picks + // backend instance re-resolves config on start, so the new toggle picks // up automatically. yield* primaryBackend.stop(); yield* primaryBackend.start; // Bounded readiness wait — if the new backend doesn't come up in time // (bad distro, missing node-pty, preflight failure that scheduled - // restarts forever) revert to the previous mode so the user isn't stuck. + // restarts forever) revert to the previous toggle so the user isn't stuck. const ready = yield* primaryBackend.waitForReady(SWAP_READINESS_TIMEOUT); if (!ready) { - yield* appSettings.setWslMode({ - mode: previousSettings.wslMode, - distro: previousSettings.wslDistro, - }); + yield* appSettings.setWslBackendEnabled(previousSettings.wslBackendEnabled); + yield* appSettings.setWslDistro(previousSettings.wslDistro); yield* primaryBackend.stop(); yield* primaryBackend.start; const rolledBack = yield* primaryBackend.waitForReady(SWAP_READINESS_TIMEOUT); - const failedTarget = input.mode === "wsl" ? "WSL backend" : "local backend"; + const failedTarget = targetEnabled ? "WSL backend" : "local backend"; return yield* new WslBackendSwapError({ message: rolledBack ? `The ${failedTarget} didn't come up. Rolled back to the previous mode — check that the chosen distro is healthy and try again.` diff --git a/apps/desktop/src/settings/DesktopAppSettings.test.ts b/apps/desktop/src/settings/DesktopAppSettings.test.ts index 15d11b8286..ffcff21582 100644 --- a/apps/desktop/src/settings/DesktopAppSettings.test.ts +++ b/apps/desktop/src/settings/DesktopAppSettings.test.ts @@ -21,6 +21,7 @@ const DesktopSettingsPatch = Schema.Struct({ tailscaleServePort: Schema.optionalKey(Schema.Number), updateChannel: Schema.optionalKey(Schema.Literals(["latest", "nightly"])), updateChannelConfiguredByUser: Schema.optionalKey(Schema.Boolean), + wslBackendEnabled: Schema.optionalKey(Schema.Boolean), wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])), wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)), }); @@ -97,7 +98,7 @@ describe("DesktopSettings", () => { tailscaleServePort: 443, updateChannel: "nightly", updateChannelConfiguredByUser: false, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, } satisfies DesktopSettingsValue); }); @@ -120,7 +121,7 @@ describe("DesktopSettings", () => { tailscaleServePort: 8443, updateChannel: "latest", updateChannelConfiguredByUser: true, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, } satisfies DesktopSettingsValue); @@ -201,7 +202,7 @@ describe("DesktopSettings", () => { tailscaleServePort: 8443, updateChannel: "latest", updateChannelConfiguredByUser: false, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, } satisfies DesktopSettingsValue); }), @@ -242,7 +243,7 @@ describe("DesktopSettings", () => { tailscaleServePort: 443, updateChannel: "nightly", updateChannelConfiguredByUser: false, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, } satisfies DesktopSettingsValue); }), @@ -266,7 +267,7 @@ describe("DesktopSettings", () => { tailscaleServePort: 443, updateChannel: "latest", updateChannelConfiguredByUser: true, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, } satisfies DesktopSettingsValue); }), @@ -289,45 +290,63 @@ describe("DesktopSettings", () => { tailscaleServePort: 443, updateChannel: "latest", updateChannelConfiguredByUser: false, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, } satisfies DesktopSettingsValue); }), ), ); - it.effect("persists wsl mode and normalizes invalid distro names", () => + it.effect("persists wsl backend toggle and normalizes invalid distro names", () => withSettings( Effect.gen(function* () { const settings = yield* DesktopAppSettings.DesktopAppSettings; - const enable = yield* settings.setWslMode({ mode: "wsl", distro: "Ubuntu-22.04" }); + const enable = yield* settings.setWslBackendEnabled(true); assert.isTrue(enable.changed); - assert.equal(enable.settings.wslMode, "wsl"); - assert.equal(enable.settings.wslDistro, "Ubuntu-22.04"); + assert.equal(enable.settings.wslBackendEnabled, true); + + const distro = yield* settings.setWslDistro("Ubuntu-22.04"); + assert.isTrue(distro.changed); + assert.equal(distro.settings.wslDistro, "Ubuntu-22.04"); const reloaded = yield* settings.load; - assert.equal(reloaded.wslMode, "wsl"); + assert.equal(reloaded.wslBackendEnabled, true); assert.equal(reloaded.wslDistro, "Ubuntu-22.04"); - const reject = yield* settings.setWslMode({ mode: "wsl", distro: "bad name!" }); + const reject = yield* settings.setWslDistro("bad name!"); assert.equal(reject.settings.wslDistro, null); - const noop = yield* settings.setWslMode({ mode: "wsl", distro: null }); + const noop = yield* settings.setWslDistro(null); assert.isFalse(noop.changed); }), ), ); + it.effect("migrates legacy wslMode=wsl to wslBackendEnabled on load", () => + withSettings( + Effect.gen(function* () { + const settings = yield* DesktopAppSettings.DesktopAppSettings; + yield* writeSettingsPatch({ + wslMode: "wsl", + wslDistro: "Ubuntu-22.04", + }); + const loaded = yield* settings.load; + assert.equal(loaded.wslBackendEnabled, true); + assert.equal(loaded.wslDistro, "Ubuntu-22.04"); + }), + ), + ); + it.effect("drops invalid persisted wsl distro values on load", () => withSettings( Effect.gen(function* () { const settings = yield* DesktopAppSettings.DesktopAppSettings; yield* writeSettingsPatch({ - wslMode: "wsl", + wslBackendEnabled: true, wslDistro: "bad/name", }); const loaded = yield* settings.load; - assert.equal(loaded.wslMode, "wsl"); + assert.equal(loaded.wslBackendEnabled, true); assert.equal(loaded.wslDistro, null); }), ), diff --git a/apps/desktop/src/settings/DesktopAppSettings.ts b/apps/desktop/src/settings/DesktopAppSettings.ts index bde4b1c4f8..f1f2d290d9 100644 --- a/apps/desktop/src/settings/DesktopAppSettings.ts +++ b/apps/desktop/src/settings/DesktopAppSettings.ts @@ -1,10 +1,8 @@ import { DesktopServerExposureModeSchema, DesktopUpdateChannelSchema, - DesktopWslModeSchema, type DesktopServerExposureMode, type DesktopUpdateChannel, - type DesktopWslMode, } from "@t3tools/contracts"; import { fromLenientJson } from "@t3tools/shared/schemaJson"; import * as Context from "effect/Context"; @@ -29,7 +27,12 @@ export interface DesktopSettings { readonly tailscaleServePort: number; readonly updateChannel: DesktopUpdateChannel; readonly updateChannelConfiguredByUser: boolean; - readonly wslMode: DesktopWslMode; + // Was a "local" | "wsl" swap mode in an earlier iteration of the WSL + // integration. We now run Windows and WSL backends side by side, so the + // setting is just whether the WSL backend should be running alongside the + // primary. Persisted documents that still carry the legacy `wslMode: "wsl"` + // value are migrated to `wslBackendEnabled: true` on load. + readonly wslBackendEnabled: boolean; readonly wslDistro: string | null; } @@ -46,7 +49,7 @@ export const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = { tailscaleServePort: DEFAULT_TAILSCALE_SERVE_PORT, updateChannel: "latest", updateChannelConfiguredByUser: false, - wslMode: "local", + wslBackendEnabled: false, wslDistro: null, }; @@ -56,7 +59,11 @@ const DesktopSettingsDocument = Schema.Struct({ tailscaleServePort: Schema.optionalKey(Schema.Number), updateChannel: Schema.optionalKey(DesktopUpdateChannelSchema), updateChannelConfiguredByUser: Schema.optionalKey(Schema.Boolean), - wslMode: Schema.optionalKey(DesktopWslModeSchema), + // Newer form of the WSL toggle. `wslMode` is still accepted on load so + // existing on-disk settings keep working; on the next persist we write the + // new boolean and the legacy key drops out. + wslBackendEnabled: Schema.optionalKey(Schema.Boolean), + wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])), wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)), }); @@ -93,10 +100,12 @@ export interface DesktopAppSettingsShape { readonly setUpdateChannel: ( channel: DesktopUpdateChannel, ) => Effect.Effect; - readonly setWslMode: (input: { - readonly mode: DesktopWslMode; - readonly distro: string | null; - }) => Effect.Effect; + readonly setWslBackendEnabled: ( + enabled: boolean, + ) => Effect.Effect; + readonly setWslDistro: ( + distro: string | null, + ) => Effect.Effect; } export class DesktopAppSettings extends Context.Service< @@ -132,6 +141,13 @@ function normalizeDesktopSettingsDocument( parsed.updateChannelConfiguredByUser === true || (isLegacySettings && Option.contains(parsedUpdateChannel, "nightly")); + // Newer form wins when both are present; otherwise fall back to the legacy + // `wslMode === "wsl"` signal so users coming off the swap-mode build keep + // their WSL backend enabled. + const wslBackendEnabled = + parsed.wslBackendEnabled === true || + (parsed.wslBackendEnabled === undefined && parsed.wslMode === "wsl"); + return { serverExposureMode: parsed.serverExposureMode === "network-accessible" ? "network-accessible" : "local-only", @@ -141,7 +157,7 @@ function normalizeDesktopSettingsDocument( ? Option.getOrElse(parsedUpdateChannel, () => defaultSettings.updateChannel) : defaultSettings.updateChannel, updateChannelConfiguredByUser, - wslMode: parsed.wslMode === "wsl" ? "wsl" : "local", + wslBackendEnabled, wslDistro: normalizeWslDistro(parsed.wslDistro), }; } @@ -167,8 +183,8 @@ function toDesktopSettingsDocument( if (settings.updateChannelConfiguredByUser !== defaults.updateChannelConfiguredByUser) { document.updateChannelConfiguredByUser = settings.updateChannelConfiguredByUser; } - if (settings.wslMode !== defaults.wslMode) { - document.wslMode = settings.wslMode; + if (settings.wslBackendEnabled !== defaults.wslBackendEnabled) { + document.wslBackendEnabled = settings.wslBackendEnabled; } if (settings.wslDistro !== defaults.wslDistro) { document.wslDistro = settings.wslDistro; @@ -219,17 +235,22 @@ function setUpdateChannel( }; } -function setWslMode( - settings: DesktopSettings, - input: { readonly mode: DesktopWslMode; readonly distro: string | null }, -): DesktopSettings { - const distro = normalizeWslDistro(input.distro); - return settings.wslMode === input.mode && settings.wslDistro === distro +function setWslBackendEnabled(settings: DesktopSettings, enabled: boolean): DesktopSettings { + return settings.wslBackendEnabled === enabled ? settings : { ...settings, - wslMode: input.mode, - wslDistro: distro, + wslBackendEnabled: enabled, + }; +} + +function setWslDistro(settings: DesktopSettings, distro: string | null): DesktopSettings { + const normalized = normalizeWslDistro(distro); + return settings.wslDistro === normalized + ? settings + : { + ...settings, + wslDistro: normalized, }; } @@ -324,10 +345,14 @@ export const layer = Layer.effect( persist((settings) => setUpdateChannel(settings, channel)).pipe( Effect.withSpan("desktop.settings.setUpdateChannel", { attributes: { channel } }), ), - setWslMode: (input) => - persist((settings) => setWslMode(settings, input)).pipe( - Effect.withSpan("desktop.settings.setWslMode", { - attributes: { mode: input.mode, distro: input.distro ?? null }, + setWslBackendEnabled: (enabled) => + persist((settings) => setWslBackendEnabled(settings, enabled)).pipe( + Effect.withSpan("desktop.settings.setWslBackendEnabled", { attributes: { enabled } }), + ), + setWslDistro: (distro) => + persist((settings) => setWslDistro(settings, distro)).pipe( + Effect.withSpan("desktop.settings.setWslDistro", { + attributes: { distro: distro ?? null }, }), ), }); @@ -358,7 +383,9 @@ export const layerTest = (initialSettings: DesktopSettings = DEFAULT_DESKTOP_SET update((settings) => setServerExposureMode(settings, mode)), setTailscaleServe: (input) => update((settings) => setTailscaleServe(settings, input)), setUpdateChannel: (channel) => update((settings) => setUpdateChannel(settings, channel)), - setWslMode: (input) => update((settings) => setWslMode(settings, input)), + setWslBackendEnabled: (enabled) => + update((settings) => setWslBackendEnabled(settings, enabled)), + setWslDistro: (distro) => update((settings) => setWslDistro(settings, distro)), }); }), );