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.
This commit is contained in:
Neil
2026-11-02 03:10:25 -08:00
committed by GitHub
parent ac4a21f3d2
commit 499da04f10
5 changed files with 63 additions and 30 deletions
+8 -1
View File
@@ -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()
})
+7 -2
View File
@@ -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) => {
+30 -25
View File
@@ -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<ReturnType<typeof fakeWslHostsSchema>>
export function readFakeWslHostsConfig(
raw = process.env.ORCA_E2E_FAKE_WSL_HOSTS
): z.infer<typeof FakeWslHostsSchema> | 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<typeof FakeWslHostsSchema>,
config: FakeWslHostsConfig,
onChange: NonNullable<WslOrcadConnectionDependencies['onChange']>,
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<string, WslOrcadConnection>()
const key = (distro: string): string => distro.toLowerCase()
const readDistros = (): z.infer<typeof FakeDistrosSchema> =>
const readDistros = (): z.infer<ReturnType<typeof fakeDistrosSchema>> =>
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 =>
+15 -1
View File
@@ -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()
+3 -1
View File
@@ -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
}