Files
orca/config/scripts/shared-native-build.test.mjs
T
Neil df0c44571b fix(build): shared native packages get one immutable build instead of in-place rebuilds (#30226)
* refactor(build): extract the lock-free shared tree publish from the Electron dist cache

Native builds in package dirs shared across worktrees need the same
stage, validate, rename-into-place publish; one implementation keeps the
never-replace-a-good-entry and quarantine-restore rules in one place.

* fix(build): never rebuild a native addon in a package dir other worktrees load

Under pnpm's global virtual store node_modules/node-pty is one store slot
shared by every worktree, so an in-place node-gyp or @electron/rebuild run
in one worktree replaced the pty.node another was running. A shared
package dir now gets an immutable per-ABI build/orca-napi-<platform>-<arch>
compiled in a scratch copy, load-checked in a fresh process, and published
atomically; a usable entry is reused, never rebuilt. The patched node-pty
and windows-process-tree loaders try that dir before build/Release, and
packaging renames it to build/Release so packaged layouts are unchanged.
Private (non-GVS, CI) installs keep the existing in-place path.

* fix(build): find shared native packages through the project's own node_modules link

require.resolve with paths fell back to another install under Bun, so a
test fixture's private node-pty was judged shared.

* ci: run native checks when the shared native build modules change

* fix(build): pin the shared native slot and keep sibling publishes intact

Resolve the package's physical dir once so a concurrent relink cannot publish
one slot's build into another, verify the quarantined tree is the one judged
unusable before replacing it, and keep in-flight staging/quarantine trees out
of the packaged resource copy.

* fix(build): never move a sibling's fresh native entry, and refuse off-platform builds

Re-check the entry's identity right before quarantining it, and fail instead
of publishing a host-OS binary under another platform's entry name.
2026-10-18 20:21:50 -07:00

98 lines
3.1 KiB
JavaScript

import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { checkNativeBuildDirArtifacts, ensureSharedNativeBuild } from './shared-native-build.mjs'
const roots = []
afterEach(() => {
while (roots.length > 0) {
rmSync(roots.pop(), { recursive: true, force: true })
}
})
function makePackage() {
const root = mkdtempSync(path.join(tmpdir(), 'orca-shared-native-'))
roots.push(root)
return realpathSync(root)
}
describe('ensureSharedNativeBuild', () => {
it('reuses a usable published entry without compiling, even when forced', () => {
const packageDir = makePackage()
const entryDir = path.join(packageDir, 'build', 'orca-napi-linux-x64')
mkdirSync(entryDir, { recursive: true })
const checked = []
const result = ensureSharedNativeBuild({
moduleName: 'node-pty',
packageDir,
platform: 'linux',
arch: 'x64',
checkBuildDir: (dir) => {
checked.push(dir)
return null
},
// No source in packageDir: any compile attempt would throw.
log: () => {}
})
expect(result).toEqual({ entryDir, built: false })
expect(checked).toEqual([entryDir])
})
it('publishes beside the physical package dir, not through the project link', () => {
const packageDir = makePackage()
const entryDir = path.join(packageDir, 'build', 'orca-napi-linux-x64')
mkdirSync(entryDir, { recursive: true })
const link = path.join(makePackage(), 'node-pty')
symlinkSync(packageDir, link, 'junction')
const result = ensureSharedNativeBuild({
moduleName: 'node-pty',
packageDir: link,
platform: 'linux',
arch: 'x64',
checkBuildDir: () => null,
log: () => {}
})
expect(result.entryDir).toBe(entryDir)
})
it('refuses to compile for another platform', () => {
const otherPlatform = process.platform === 'linux' ? 'darwin' : 'linux'
expect(() =>
ensureSharedNativeBuild({
moduleName: 'node-pty',
packageDir: makePackage(),
platform: otherPlatform,
arch: 'x64',
checkBuildDir: () => null,
log: () => {}
})
).toThrow(`cannot build node-pty for ${otherPlatform}`)
})
})
describe('checkNativeBuildDirArtifacts', () => {
it('requires spawn-helper on macOS only', () => {
const dir = makePackage()
writeFileSync(path.join(dir, 'pty.node'), '')
expect(checkNativeBuildDirArtifacts('node-pty', dir, { platform: 'linux', arch: 'x64' })).toBe(
null
)
expect(
checkNativeBuildDirArtifacts('node-pty', dir, { platform: 'darwin', arch: 'x64' })
).toMatch(/spawn-helper is missing/)
})
it('rejects a Windows addon built for another arch', () => {
const dir = makePackage()
mkdirSync(path.join(dir, 'conpty'))
for (const file of ['conpty.node', 'conpty/conpty.dll', 'conpty/OpenConsole.exe']) {
writeFileSync(path.join(dir, file), 'not-a-pe')
}
expect(
checkNativeBuildDirArtifacts('node-pty', dir, { platform: 'win32', arch: 'arm64' })
).toMatch(/not arm64/)
})
})