diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index 23f8adfbea..de9327b9cf 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -4,6 +4,7 @@ import { DesktopEnvironmentBootstrapSchema, DesktopThemeSchema, PickFolderOptionsSchema, + PRIMARY_LOCAL_ENVIRONMENT_ID, type DesktopEnvironmentBootstrap, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; @@ -76,6 +77,20 @@ export const getLocalEnvironmentBootstraps = makeSyncIpcMethod({ }), }); +const WSL_INSTANCE_ID_PREFIX = "wsl:"; + +// Pull the distro selection out of a backend instance id like +// "wsl:ubuntu". Returns null for "wsl:default", which is the sentinel +// for "track the user's WSL default distro" and maps to the +// wslEnv-derived default at picker time. +function extractWslDistroFromEnvironmentId(envId: string): string | null { + if (!envId.startsWith(WSL_INSTANCE_ID_PREFIX)) { + return null; + } + const suffix = envId.slice(WSL_INSTANCE_ID_PREFIX.length); + return suffix === "default" || suffix.length === 0 ? null : suffix; +} + export const pickFolder = makeIpcMethod({ channel: IpcChannels.PICK_FOLDER_CHANNEL, payload: Schema.UndefinedOr(PickFolderOptionsSchema), @@ -86,16 +101,36 @@ export const pickFolder = makeIpcMethod({ const environment = yield* DesktopEnvironment.DesktopEnvironment; const appSettings = yield* DesktopAppSettings.DesktopAppSettings; const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment; + // Three picker modes: + // - targetEnvironmentId omitted: default to the primary picker. Keeps + // the historical behavior unchanged for users who never enabled the + // WSL backend, and is what unfamiliar callers should get out of the + // box. + // - targetEnvironmentId starts with "wsl:": route to the WSL picker + // using the distro encoded in the id (or the user's selected + // wslDistro when the id is the "wsl:default" sentinel). + // - anything else (incl. PRIMARY_LOCAL_ENVIRONMENT_ID): primary picker. + const targetId = options?.targetEnvironmentId; + const wslDistroFromTarget = + targetId !== undefined && targetId.startsWith(WSL_INSTANCE_ID_PREFIX) + ? extractWslDistroFromEnvironmentId(targetId) + : null; + const useWsl = + targetId !== undefined && + targetId !== PRIMARY_LOCAL_ENVIRONMENT_ID && + targetId.startsWith(WSL_INSTANCE_ID_PREFIX); const settings = yield* appSettings.get; - const wslAvailable = yield* wslEnvironment.isAvailable; - const useWsl = settings.wslBackendEnabled && wslAvailable; + // Fall back to the persisted wslDistro when the id is the + // "wsl:default" sentinel; the orchestrator uses the same fallback + // for the actual backend. + const wslDistro = useWsl ? (wslDistroFromTarget ?? settings.wslDistro) : null; const defaultPath = useWsl ? Option.fromNullishOr( resolveWslPickFolderDefaultPath( options, - { distro: settings.wslDistro }, + { distro: wslDistro }, yield* wslEnvironment.listDistros, - Option.getOrNull(yield* wslEnvironment.getUserHome(settings.wslDistro)), + Option.getOrNull(yield* wslEnvironment.getUserHome(wslDistro)), ), ) : environment.resolvePickFolderDefaultPath(options); diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index cbd993a519..b59fb8e4d8 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -374,10 +374,17 @@ export const DesktopServerExposureStateSchema = Schema.Struct({ export interface PickFolderOptions { initialPath?: string | null; + // When set, the desktop dialog opens against the named backend's + // filesystem instead of the primary's. Used by callers that already + // know which local environment they're targeting (e.g. opening a + // project that lives inside WSL). Omitting it keeps the historical + // behavior so non-WSL users never see a different picker. + targetEnvironmentId?: string; } export const PickFolderOptionsSchema = Schema.Struct({ initialPath: Schema.optionalKey(Schema.NullOr(Schema.String)), + targetEnvironmentId: Schema.optionalKey(Schema.String), }); export interface DesktopWslDistro {