Skip to content

Commit cd4241b

Browse files
authored
fix: stop requiring node-pty in the Windows desktop package (#413)
1 parent 861c5a3 commit cd4241b

8 files changed

Lines changed: 14 additions & 244 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-desktop": patch
3+
---
4+
5+
Fix the Windows desktop installer build.

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -160,55 +160,6 @@ 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-
212163
async function deploy(target: string): Promise<void> {
213164
const savedWorkspaceState = existsSync(workspaceState) ? await readFile(workspaceState) : undefined
214165
try {
@@ -253,10 +204,6 @@ async function main(): Promise<void> {
253204
})
254205
}
255206
await materializeLinks()
256-
await dereferenceSymlinks(join(staging, 'node_modules', 'node-pty'))
257-
if (shouldAssertWindowsNodePty(process.platform)) {
258-
await assertWindowsNodePty(join(staging, 'node_modules'))
259-
}
260207
} finally {
261208
await rm(deployed, { recursive: true, force: true })
262209
}
Lines changed: 2 additions & 40 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, cp, lstat, rm } from 'node:fs/promises'
3+
import { access } from 'node:fs/promises'
44
import { join } from 'node:path'
55
import type { AfterPackContext } from 'electron-builder'
66

@@ -9,56 +9,18 @@ const REQUIRED_HOST_FILES = [
99
['@pymodel', 'pythinker-code', 'dist-web', 'index.html'],
1010
] as const
1111

12-
const REQUIRED_WINDOWS_NODE_PTY_ENTRIES = [
13-
['node-pty', 'prebuilds', 'win32-x64', 'pty.node'],
14-
['node-pty', 'prebuilds', 'win32-x64', 'conpty.node'],
15-
['node-pty', 'prebuilds', 'win32-x64', 'conpty_console_list.node'],
16-
] as const
17-
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-
3912
/**
4013
* Verify the Host files required before the signed application can start.
4114
* @param context - Electron Builder's completed application directory.
42-
* @param stagedNodeModules - Optional staged Host tree used to repair Windows addons.
4315
* @returns A promise that rejects when a staged Host entrypoint is absent.
4416
*/
45-
export async function afterPack(
46-
context: AfterPackContext,
47-
stagedNodeModules: string = join(import.meta.dirname, '..', 'runtime-host', 'node_modules'),
48-
): Promise<void> {
17+
export async function afterPack(context: AfterPackContext): Promise<void> {
4918
const resources = context.electronPlatformName === 'darwin'
5019
? join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`, 'Contents', 'Resources')
5120
: join(context.appOutDir, 'resources')
5221
for (const segments of REQUIRED_HOST_FILES) {
5322
await access(join(resources, 'host', 'node_modules', ...segments))
5423
}
55-
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-
})
59-
for (const segments of REQUIRED_WINDOWS_NODE_PTY_ENTRIES) {
60-
await access(join(resources, 'host', 'node_modules', ...segments))
61-
}
6224
}
6325

6426
export default afterPack

‎apps/desktop/src/host-supervisor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export function spawnPythinkerServer(options: SpawnPythinkerServerOptions): Host
428428
* Terminate a child and its descendants.
429429
*
430430
* Windows has no signal delivery: `child.kill` calls TerminateProcess on one
431-
* PID, so the Host's own children (node-pty shells, subagent hosts) survive and
431+
* PID, so the Host's own children (shells, subagent hosts) survive and
432432
* keep holding the loopback port. `taskkill /T` walks the tree instead.
433433
* Because this call is synchronous, keep it bounded so a stalled taskkill falls
434434
* back to a single-process kill instead of blocking the Electron main loop indefinitely.

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

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import { tmpdir } from 'node:os'
44
import { describe, expect, it } from 'vitest'
55
import {
66
DEPLOY_ATTEMPTS,
7-
assertWindowsNodePty,
87
deployRetryDelayMs,
98
deployTargetArgument,
10-
dereferenceSymlinks,
119
packageManagerInvocation,
12-
shouldAssertWindowsNodePty,
1310
withDeployRetries,
1411
} from '../scripts/stage-runtime'
1512

@@ -62,51 +59,6 @@ describe('deploy target argument', () => {
6259
})
6360
})
6461

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-
11062
describe('deploy retries', () => {
11163
function recorder() {
11264
const waits: number[] = []

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

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
1+
import { mkdir, mkdtemp, rm, 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, restoreWindowsNodePty } from '../scripts/verify-packaged-runtime'
5+
import { afterPack } from '../scripts/verify-packaged-runtime'
66

77
function context(appOutDir: string, electronPlatformName = 'darwin') {
88
return {
@@ -39,7 +39,7 @@ describe('packaged desktop runtime verification', () => {
3939
}
4040
})
4141

42-
it('rejects a Windows shell whose node-pty native closure was filtered out', async () => {
42+
it('accepts a Windows shell without any node-pty addon', async () => {
4343
const appOutDir = await mkdtemp(join(tmpdir(), 'pythinker-packaged-runtime-'))
4444
try {
4545
const resources = join(appOutDir, 'resources', 'host', 'node_modules')
@@ -50,58 +50,13 @@ describe('packaged desktop runtime verification', () => {
5050
await writeFile(cli, '')
5151
await writeFile(web, '')
5252

53-
await expect(afterPack(context(appOutDir, 'win32'))).rejects.toMatchObject({ code: 'ENOENT' })
53+
await expect(afterPack(context(appOutDir, 'win32'))).resolves.toBeUndefined()
5454
} finally {
5555
await rm(appOutDir, { recursive: true, force: true })
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-
104-
it('does not require Windows node-pty entries for a Darwin shell', async () => {
59+
it('accepts a Darwin shell', async () => {
10560
const appOutDir = await mkdtemp(join(tmpdir(), 'pythinker-packaged-runtime-'))
10661
try {
10762
const resources = join(appOutDir, 'Pythinker.app', 'Contents', 'Resources', 'host', 'node_modules')

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"architecture:check": "node scripts/architecture/architecture-check.mjs",
9-
"postinstall": "node scripts/fix-node-pty-perms.mjs && node scripts/security/prune-monaco-distributions.mjs",
9+
"postinstall": "node scripts/security/prune-monaco-distributions.mjs",
1010
"build": "pnpm -r run build",
1111
"build:packages": "pnpm -r --filter './packages/*' run build",
1212
"dev:cli": "pnpm -C apps/pythinker-code run dev",

‎scripts/fix-node-pty-perms.mjs‎

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)