diff --git a/.github/workflows/daemon-relocation-spike.yml b/.github/workflows/daemon-relocation-spike.yml index 9eeeb5d03..aa7854e59 100644 --- a/.github/workflows/daemon-relocation-spike.yml +++ b/.github/workflows/daemon-relocation-spike.yml @@ -70,10 +70,14 @@ jobs: foreach ($tier in $tiers) { "===== TIER: $tier =====" | Tee-Object -FilePath $log -Append $work = Join-Path $env:RUNNER_TEMP "spike-$tier" + # --keep-work-dir so the per-tier daemon stdout/stderr logs survive + # for the artifact upload (the spike otherwise removes work-dir), + # which is what diagnoses a tier that fails to reach ready. node tools/daemon-relocation-spike/spike.mjs ` --app-dir dist/win-unpacked ` --work-dir "$work" ` - --tier $tier 2>&1 | Tee-Object -FilePath $log -Append + --tier $tier ` + --keep-work-dir 2>&1 | Tee-Object -FilePath $log -Append if ($LASTEXITCODE -eq 0) { $any = $true } } if (-not $any) { throw "No tier passed the relocation spike" } diff --git a/tools/daemon-relocation-spike/README.md b/tools/daemon-relocation-spike/README.md index dbc13946c..d7edd313a 100644 --- a/tools/daemon-relocation-spike/README.md +++ b/tools/daemon-relocation-spike/README.md @@ -69,12 +69,19 @@ a code change. ## How node-pty's native + ConPTY runtime is handled -The whole `node-pty` package tree is copied, mirroring its -`app.asar.unpacked/node_modules/node-pty` relative layout. That preserves two -resolutions from the relocated path: +The whole `node-pty` package tree is copied, and **the entire win-unpacked +layout is mirrored verbatim** (every copy destination is relative to the +win-unpacked root, not the asar-unpacked root). This matters because node-pty is +packaged at `resources/node_modules/node-pty` — a **sibling** of +`app.asar.unpacked`, not under it (see +`config/packaged-runtime-node-modules.cjs`). Mirroring the full layout preserves +two resolutions from the relocated path: -1. **The daemon require-closure** resolves `node-pty` by walking up from the - mirrored `daemon-entry.js` to `node_modules/node-pty`. +1. **The daemon require-closure** resolves `require('node-pty')` by walking + parent dirs up from the mirrored + `resources/app.asar.unpacked/out/main/daemon-entry.js`, which passes through + `resources/` and finds `resources/node_modules/node-pty` — exactly as in the + packaged app. 2. **node-pty's own native loader** resolves `conpty.node` from `build/Release` (or `prebuilds/win32-`) relative to node-pty's own `__dirname`, and node-pty's Windows addon loads `conpty.dll` from `/conpty/` diff --git a/tools/daemon-relocation-spike/host-copy.mjs b/tools/daemon-relocation-spike/host-copy.mjs index abce4a6a7..f95541a91 100644 --- a/tools/daemon-relocation-spike/host-copy.mjs +++ b/tools/daemon-relocation-spike/host-copy.mjs @@ -5,6 +5,7 @@ import { cpSync, existsSync, mkdirSync } from 'node:fs' import { dirname, join } from 'node:path' import { HOST_EXE } from './app-inventory.mjs' +import { toPosixRelative } from './tier-file-set.mjs' export const HOST_SUBDIR = 'daemon-host' @@ -43,19 +44,17 @@ export function copyHost(inv, plan, workDir) { } const hostExePath = join(hostRoot, HOST_EXE) - const daemonEntryPath = inv.daemonEntry.relFromUnpacked - ? destPath(hostRoot, inv.daemonEntry.relFromUnpacked) + // Both paths mirror the win-unpacked layout under hostRoot, so they resolve + // exactly as they do in the packaged app (relative to appDir). + const daemonEntryPath = inv.daemonEntry.exists + ? destPath(hostRoot, toPosixRelative(inv.appDir, inv.daemonEntry.path)) : '' // The relocated node-pty native dir (build/Release or prebuilds/...), mirrored - // under the host root at node-pty's unpacked-relative path. + // under the host root at node-pty's real win-unpacked-relative path. let nodePtyNativeDir = '' - if (inv.nodePty.exists && inv.nodePty.nativeRel && inv.unpackedRoot) { - const pkgRel = inv.nodePty.packageDir - .slice(inv.unpackedRoot.length) - .replace(/^[\\/]+/, '') - .split('\\') - .join('/') + if (inv.nodePty.exists && inv.nodePty.nativeRel) { + const pkgRel = toPosixRelative(inv.appDir, inv.nodePty.packageDir) nodePtyNativeDir = destPath(hostRoot, `${pkgRel}/${inv.nodePty.nativeRel}`) } diff --git a/tools/daemon-relocation-spike/selftest.mjs b/tools/daemon-relocation-spike/selftest.mjs index f972cf33f..520126716 100644 --- a/tools/daemon-relocation-spike/selftest.mjs +++ b/tools/daemon-relocation-spike/selftest.mjs @@ -33,10 +33,14 @@ function makeSyntheticInventory() { size: 1, relFromUnpacked: 'out/main/daemon-entry.js' }, + // node-pty is packaged at resources/node_modules/node-pty — a SIBLING of + // app.asar.unpacked, NOT under it (see packaged-runtime-node-modules.cjs). + // The synthetic inventory reflects that so the copy-plan test catches any + // regression that mislocates it. nodePty: { exists: true, - packageDir: `${unpackedRoot}\\node_modules\\node-pty`, - nativeDir: `${unpackedRoot}\\node_modules\\node-pty\\build\\Release`, + packageDir: 'C:\\App\\resources\\node_modules\\node-pty', + nativeDir: 'C:\\App\\resources\\node_modules\\node-pty\\build\\Release', nativeRel: 'build/Release', conptyNode: { name: 'conpty.node', path: 'x', exists: true, size: 1 } } @@ -95,11 +99,16 @@ function run() { const dests = plan.ops.map((o) => o.destRel) check(`${tier}: has exe`, dests.includes('Orca.exe')) check(`${tier}: has icu`, dests.includes('icudtl.dat')) - check(`${tier}: has daemon entry`, dests.includes('out/main/daemon-entry.js')) - check(`${tier}: has node-pty dir`, dests.includes('node_modules/node-pty')) + // destRel mirrors the full win-unpacked layout so the require-closure and + // node-pty native resolution work verbatim from the copy. + check( + `${tier}: has daemon entry`, + dests.includes('resources/app.asar.unpacked/out/main/daemon-entry.js') + ) + check(`${tier}: has node-pty dir`, dests.includes('resources/node_modules/node-pty')) check( `${tier}: node-pty op is a dir`, - plan.ops.find((o) => o.destRel === 'node_modules/node-pty')?.kind === 'dir' + plan.ops.find((o) => o.destRel === 'resources/node_modules/node-pty')?.kind === 'dir' ) } const fullDests = resolveTierFileSet(inv, 'full').ops.map((o) => o.destRel) diff --git a/tools/daemon-relocation-spike/tier-file-set.mjs b/tools/daemon-relocation-spike/tier-file-set.mjs index 7bc58c431..312d9a6c8 100644 --- a/tools/daemon-relocation-spike/tier-file-set.mjs +++ b/tools/daemon-relocation-spike/tier-file-set.mjs @@ -2,6 +2,8 @@ // rather than a code change. A tier resolves (given a discovered inventory) to a // flat list of copy operations { sourcePath, destRel, kind }. +import { dirname, join, relative, sep } from 'node:path' + // GPU/render DLLs that a run-as-node Electron host plausibly never loads. The // spike measures whether dropping them still yields a working ConPTY host. // ffmpeg.dll is deliberately NOT here — it is kept in the no-gpu tier. @@ -42,71 +44,74 @@ export function selectTierDlls(topLevelDlls, tier) { return topLevelDlls.filter((e) => !isGpuDll(e.name)) } -// Copy the whole node-pty package tree so its default native/conpty resolution -// (relative to node-pty's own __dirname) keeps working from the relocated path, -// and so the daemon require-closure resolves `node-pty` by walking up from the -// mirrored daemon-entry.js. destRel mirrors the app.asar.unpacked layout. -function nodePtyRelDir(inv) { - // node-pty's package dir relative to the unpacked root, normalized to '/'. - const root = inv.unpackedRoot ?? '' - const rel = inv.nodePty.packageDir.slice(root.length).replace(/^[\\/]+/, '') - return rel.split('\\').join('/') +/** + * A source file/dir's path relative to the win-unpacked root, normalized to '/'. + * + * Why relative to the APP DIR (not the app.asar.unpacked root): node-pty is + * packaged at resources/node_modules/node-pty (see + * config/packaged-runtime-node-modules.cjs), a SIBLING of app.asar.unpacked. + * The daemon resolves `require('node-pty')` by walking parent dirs up from + * resources/app.asar.unpacked/out/main/daemon-entry.js, which passes through + * resources/ and finds resources/node_modules/node-pty. Mirroring the full + * win-unpacked layout verbatim is the only copy that preserves that walk. + */ +export function toPosixRelative(appDir, absPath) { + return relative(appDir, absPath).split(sep).join('/') } /** * Build the ordered copy plan for a tier. Returns { ops, warnings } where each - * op is { sourcePath, destRel, kind: 'file' | 'dir' }. Missing required inputs - * are surfaced as warnings rather than thrown, so the report stays complete. + * op is { sourcePath, destRel, kind: 'file' | 'dir' }. Every destRel mirrors the + * source's win-unpacked-relative path. Missing required inputs are surfaced as + * warnings rather than thrown, so the report stays complete. */ export function resolveTierFileSet(inv, tier) { const ops = [] const warnings = [] + const appDir = inv.appDir - const addFile = (entry, destRel, requiredLabel) => { + const addFile = (entry, requiredLabel, optional = false) => { if (!entry || !entry.exists) { if (requiredLabel) { warnings.push(`missing required input: ${requiredLabel}`) } return } - ops.push({ sourcePath: entry.path, destRel, kind: 'file' }) + ops.push({ + sourcePath: entry.path, + destRel: toPosixRelative(appDir, entry.path), + kind: 'file', + optional + }) } - // Core: exe + runtime data blobs live next to Orca.exe in win-unpacked, so - // they mirror to the host-dir root. - addFile(inv.hostExe, inv.hostExe.name, 'Orca.exe') + // Core: exe + runtime data blobs live next to Orca.exe in win-unpacked. + addFile(inv.hostExe, 'Orca.exe') for (const entry of inv.runtimeData) { - addFile(entry, entry.name, entry.name) + addFile(entry, entry.name) } // Top-level DLLs per tier. for (const dll of selectTierDlls(inv.topLevelDlls, tier)) { - addFile(dll, dll.name) + addFile(dll) } - // Daemon bundle: the entry plus its sibling chunks + the unpacked - // out/package.json (module-type resolution). Mirror the unpacked layout so the - // relative require-closure resolves unchanged. + // Daemon bundle: the entry, its sibling chunks/, and the unpacked + // out/package.json (CJS/ESM loader resolution). Mirror the layout verbatim. if (inv.daemonEntry.exists && inv.unpackedRoot) { + addFile(inv.daemonEntry, 'daemon-entry.js') + const entryDir = dirname(inv.daemonEntry.path) + const chunksDir = join(entryDir, 'chunks') ops.push({ - sourcePath: inv.daemonEntry.path, - destRel: inv.daemonEntry.relFromUnpacked, - kind: 'file' - }) - const entryDir = inv.daemonEntry.relFromUnpacked.includes('/') - ? inv.daemonEntry.relFromUnpacked.slice(0, inv.daemonEntry.relFromUnpacked.lastIndexOf('/')) - : '' - // chunks/ sit beside the entry (out/main/chunks per asarUnpack config). - ops.push({ - sourcePath: `${inv.unpackedRoot}\\${entryDir.split('/').join('\\')}\\chunks`, - destRel: entryDir ? `${entryDir}/chunks` : 'chunks', + sourcePath: chunksDir, + destRel: toPosixRelative(appDir, chunksDir), kind: 'dir', optional: true }) - // out/package.json is unpacked so Node can resolve the CJS/ESM loader. + const pkgJson = join(inv.unpackedRoot, 'out', 'package.json') ops.push({ - sourcePath: `${inv.unpackedRoot}\\out\\package.json`, - destRel: 'out/package.json', + sourcePath: pkgJson, + destRel: toPosixRelative(appDir, pkgJson), kind: 'file', optional: true }) @@ -115,11 +120,11 @@ export function resolveTierFileSet(inv, tier) { } // node-pty package tree (native binding + conpty runtime), mirrored at its - // app.asar.unpacked-relative path. - if (inv.nodePty.exists && inv.nodePty.conptyNode && inv.unpackedRoot) { + // real win-unpacked path (resources/node_modules/node-pty). + if (inv.nodePty.exists && inv.nodePty.conptyNode) { ops.push({ sourcePath: inv.nodePty.packageDir, - destRel: nodePtyRelDir(inv), + destRel: toPosixRelative(appDir, inv.nodePty.packageDir), kind: 'dir' }) } else {