From 5d80468d7edfa8fbd2c1d1a057d74f252db43054 Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Sun, 17 May 2026 13:21:51 -0400 Subject: [PATCH] feat(desktop): route pickFolder by target environment id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PickFolderOptions gains an optional targetEnvironmentId so callers that know which local backend they're targeting (a project opened in WSL, for example) can ask for that backend's filesystem picker. The default behavior is unchanged: when targetEnvironmentId is undefined, the dialog opens against the Windows-native primary, which is what every existing caller gets. This is deliberate — most users never enable the WSL backend and shouldn't see a different picker showing up. Only callers that explicitly opt in route to WSL. When targetEnvironmentId starts with "wsl:", the handler uses the WSL helpers in wslPathParsing.ts. The id encodes the distro selection (e.g. "wsl:ubuntu") and falls back to the persisted wslDistro setting when the id is the "wsl:default" sentinel, matching how DesktopWslBackend.reconcile resolves the same input. The legacy "if wslBackendEnabled then always use WSL picker" branch is gone — that was the swap-mode mental model. --- apps/desktop/src/ipc/methods/window.ts | 43 +++++++++++++++++++++++--- packages/contracts/src/ipc.ts | 7 +++++ 2 files changed, 46 insertions(+), 4 deletions(-) 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 {