Skip to content

Commit e406b44

Browse files
authored
fix: keep Windows node-pty addons in the desktop package (#408)
## Requirement or Bug The Windows desktop package step stops because `pty.node` is missing from the packaged Host. ## Bug Reproduction Steps Rerun the failed jobs on desktop release run 43546042628. The Windows job fails in `afterPack` with ENOENT for `resources/host/node_modules/node-pty/prebuilds/win32-x64/pty.node`. ## Root Cause `pnpm deploy` leaves `node-pty` native addons as links into the store. electron-builder recreates every link as a directory junction on Windows. A junction cannot be `pty.node`, so the file check in `verify-packaged-runtime.ts` fails. This is a fundamental fix of the copy, plus a restore at pack time if a link still gets through. ## Code Changes Staging now copies the Host `node_modules` with links expanded, then expands any link still under `node-pty`, and refuses to continue unless `pty.node`, `conpty.node`, and `conpty_console_list.node` are real files. The pack check copies those files from the staged tree when the packaged copy is missing or is still a link. ## Behavior Changes and Affected Users | Behavior | Before | After | Who relies on the old behavior | Escape hatch | |---|---|---|---|---| | Windows desktop package contains node-pty `win32-x64` addons | The package step can stop with ENOENT for `pty.node` | The addons are real files in the packaged Host | Windows desktop build | None | Affected module: `apps/desktop` staging and `afterPack`. Tests: `stage-runtime.spec.ts`, `verify-packaged-runtime.spec.ts`. ## Checklist - [x] I have read the [CONTRIBUTING](https://github.com/PyModel/pythinker-code/blob/main/CONTRIBUTING.md) document. - [ ] I have linked a related issue (external PRs: issue must have a maintainer's `/approve`). - [x] I have added tests for new behavior. - [ ] I have updated relevant documentation. - [ ] I have run `pnpm typecheck` locally and it passes.
1 parent 7871d4d commit e406b44

4 files changed

Lines changed: 181 additions & 4 deletions

File tree

‎apps/desktop/scripts/stage-runtime.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,55 @@ async function materializeLinks(): Promise<void> {
160160
}
161161
}
162162

163+
/** Windows node-pty files the packaged Host must contain as real files. */
164+
export const WINDOWS_NODE_PTY_FILES = [
165+
['node-pty', 'prebuilds', 'win32-x64', 'pty.node'],
166+
['node-pty', 'prebuilds', 'win32-x64', 'conpty.node'],
167+
['node-pty', 'prebuilds', 'win32-x64', 'conpty_console_list.node'],
168+
] as const
169+
170+
/**
171+
* Replace every symlink under `directory` with the file or directory it names.
172+
*
173+
* `pnpm deploy` stores native addons as links into the content-addressable
174+
* store. electron-builder recreates every link as a directory junction on
175+
* Windows, and a junction cannot be `pty.node`, so the packaged Host then
176+
* fails its own file check.
177+
* @param directory - Tree to rewrite. Missing directories are ignored.
178+
*/
179+
export async function dereferenceSymlinks(directory: string): Promise<void> {
180+
if (!existsSync(directory)) return
181+
for (let link = await findSymlink(directory); link !== undefined; link = await findSymlink(directory)) {
182+
const source = await realpath(link)
183+
await rm(link, { recursive: true, force: true })
184+
await cp(source, link, { recursive: true, dereference: true })
185+
}
186+
}
187+
188+
/**
189+
* The Windows addon check applies only when this process is packaging Windows.
190+
* Linux and macOS package jobs do not download the win32 prebuilds.
191+
* @param platform - The value of `process.platform`.
192+
* @returns Whether staging must contain real `win32-x64` node-pty files.
193+
*/
194+
export function shouldAssertWindowsNodePty(platform: string): boolean {
195+
return platform === 'win32'
196+
}
197+
198+
/**
199+
* Fail staging when the Windows node-pty addon was not materialized.
200+
* @param nodeModules - The staged `node_modules` directory.
201+
*/
202+
export async function assertWindowsNodePty(nodeModules: string): Promise<void> {
203+
for (const segments of WINDOWS_NODE_PTY_FILES) {
204+
const path = join(nodeModules, ...segments)
205+
const metadata = await lstat(path).catch(() => undefined)
206+
if (metadata === undefined || !metadata.isFile() || metadata.isSymbolicLink()) {
207+
throw new Error(`desktop Host is missing a real Windows node-pty file: ${path}`)
208+
}
209+
}
210+
}
211+
163212
async function deploy(target: string): Promise<void> {
164213
const savedWorkspaceState = existsSync(workspaceState) ? await readFile(workspaceState) : undefined
165214
try {
@@ -193,6 +242,7 @@ async function main(): Promise<void> {
193242
await mkdir(staging, { recursive: true })
194243
await cp(join(deployed, 'node_modules'), join(staging, 'node_modules'), {
195244
recursive: true,
245+
dereference: true,
196246
})
197247
const runtimePackage = join(staging, 'node_modules/@pymodel/pythinker-code')
198248
await mkdir(runtimePackage, { recursive: true })
@@ -203,6 +253,10 @@ async function main(): Promise<void> {
203253
})
204254
}
205255
await materializeLinks()
256+
await dereferenceSymlinks(join(staging, 'node_modules', 'node-pty'))
257+
if (shouldAssertWindowsNodePty(process.platform)) {
258+
await assertWindowsNodePty(join(staging, 'node_modules'))
259+
}
206260
} finally {
207261
await rm(deployed, { recursive: true, force: true })
208262
}

