From 499da04f1002fc41310ebe8b4cc1d3ef705ae651 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 2 Nov 2026 03:10:25 -0800 Subject: [PATCH] fix(wsl-hosts): keep the e2e fake WSL connector out of release builds (#37378) * fix(wsl-hosts): keep the e2e fake WSL connector out of release builds The fake WSL host connector was compiled into every build and honored whenever ORCA_E2E_FAKE_WSL_HOSTS was set. Gate main and preload on import.meta.env.MODE === 'e2e' (the mode every e2e build already uses), so other builds fold the branch away and drop the module, and mark its module-level schemas pure so none of it is retained. * fix(wsl-hosts): build the e2e fake's zod schemas lazily so release builds drop them Eager module-level zod chains survived tree-shaking even with a PURE annotation on the outer call, so the fake's schema construction still shipped in out/main/index.js. --- src/main/ipc/wsl-host-gate-off.test.ts | 9 +++- src/main/ipc/wsl-host-handlers.ts | 9 +++- src/main/wsl/wsl-host-e2e-fake-connector.ts | 55 +++++++++++---------- src/preload/api/wsl-hosts-api.test.ts | 16 +++++- src/preload/api/wsl-hosts-api.ts | 4 +- 5 files changed, 63 insertions(+), 30 deletions(-) diff --git a/src/main/ipc/wsl-host-gate-off.test.ts b/src/main/ipc/wsl-host-gate-off.test.ts index 7f842b4a260..94f027bd49f 100644 --- a/src/main/ipc/wsl-host-gate-off.test.ts +++ b/src/main/ipc/wsl-host-gate-off.test.ts @@ -202,6 +202,7 @@ describe('WSL hosts with the internal gate closed', () => { }) it('creates the WSL host service only once the gate is open', () => { + vi.stubEnv('MODE', 'e2e') vi.stubEnv(WSL_HOSTS_INTERNAL_GATE_ENV, '1') vi.stubEnv( 'ORCA_E2E_FAKE_WSL_HOSTS', @@ -225,8 +226,13 @@ describe('WSL hosts with the internal gate closed', () => { expect(registeredWslHostChannels().sort()).toEqual([...WSL_HOST_HANDLER_CHANNELS].sort()) }) - it('registers nothing with the gate open on a non-Windows host without the fake', () => { + it.each([ + ['without the fake', ''], + ['with the fake outside an e2e build', JSON.stringify({ distros: [], pairingCode: 'x' })] + ])('registers nothing with the gate open on a non-Windows host %s', (_label, fake) => { + vi.stubEnv('MODE', 'production') vi.stubEnv(WSL_HOSTS_INTERNAL_GATE_ENV, '1') + vi.stubEnv('ORCA_E2E_FAKE_WSL_HOSTS', fake) const platform = Object.getOwnPropertyDescriptor(process, 'platform') Object.defineProperty(process, 'platform', { value: 'linux' }) try { @@ -238,6 +244,7 @@ describe('WSL hosts with the internal gate closed', () => { } expect(getWslHostService()).toBeNull() + expect(mocks.createFakeConnector).not.toHaveBeenCalled() expect(registeredWslHostChannels()).toEqual([]) expect(store.onSettingsChanged).not.toHaveBeenCalled() }) diff --git a/src/main/ipc/wsl-host-handlers.ts b/src/main/ipc/wsl-host-handlers.ts index bff9cc45ca7..a42cc5955ab 100644 --- a/src/main/ipc/wsl-host-handlers.ts +++ b/src/main/ipc/wsl-host-handlers.ts @@ -109,7 +109,8 @@ function createService(args: WslHostHandlerArgs): WslHostService | null { if (!wslHostsServing()) { return null } - const fake = readFakeWslHostsConfig() + // Inline literal so non-e2e builds fold `fake` to null and drop the fake connector module. + const fake = import.meta.env.MODE === 'e2e' ? readFakeWslHostsConfig() : null if (process.platform !== 'win32' && !fake) { return null } @@ -182,7 +183,11 @@ function startService(args: WslHostHandlerArgs): WslHostService | null { return null } setWslHostService(created) - if (process.platform === 'win32' && !readFakeWslHostsConfig() && created.hasServerFiles()) { + if ( + process.platform === 'win32' && + !(import.meta.env.MODE === 'e2e' && readFakeWslHostsConfig()) && + created.hasServerFiles() + ) { wireWslNodeRuntimePrefetch({ store: args.store, userDataPath: args.getUserDataPath() }) } args.store.onSettingsChanged((updates, settings) => { diff --git a/src/main/wsl/wsl-host-e2e-fake-connector.ts b/src/main/wsl/wsl-host-e2e-fake-connector.ts index 87c7b73d80a..29c70934669 100644 --- a/src/main/wsl/wsl-host-e2e-fake-connector.ts +++ b/src/main/wsl/wsl-host-e2e-fake-connector.ts @@ -12,39 +12,44 @@ import { type WslHostRuntimeAccess } from './wsl-host-server-stop' -const FakeDistrosSchema = z.array( - z.object({ - name: z.string().min(1), - state: z.enum(['running', 'stopped']), - version: z.union([z.literal(1), z.literal(2)]) - }) -) +// Lazy: eager zod chains survive tree-shaking, so module-level schemas would ship in release builds. +const fakeDistrosSchema = () => + z.array( + z.object({ + name: z.string().min(1), + state: z.enum(['running', 'stopped']), + version: z.union([z.literal(1), z.literal(2)]) + }) + ) -const FakeWslHostsSchema = z.object({ - distros: FakeDistrosSchema, - /** Re-read on every listing, so a test can register a distro while the app runs, or stop one: - * a distro marked `stopped` loses its bridge, and only a booting connect starts it again. */ - distrosFile: z.string().optional(), - pairingCode: z.string().min(1), - /** Each distro the fake is asked to start, one per line, so e2e can prove nothing respawns. */ - connectLogFile: z.string().optional(), - /** `missing` stands in for a build shipped without the orcad template. */ - serverFiles: z.enum(['present', 'missing']).optional(), - /** A stop the fake does not report, as when liveness sees only a quick reboot: a runtime call must. */ - stopSilently: z.boolean().optional() -}) +const fakeWslHostsSchema = () => + z.object({ + distros: fakeDistrosSchema(), + /** Re-read on every listing, so a test can register a distro while the app runs, or stop one: + * a distro marked `stopped` loses its bridge, and only a booting connect starts it again. */ + distrosFile: z.string().optional(), + pairingCode: z.string().min(1), + /** Each distro the fake is asked to start, one per line, so e2e can prove nothing respawns. */ + connectLogFile: z.string().optional(), + /** `missing` stands in for a build shipped without the orcad template. */ + serverFiles: z.enum(['present', 'missing']).optional(), + /** A stop the fake does not report, as when liveness sees only a quick reboot: a runtime call must. */ + stopSilently: z.boolean().optional() + }) + +type FakeWslHostsConfig = z.infer> export function readFakeWslHostsConfig( raw = process.env.ORCA_E2E_FAKE_WSL_HOSTS -): z.infer | null { +): FakeWslHostsConfig | null { if (!raw) { return null } - return FakeWslHostsSchema.parse(JSON.parse(raw)) + return fakeWslHostsSchema().parse(JSON.parse(raw)) } export function createFakeWslHostConnector( - config: z.infer, + config: FakeWslHostsConfig, onChange: NonNullable, options: { isKnownDistro: (distro: string) => boolean; runtime: WslHostRuntimeAccess } ): WslHostConnector { @@ -52,9 +57,9 @@ export function createFakeWslHostConnector( const port = offer ? Number(new URL(offer.endpoint).port) : 0 const active = new Map() const key = (distro: string): string => distro.toLowerCase() - const readDistros = (): z.infer => + const readDistros = (): z.infer> => config.distrosFile - ? FakeDistrosSchema.parse(JSON.parse(readFileSync(config.distrosFile, 'utf8'))) + ? fakeDistrosSchema().parse(JSON.parse(readFileSync(config.distrosFile, 'utf8'))) : config.distros // Only a distros file can stop a distro, so fixed listings keep their always-running hosts. const isStopped = (distro: string): boolean => diff --git a/src/preload/api/wsl-hosts-api.test.ts b/src/preload/api/wsl-hosts-api.test.ts index 168e62d1a04..03aa93c6860 100644 --- a/src/preload/api/wsl-hosts-api.test.ts +++ b/src/preload/api/wsl-hosts-api.test.ts @@ -1,10 +1,24 @@ -import { describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('electron', () => ({ ipcRenderer: { invoke: vi.fn(), on: vi.fn() } })) import { getWslHostsApi } from './wsl-hosts-api' describe('WSL hosts preload API', () => { + beforeEach(() => { + vi.stubEnv('MODE', 'e2e') + }) + afterEach(() => { + vi.unstubAllEnvs() + }) + + it('ignores the e2e fake outside an e2e build', () => { + vi.stubEnv('MODE', 'production') + const env = { ORCA_INTERNAL_WSL_HOSTS: '1', ORCA_E2E_FAKE_WSL_HOSTS: '{}' } + expect(getWslHostsApi(env, 'linux')).toBeUndefined() + expect(getWslHostsApi(env, 'win32')).toBeDefined() + }) + it('is absent on Windows and under the e2e fake while the internal gate is closed', () => { expect(getWslHostsApi({}, 'win32', [])).toBeUndefined() expect(getWslHostsApi({ ORCA_INTERNAL_WSL_HOSTS: 'true' }, 'win32', [])).toBeUndefined() diff --git a/src/preload/api/wsl-hosts-api.ts b/src/preload/api/wsl-hosts-api.ts index 311b7007966..f90511203fe 100644 --- a/src/preload/api/wsl-hosts-api.ts +++ b/src/preload/api/wsl-hosts-api.ts @@ -67,5 +67,7 @@ export function getWslHostsApi( if (!isWslHostsServingForWindow(env, argv)) { return undefined } - return platform === 'win32' || env.ORCA_E2E_FAKE_WSL_HOSTS ? wslHostsApi : undefined + // The fake is only honored in `--mode e2e` builds, matching main. + const e2eFake = import.meta.env.MODE === 'e2e' && Boolean(env.ORCA_E2E_FAKE_WSL_HOSTS) + return platform === 'win32' || e2eFake ? wslHostsApi : undefined }