singularity-forge/scripts/link-workspace-packages.cjs
Jeremy McSpadden 23b89c64c9 fix: detect broken install and add Windows symlink fallback (#890)
Fixes #882 — npm install -g gsd-pi installing a broken version where
@gsd/pi-coding-agent cannot be resolved, causing ERR_MODULE_NOT_FOUND.

Root causes addressed:
1. On Windows without Developer Mode or admin rights, symlinkSync fails
   even for NTFS junctions, leaving node_modules/@gsd/ empty and causing
   a cryptic ERR_MODULE_NOT_FOUND instead of a usable error message.
2. If npm latest dist-tag is stale (pointing to an old version that
   predates the packages/ directory), users get the same failure.

Changes:
- src/loader.ts: after symlinking, validate @gsd/pi-coding-agent exists;
  emit a clear actionable error with reinstall instructions instead of
  letting Node throw ERR_MODULE_NOT_FOUND deep inside cli.js. Also adds
  cpSync fallback when symlinkSync fails (Windows without elevated perms).
- scripts/link-workspace-packages.cjs: same cpSync fallback — ensures
  postinstall succeeds on restricted Windows environments.
- scripts/validate-pack.js: verify @gsd/* packages are resolvable after
  the isolated install test, and run `gsd -v` to confirm end-to-end
  resolution before declaring the pack valid.
- .github/workflows/build-native.yml: add post-publish dist-tag
  verification step that confirms npm dist-tags.latest matches the
  published version for stable releases, catching stale-tag regressions
  in CI before users encounter them.
2026-03-17 09:35:57 -06:00

86 lines
2.8 KiB
JavaScript

#!/usr/bin/env node
/**
* link-workspace-packages.cjs
*
* Creates node_modules/@gsd/* symlinks pointing to packages/* directories.
*
* During development, npm workspaces creates these automatically. But in the
* published tarball, workspace packages are shipped under packages/ (via the
* "files" field) and the @gsd/* imports in compiled code need node_modules/@gsd/*
* to resolve. This script bridges the gap.
*
* Runs as part of postinstall (before any ESM code that imports @gsd/*).
*
* On Windows without Developer Mode or administrator rights, creating symlinks
* (even NTFS junctions) can fail with EPERM. In that case we fall back to
* cpSync (directory copy) which works universally.
*/
const { existsSync, mkdirSync, symlinkSync, cpSync, lstatSync, readlinkSync, unlinkSync } = require('fs')
const { resolve, join } = require('path')
const root = resolve(__dirname, '..')
const packagesDir = join(root, 'packages')
const nodeModulesGsd = join(root, 'node_modules', '@gsd')
// Map directory names to package names
const packageMap = {
'native': 'native',
'pi-agent-core': 'pi-agent-core',
'pi-ai': 'pi-ai',
'pi-coding-agent': 'pi-coding-agent',
'pi-tui': 'pi-tui',
}
// Ensure @gsd scope directory exists
if (!existsSync(nodeModulesGsd)) {
mkdirSync(nodeModulesGsd, { recursive: true })
}
let linked = 0
let copied = 0
for (const [dir, name] of Object.entries(packageMap)) {
const source = join(packagesDir, dir)
const target = join(nodeModulesGsd, name)
if (!existsSync(source)) continue
// Skip if already correctly linked or is a real directory (bundled)
if (existsSync(target)) {
try {
const stat = lstatSync(target)
if (stat.isSymbolicLink()) {
const linkTarget = readlinkSync(target)
if (resolve(join(nodeModulesGsd, linkTarget)) === source || linkTarget === source) {
continue // Already correct
}
unlinkSync(target) // Wrong target, relink
} else {
continue // Real directory (e.g., copied or from bundleDependencies), don't touch
}
} catch {
continue
}
}
let symlinkOk = false
try {
symlinkSync(source, target, 'junction') // junction works on Windows too
symlinkOk = true
linked++
} catch {
// Symlink failed — common on Windows without Developer Mode or admin rights.
// Fall back to a directory copy so the package is still resolvable.
}
if (!symlinkOk) {
try {
cpSync(source, target, { recursive: true })
copied++
} catch {
// Non-fatal — loader.ts will emit a clearer error if resolution still fails
}
}
}
if (linked > 0) process.stderr.write(` Linked ${linked} workspace package${linked !== 1 ? 's' : ''}\n`)
if (copied > 0) process.stderr.write(` Copied ${copied} workspace package${copied !== 1 ? 's' : ''} (symlinks unavailable)\n`)