‎apps/desktop/scripts/verify-packaged-runtime.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** Reject a packaged desktop shell that omitted the staged Host entrypoints. */
22

3-
import { access } from 'node:fs/promises'
3+
import { access, cp, lstat, rm } from 'node:fs/promises'
44
import { join } from 'node:path'
55
import type { AfterPackContext } from 'electron-builder'
66

@@ -15,19 +15,47 @@ const REQUIRED_WINDOWS_NODE_PTY_ENTRIES = [
1515
['node-pty', 'prebuilds', 'win32-x64', 'conpty_console_list.node'],
1616
] as const
1717

18+
/**
19+
* Copy a staged node-pty file into the package when the packager left a link.
20+
*
21+
* electron-builder turns a file symlink into a Windows directory junction, and
22+
* `access` then reports the addon as missing even though staging had it.
23+
* @param packagedNodeModules - `resources/host/node_modules` inside the app.
24+
* @param stagedNodeModules - `runtime-host/node_modules` produced before packing.
25+
*/
26+
export async function restoreWindowsNodePty(
27+
packagedNodeModules: string,
28+
stagedNodeModules: string,
29+
): Promise<void> {
30+
for (const segments of REQUIRED_WINDOWS_NODE_PTY_ENTRIES) {
31+
const destination = join(packagedNodeModules, ...segments)
32+
const metadata = await lstat(destination).catch(() => undefined)
33+
if (metadata?.isFile() === true && !metadata.isSymbolicLink()) continue
34+
if (metadata !== undefined) await rm(destination, { recursive: true, force: true })
35+
await cp(join(stagedNodeModules, ...segments), destination, { dereference: true })
36+
}
37+
}
38+
1839
/**
1940
* Verify the Host files required before the signed application can start.
2041
* @param context - Electron Builder's completed application directory.
42+
* @param stagedNodeModules - Optional staged Host tree used to repair Windows addons.
2143
* @returns A promise that rejects when a staged Host entrypoint is absent.
2244
*/
23-
export async function afterPack(context: AfterPackContext): Promise<void> {
45+
export async function afterPack(
46+
context: AfterPackContext,
47+
stagedNodeModules: string = join(import.meta.dirname, '..', 'runtime-host', 'node_modules'),
48+
): Promise<void> {
2449
const resources = context.electronPlatformName === 'darwin'
2550
? join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`, 'Contents', 'Resources')
2651
: join(context.appOutDir, 'resources')
2752
for (const segments of REQUIRED_HOST_FILES) {
2853
await access(join(resources, 'host', 'node_modules', ...segments))
2954
}
3055
if (context.electronPlatformName !== 'win32') return
56+
await restoreWindowsNodePty(join(resources, 'host', 'node_modules'), stagedNodeModules).catch(error => {
57+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
58+
})
3159
for (const segments of REQUIRED_WINDOWS_NODE_PTY_ENTRIES) {
3260
await access(join(resources, 'host', 'node_modules', ...segments))
3361
}

‎apps/desktop/tests/stage-runtime.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { lstat, mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'
12
import { isAbsolute, join } from 'node:path'
3+
import { tmpdir } from 'node:os'
24
import { describe, expect, it } from 'vitest'
35
import {
46
DEPLOY_ATTEMPTS,
7+
assertWindowsNodePty,
58
deployRetryDelayMs,
69
deployTargetArgument,
10+
dereferenceSymlinks,
711
packageManagerInvocation,
12+
shouldAssertWindowsNodePty,
813
withDeployRetries,
914
} from '../scripts/stage-runtime'
1015

@@ -57,6 +62,51 @@ describe('deploy target argument', () => {
5762
})
5863
})
5964

65+
describe('Windows node-pty materialization', () => {
66+
it('checks the Windows addon only on Windows', () => {
67+
expect(shouldAssertWindowsNodePty('win32')).toBe(true)
68+
expect(shouldAssertWindowsNodePty('linux')).toBe(false)
69+
expect(shouldAssertWindowsNodePty('darwin')).toBe(false)
70+
})
71+
72+
it('replaces a symlink with the file it names', async () => {
73+
const root = await mkdtemp(join(tmpdir(), 'pythinker-deref-'))
74+
try {
75+
const real = join(root, 'real.node')
76+
const link = join(root, 'tree', 'pty.node')
77+
await writeFile(real, 'addon')
78+
await mkdir(join(root, 'tree'))
79+
await symlink(real, link)
80+
81+
await dereferenceSymlinks(join(root, 'tree'))
82+
83+
const metadata = await lstat(link)
84+
expect(metadata.isSymbolicLink()).toBe(false)
85+
expect(metadata.isFile()).toBe(true)
86+
} finally {
87+
await rm(root, { recursive: true, force: true })
88+
}
89+
})
90+
91+
it('rejects a staged Host whose pty.node is still a symlink', async () => {
92+
const root = await mkdtemp(join(tmpdir(), 'pythinker-pty-assert-'))
93+
try {
94+
const real = join(root, 'real.node')
95+
const nodeModules = join(root, 'node_modules')
96+
const link = join(nodeModules, 'node-pty', 'prebuilds', 'win32-x64', 'pty.node')
97+
await writeFile(real, 'addon')
98+
await mkdir(join(link, '..'), { recursive: true })
99+
await symlink(real, link)
100+
await writeFile(join(link, '..', 'conpty.node'), '')
101+
await writeFile(join(link, '..', 'conpty_console_list.node'), '')
102+
103+
await expect(assertWindowsNodePty(nodeModules)).rejects.toThrow(/pty\.node/)
104+
} finally {
105+
await rm(root, { recursive: true, force: true })
106+
}
107+
})
108+
})
109+
60110
describe('deploy retries', () => {
61111
function recorder() {
62112
const waits: number[] = []

‎apps/desktop/tests/verify-packaged-runtime.spec.ts‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
1+
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
22
import { tmpdir } from 'node:os'
33
import { join } from 'node:path'
44
import { describe, expect, it } from 'vitest'
5-
import { afterPack } from '../scripts/verify-packaged-runtime'
5+
import { afterPack, restoreWindowsNodePty } from '../scripts/verify-packaged-runtime'
66

77
function context(appOutDir: string, electronPlatformName = 'darwin') {
88
return {
@@ -56,6 +56,51 @@ describe('packaged desktop runtime verification', () => {
5656
}
5757
})
5858

59+
it('replaces a missing Windows pty.node from the staged Host tree', async () => {
60+
const appOutDir = await mkdtemp(join(tmpdir(), 'pythinker-packaged-runtime-'))
61+
const staged = await mkdtemp(join(tmpdir(), 'pythinker-staged-pty-'))
62+
try {
63+
const resources = join(appOutDir, 'resources', 'host', 'node_modules')
64+
const cli = join(resources, '@pymodel', 'pythinker-code', 'dist', 'main.mjs')
65+
const web = join(resources, '@pymodel', 'pythinker-code', 'dist-web', 'index.html')
66+
await mkdir(join(cli, '..'), { recursive: true })
67+
await mkdir(join(web, '..'), { recursive: true })
68+
await writeFile(cli, '')
69+
await writeFile(web, '')
70+
for (const name of ['pty.node', 'conpty.node', 'conpty_console_list.node']) {
71+
const file = join(staged, 'node-pty', 'prebuilds', 'win32-x64', name)
72+
await mkdir(join(file, '..'), { recursive: true })
73+
await writeFile(file, name)
74+
}
75+
76+
await expect(afterPack(context(appOutDir, 'win32'), staged)).resolves.toBeUndefined()
77+
} finally {
78+
await rm(appOutDir, { recursive: true, force: true })
79+
await rm(staged, { recursive: true, force: true })
80+
}
81+
})
82+
83+
it('copies through a symlink when restoring a Windows node-pty addon', async () => {
84+
const root = await mkdtemp(join(tmpdir(), 'pythinker-pty-link-'))
85+
try {
86+
const real = join(root, 'real.node')
87+
const link = join(root, 'staged', 'node-pty', 'prebuilds', 'win32-x64', 'pty.node')
88+
await writeFile(real, 'pty')
89+
await mkdir(join(link, '..'), { recursive: true })
90+
await symlink(real, link)
91+
const packaged = join(root, 'packaged')
92+
await mkdir(join(packaged, 'node-pty', 'prebuilds', 'win32-x64'), { recursive: true })
93+
await writeFile(join(packaged, 'node-pty', 'prebuilds', 'win32-x64', 'conpty.node'), '')
94+
await writeFile(join(packaged, 'node-pty', 'prebuilds', 'win32-x64', 'conpty_console_list.node'), '')
95+
96+
await restoreWindowsNodePty(packaged, join(root, 'staged'))
97+
98+
await expect(readFile(join(packaged, 'node-pty', 'prebuilds', 'win32-x64', 'pty.node'), 'utf8')).resolves.toBe('pty')
99+
} finally {
100+
await rm(root, { recursive: true, force: true })
101+
}
102+
})
103+
59104
it('does not require Windows node-pty entries for a Darwin shell', async () => {
60105
const appOutDir = await mkdtemp(join(tmpdir(), 'pythinker-packaged-runtime-'))
61106
try {

0 commit comments

Comments
 (0)