mirror of
https://github.com/stablyai/orca.git
synced 2026-10-24 16:03:45 +00:00
* build(xterm): restore the patch regeneration harness and gate it in CI docs/reference/ime-architecture.md says "Never hand-edit the bundles in the patch" and links to docs/reference/xterm-patch-regeneration.md. That doc does not exist, and neither does the harness it describes. Both landed in29117bf776and were deleted by17cfc968cf, a revert of the composition-ownership change, which swept up a build tool and a CI gate as collateral. The rule survived; its enforcement did not. Every xterm patch since has had to hand-edit minified bundles to comply with the surrounding architecture, because everything resolves to lib/xterm.mjs at runtime and under vitest, so a src-only edit is inert. The shipped bundles were therefore not the output of any build, and this restores them to build output. Comparing identifier multisets against a pristine build of the pinned commit finds hand-written names a minifier never emits ($rl, $hp, $tid), const in an otherwise let-only esbuild bundle, !! where the source reads Boolean(), an escaped LRM where esbuild emits the literal, and a return block esbuild collapses to void(...). Every remaining token difference is a minifier local reallocating. The old source patch could not be reused. It described the reverted composition-ownership architecture, so restoring it would have re-applied an abandoned design on top of dropping three accumulated fixes. It is re-derived from the shipped patch instead, and the derivation is a fixed point. Two deliberate departures from the deleted version. Sourcemaps are included rather than deleted, because a live test reads lib/*.map and asserts the mapped version matches the runtime version. The source-patch superset carve-out is gone, so a source hunk the shipped patch cannot name now fails loudly instead of being carved out silently. The doc's claim that the webgl and serialize addons reproduce byte for byte was half wrong. Their ESM output does reproduce at the pinned commit, but both also publish CJS that the root package script never builds, so folding either in needs a build step this harness lacks. Recorded as a blocker rather than a confident sentence. xterm_patch_sync runs the regenerator in --check mode, so a patch that does not match a rebuild of the pinned upstream now fails PR CI. The -diff -text attribute is required, not cosmetic: pnpm hashes the patch byte-for-byte, so a CRLF checkout breaks the install outright. Not verified: the CI job has not run on a real runner, the addon CJS bundles are unreproduced, and the generator is untested on Windows and Linux. * build(xterm): make the regenerator runnable on Windows and drop dead paths Readiness review on the restore found one blocking gap and two cheap cleanups. None of them change the emitted patch, which is byte-identical before and after. The generator could not run on Windows at all. Three sites called npm through execFileSync with shell:false, but npm ships as npm.cmd there, execFile applies no PATHEXT, and since CVE-2024-27980 it refuses a .cmd target without a shell. That matters because this harness arms a blocking gate whose documented remedy is --write, so a Windows contributor who tripped the gate had no remedy except hand-editing a 7MB minified bundle, which is the practice the gate exists to abolish. Four sibling scripts in config/scripts already handle this; the fix follows them and lands in run(), so the manifest-driven build step is covered too. git and tar are real executables in System32 and keep resolving without a shell, which avoids quoting exposure on paths with spaces. deleteGeneratedSourcemaps was unreachable, since the policy is include. Deleting it left "delete" as a legal policy value that nothing honoured, so a manifest asking for it would have silently shipped sourcemaps that do not match the bundle. The enum is narrowed and an unrecognised policy now throws rather than falling through. generatedHunks moved into the test file rather than being dropped; its partition assertion, that generated and source hunks reconstruct the whole patch, is worth keeping. The -text attribute now covers all five patch files. pnpm hashes each of them byte-for-byte, so the CRLF hazard the xterm patch was protected from applies equally to node-pty and the three addons. All five were already LF in the object DB, so this pins existing behaviour. -diff stays scoped to the xterm patch, since the others are readable. The doc's claim that the addons reproduce byte for byte is now dated and marked a one-off measurement rather than an invariant, because nothing re-runs it. Effective lines fall from 591 to 568 against the 600 budget. Still the largest file in config/scripts, and adding a second package to the manifest would need a split first.
691 lines
25 KiB
JavaScript
691 lines
25 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { execFileSync } from 'node:child_process'
|
|
import { createHash } from 'node:crypto'
|
|
import {
|
|
copyFileSync,
|
|
cpSync,
|
|
existsSync,
|
|
mkdirSync,
|
|
readFileSync,
|
|
readdirSync,
|
|
realpathSync,
|
|
rmSync,
|
|
statSync,
|
|
writeFileSync
|
|
} from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import path from 'node:path'
|
|
import { pathToFileURL } from 'node:url'
|
|
|
|
const DEFAULT_REPO_ROOT = path.resolve(import.meta.dirname, '..', '..')
|
|
const MANIFEST_RELATIVE_PATH = path.join('config', 'patches', 'xterm-upstream.json')
|
|
|
|
/**
|
|
* Flags pnpm@10 passes to `git diff` in its own `diffFolders()`. A patch built
|
|
* with anything else is a patch pnpm may re-diff differently on the next
|
|
* `pnpm patch-commit`, so the byte-comparison gate would never settle.
|
|
*/
|
|
export const PNPM_DIFF_FLAGS = [
|
|
'-c',
|
|
'core.safecrlf=false',
|
|
'diff',
|
|
'--src-prefix=a/',
|
|
'--dst-prefix=b/',
|
|
'--ignore-cr-at-eol',
|
|
'--irreversible-delete',
|
|
'--full-index',
|
|
'--no-index',
|
|
'--text',
|
|
'--no-ext-diff',
|
|
'--no-color'
|
|
]
|
|
|
|
/**
|
|
* The same formatting as PNPM_DIFF_FLAGS minus `--no-index`, so a diff taken
|
|
* inside the upstream checkout is byte-comparable with the emitted patch.
|
|
*/
|
|
export const CHECKOUT_DIFF_FLAGS = PNPM_DIFF_FLAGS.filter((flag) => flag !== '--no-index')
|
|
|
|
/** Blanks the vars pnpm blanks so user and system git config cannot reach the diff. */
|
|
export function pnpmDiffEnvironment(baseEnvironment = process.env) {
|
|
return {
|
|
...baseEnvironment,
|
|
GIT_CONFIG_NOSYSTEM: '1',
|
|
HOME: '',
|
|
XDG_CONFIG_HOME: '',
|
|
USERPROFILE: ''
|
|
}
|
|
}
|
|
|
|
function escapeRegExp(value) {
|
|
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
}
|
|
|
|
function trimSurroundingSlashes(value) {
|
|
return value[0] === '/' || value.endsWith('/') ? value.replace(/^\/|\/$/g, '') : value
|
|
}
|
|
|
|
/**
|
|
* Reproduces pnpm's post-processing of the raw `git diff` output: strip the two
|
|
* scratch folder prefixes, drop a trailing no-newline marker, and remove
|
|
* .DS_Store entries a macOS run would otherwise smuggle in.
|
|
*/
|
|
export function normalizePnpmDiff(stdout, folderA, folderB) {
|
|
const a = folderA.replace(/\\/g, '/')
|
|
const b = folderB.replace(/\\/g, '/')
|
|
return stdout
|
|
.replace(new RegExp(`(a|b)(${escapeRegExp(`/${trimSurroundingSlashes(a)}/`)})`, 'g'), '$1/')
|
|
.replace(new RegExp(`(a|b)${escapeRegExp(`/${trimSurroundingSlashes(b)}/`)}`, 'g'), '$1/')
|
|
.replace(new RegExp(escapeRegExp(`${a}/`), 'g'), '')
|
|
.replace(new RegExp(escapeRegExp(`${b}/`), 'g'), '')
|
|
.replace(/\n\\ No newline at end of file\n$/, '\n')
|
|
.replace(/^diff --git a\/.*\.DS_Store b\/.*\.DS_Store[\s\S]+?(?=^diff --git)/gm, '')
|
|
.replace(/^diff --git a\/.*\.DS_Store b\/.*\.DS_Store[\s\S]*$/gm, '')
|
|
}
|
|
|
|
/** Splits a patch into one entry per `diff --git` stanza, keeping the raw text. */
|
|
export function splitPatchEntries(patchText) {
|
|
return patchText
|
|
.split(/^(?=diff --git )/m)
|
|
.filter((entry) => entry.startsWith('diff --git '))
|
|
.map((text) => {
|
|
const header = text.slice(0, text.indexOf('\n'))
|
|
const match = /^diff --git a\/(.+) b\/\1$/.exec(header)
|
|
if (!match) {
|
|
throw new Error(`Unsupported diff header (renames are not supported): ${header}`)
|
|
}
|
|
return { path: match[1], text }
|
|
})
|
|
}
|
|
|
|
export function selectPatchEntries(patchText, matches) {
|
|
return splitPatchEntries(patchText)
|
|
.filter((entry) => matches(entry.path))
|
|
.map((entry) => entry.text)
|
|
.join('')
|
|
}
|
|
|
|
/** The hand-editable half of a patch: everything under `src/`. */
|
|
export function sourceHunks(patchText) {
|
|
return selectPatchEntries(patchText, (file) => file.startsWith('src/'))
|
|
}
|
|
|
|
/**
|
|
* The source patch and the emitted patch are the same edits diffed two ways, so
|
|
* they must produce the same bytes. A source hunk the emitted patch cannot carry
|
|
* would be deleted by the next `--write`, so this fails instead of shipping one.
|
|
*/
|
|
export function assertSourceDerivationsAgree(checkoutSource, patchText) {
|
|
const checkout = sourceHunks(checkoutSource)
|
|
const emitted = sourceHunks(patchText)
|
|
if (checkout === emitted) {
|
|
return
|
|
}
|
|
throw new Error(
|
|
[
|
|
'The checkout diff and the emitted patch disagree on a source file.',
|
|
` from checkout: [${splitPatchEntries(checkout)
|
|
.map((e) => e.path)
|
|
.join(', ')}]`,
|
|
` from patch: [${splitPatchEntries(emitted)
|
|
.map((e) => e.path)
|
|
.join(', ')}]`,
|
|
` first difference at character ${firstDifferenceIndex(checkout, emitted)}`,
|
|
'',
|
|
'A hunk the emitted patch cannot name is never installed, so it cannot ship',
|
|
'here; upstream .npmignore strips `src/**/*.test.ts`.'
|
|
].join('\n')
|
|
)
|
|
}
|
|
|
|
export function stampVersionSource(source, version) {
|
|
const stamped = source.replace(
|
|
/export const XTERM_VERSION = '[^']+';/,
|
|
`export const XTERM_VERSION = '${version}';`
|
|
)
|
|
if (stamped === source && !source.includes(`'${version}'`)) {
|
|
throw new Error('Version stamp file does not declare XTERM_VERSION')
|
|
}
|
|
return stamped
|
|
}
|
|
|
|
/**
|
|
* The published tarball names the commit it was built from, so a version bump
|
|
* that forgets the manifest fails here instead of producing a patch against the
|
|
* wrong tree.
|
|
*/
|
|
export function assertPublishedCommit(publishedPackageJson, packageEntry, upstreamCommit) {
|
|
if (publishedPackageJson.version !== packageEntry.version) {
|
|
throw new Error(
|
|
`${packageEntry.name}: registry served ${publishedPackageJson.version}, manifest pins ${packageEntry.version}`
|
|
)
|
|
}
|
|
if (publishedPackageJson.commit !== upstreamCommit) {
|
|
throw new Error(
|
|
[
|
|
`${packageEntry.name}@${packageEntry.version} was published from commit`,
|
|
` ${publishedPackageJson.commit ?? '(absent)'}`,
|
|
`but ${MANIFEST_RELATIVE_PATH} pins`,
|
|
` ${upstreamCommit}`,
|
|
'Update upstream.commit in the manifest to the published commit, then rerun with --write.'
|
|
].join('\n')
|
|
)
|
|
}
|
|
}
|
|
|
|
/** Guards the publish-order trap: a dev esbuild pass would silently de-minify lib/*.mjs. */
|
|
export function assertBuildStepsAllowed(manifest) {
|
|
const forbidden = new Set(manifest.forbiddenBuildScripts?.scripts ?? [])
|
|
for (const packageEntry of manifest.packages) {
|
|
for (const step of packageEntry.build) {
|
|
const script = step.command === 'npm' && step.args[0] === 'run' ? step.args[1] : undefined
|
|
if (script !== undefined && forbidden.has(script)) {
|
|
throw new Error(
|
|
`${packageEntry.name}: build step \`npm run ${script}\` is forbidden. ${manifest.forbiddenBuildScripts.why}`
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// `delete` was dropped with the code that implemented it: accepting a policy
|
|
// nothing honours would silently ship maps that do not match the bundle.
|
|
export const SOURCEMAP_POLICIES = new Set(['include'])
|
|
|
|
/** An unrecognised policy is a manifest bug, not a default to fall through to. */
|
|
export function assertSourcemapPolicy(manifest) {
|
|
const policy = manifest.sourcemaps?.policy
|
|
if (!SOURCEMAP_POLICIES.has(policy)) {
|
|
throw new Error(
|
|
`sourcemaps.policy must be one of ${[...SOURCEMAP_POLICIES].join(', ')}, got ${JSON.stringify(policy)}`
|
|
)
|
|
}
|
|
return policy
|
|
}
|
|
|
|
/**
|
|
* pnpm keys the patched package directory and the lockfile entry by the
|
|
* sha256 of the patch file itself, so a regenerated patch that leaves
|
|
* pnpm-lock.yaml alone fails `--frozen-lockfile` on every machine but the
|
|
* author's.
|
|
*/
|
|
export function patchHash(patchText) {
|
|
return createHash('sha256').update(patchText, 'utf8').digest('hex')
|
|
}
|
|
|
|
function lockfilePatchHashPattern(packageKey) {
|
|
// Unscoped keys such as `node-pty@1.1.0` are emitted unquoted.
|
|
return new RegExp(`(^ '?${escapeRegExp(packageKey)}'?:\\n hash: )([0-9a-f]{64})$`, 'm')
|
|
}
|
|
|
|
export function readLockfilePatchHash(lockfileText, packageKey) {
|
|
const match = lockfilePatchHashPattern(packageKey).exec(lockfileText)
|
|
if (!match) {
|
|
throw new Error(`pnpm-lock.yaml has no patchedDependencies entry for '${packageKey}'`)
|
|
}
|
|
return match[2]
|
|
}
|
|
|
|
function lockfileResolutionHashPattern(packageKey) {
|
|
const separator = packageKey.lastIndexOf('@')
|
|
const name = escapeRegExp(packageKey.slice(0, separator))
|
|
const version = escapeRegExp(packageKey.slice(separator + 1))
|
|
// Two spellings: `name@version(patch_hash=…)` in dependency keys, and a bare
|
|
// `: version(patch_hash=…)` under `version:` and in resolved dependency maps.
|
|
return new RegExp(`(?:${name}@|: )${version}\\(patch_hash=([0-9a-f]{64})\\)`, 'g')
|
|
}
|
|
|
|
/**
|
|
* pnpm repeats the hash inside every resolution key that depends on the patched
|
|
* package, not just in `patchedDependencies`. Updating one and not the other leaves
|
|
* a lockfile that installs on a warm store and drifts on a cold one, which is CI.
|
|
*/
|
|
export function readLockfileResolutionHashes(lockfileText, packageKey) {
|
|
return Array.from(
|
|
lockfileText.matchAll(lockfileResolutionHashPattern(packageKey)),
|
|
(match) => match[1]
|
|
)
|
|
}
|
|
|
|
export function lockfilePatchHashIsStale(lockfileText, packageKey, hash) {
|
|
return (
|
|
readLockfilePatchHash(lockfileText, packageKey) !== hash ||
|
|
readLockfileResolutionHashes(lockfileText, packageKey).some((value) => value !== hash)
|
|
)
|
|
}
|
|
|
|
export function updateLockfilePatchHash(lockfileText, packageKey, hash) {
|
|
readLockfilePatchHash(lockfileText, packageKey)
|
|
return lockfileText
|
|
.replace(lockfilePatchHashPattern(packageKey), `$1${hash}`)
|
|
.replace(lockfileResolutionHashPattern(packageKey), (match, current) =>
|
|
match.replace(`patch_hash=${current}`, `patch_hash=${hash}`)
|
|
)
|
|
}
|
|
|
|
export function firstDifferenceIndex(left, right) {
|
|
const limit = Math.min(left.length, right.length)
|
|
for (let index = 0; index < limit; index += 1) {
|
|
if (left[index] !== right[index]) {
|
|
return index
|
|
}
|
|
}
|
|
return left.length === right.length ? -1 : limit
|
|
}
|
|
|
|
export function formatCheckFailure({ name, patchPath, committed, regenerated }) {
|
|
const index = firstDifferenceIndex(committed, regenerated)
|
|
const committedFiles = splitPatchEntries(committed).map((entry) => entry.path)
|
|
const regeneratedFiles = splitPatchEntries(regenerated).map((entry) => entry.path)
|
|
return [
|
|
`${name}: ${patchPath} is not what the pinned upstream build produces.`,
|
|
` committed: ${Buffer.byteLength(committed)} bytes, files [${committedFiles.join(', ')}]`,
|
|
` regenerated: ${Buffer.byteLength(regenerated)} bytes, files [${regeneratedFiles.join(', ')}]`,
|
|
` first difference at character ${index}`,
|
|
'',
|
|
'The bundle hunks are generated. Do not edit them. Change the source patch',
|
|
'instead and regenerate both files:',
|
|
'',
|
|
' node config/scripts/regenerate-xterm-patches.mjs --write',
|
|
'',
|
|
'See docs/reference/xterm-patch-regeneration.md.'
|
|
].join('\n')
|
|
}
|
|
|
|
// npm ships as `npm.cmd` on Windows, and execFile applies no PATHEXT and refuses
|
|
// a `.cmd` target without a shell (CVE-2024-27980). git and tar are real .exe.
|
|
const WINDOWS_SHIM_COMMANDS = new Set(['npm', 'npx', 'pnpm', 'yarn'])
|
|
|
|
function run(command, args, options = {}) {
|
|
const shim = process.platform === 'win32' && WINDOWS_SHIM_COMMANDS.has(command)
|
|
return execFileSync(shim ? `${command}.cmd` : command, args, {
|
|
encoding: 'utf8',
|
|
maxBuffer: 256 * 1024 * 1024,
|
|
stdio: ['ignore', 'pipe', 'inherit'],
|
|
shell: shim,
|
|
...options
|
|
})
|
|
}
|
|
|
|
function listFilesRelative(root, base = root) {
|
|
const files = []
|
|
for (const entry of readdirSync(base, { withFileTypes: true })) {
|
|
const absolute = path.join(base, entry.name)
|
|
if (entry.isDirectory()) {
|
|
files.push(...listFilesRelative(root, absolute))
|
|
} else if (entry.isFile()) {
|
|
files.push(path.relative(root, absolute))
|
|
}
|
|
}
|
|
return files.sort()
|
|
}
|
|
|
|
function sameBytes(left, right) {
|
|
return (
|
|
statSync(left).size === statSync(right).size && readFileSync(left).equals(readFileSync(right))
|
|
)
|
|
}
|
|
|
|
function fetchPristinePackage(packageEntry, workDir) {
|
|
const target = path.join(workDir, 'pristine', packageEntry.name.replace(/[@/]/g, '_'))
|
|
rmSync(target, { recursive: true, force: true })
|
|
mkdirSync(target, { recursive: true })
|
|
const spec = `${packageEntry.name}@${packageEntry.version}`
|
|
const output = run('npm', ['pack', spec, '--pack-destination', target, '--silent'], {
|
|
cwd: workDir
|
|
})
|
|
const tarball = path.join(target, output.trim().split('\n').at(-1).trim())
|
|
run('tar', ['xzf', tarball, '-C', target])
|
|
return path.join(target, 'package')
|
|
}
|
|
|
|
function hasCommit(root, commit) {
|
|
try {
|
|
return run('git', ['cat-file', '-t', commit], { cwd: root, stdio: 'pipe' }).trim() === 'commit'
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
function ensureUpstreamCheckout(manifest, workDir) {
|
|
const root = path.join(workDir, 'upstream')
|
|
const { repository, commit } = manifest.upstream
|
|
if (!existsSync(path.join(root, '.git'))) {
|
|
mkdirSync(root, { recursive: true })
|
|
run('git', ['init', '--quiet'], { cwd: root })
|
|
run('git', ['remote', 'add', 'origin', repository], { cwd: root })
|
|
}
|
|
if (!hasCommit(root, commit)) {
|
|
run('git', ['fetch', '--depth=1', 'origin', commit], { cwd: root, stdio: 'inherit' })
|
|
}
|
|
run('git', ['checkout', '--quiet', '--detach', commit], { cwd: root })
|
|
run('git', ['reset', '--quiet', '--hard', commit], { cwd: root })
|
|
return root
|
|
}
|
|
|
|
function ensureDependencies(upstreamRoot, manifest) {
|
|
const lockfile = path.join(upstreamRoot, 'package-lock.json')
|
|
const stamp = path.join(upstreamRoot, 'node_modules', '.orca-xterm-install-stamp')
|
|
const want = `${manifest.upstream.commit}\n${statSync(lockfile).size}\n`
|
|
if (existsSync(stamp) && readFileSync(stamp, 'utf8') === want) {
|
|
return
|
|
}
|
|
run('npm', ['ci'], {
|
|
cwd: upstreamRoot,
|
|
env: { ...process.env, PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1', PUPPETEER_SKIP_DOWNLOAD: '1' }
|
|
})
|
|
assertToolchain(upstreamRoot, manifest)
|
|
writeFileSync(stamp, want)
|
|
}
|
|
|
|
function assertToolchain(upstreamRoot, manifest) {
|
|
const expected = manifest.toolchain
|
|
for (const [name, version] of Object.entries(expected)) {
|
|
if (name === 'why') {
|
|
continue
|
|
}
|
|
const installed = path.join(upstreamRoot, 'node_modules', name, 'package.json')
|
|
if (!existsSync(installed)) {
|
|
throw new Error(
|
|
`Upstream install is missing ${name}. The pinned toolchain is no longer resolvable; see the tsgo note in docs/reference/xterm-patch-regeneration.md.`
|
|
)
|
|
}
|
|
const actual = JSON.parse(readFileSync(installed, 'utf8')).version
|
|
if (actual !== version) {
|
|
throw new Error(
|
|
`Upstream ${name} resolved to ${actual}, manifest expects ${version}. Update the toolchain block only together with a verified rebuild.`
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The published src/ must equal the pinned commit's src/ apart from the version
|
|
* stamp publish.js rewrites. If it does not, the manifest points at the wrong
|
|
* commit and every hunk below would be nonsense.
|
|
*/
|
|
function assertPristineSourceMatches(pristineDir, upstreamRoot, packageEntry) {
|
|
const stampFile = packageEntry.versionStampFile
|
|
const sourceRoot = path.join(pristineDir, 'src')
|
|
const drifted = listFilesRelative(sourceRoot)
|
|
.map((relative) => path.join('src', relative))
|
|
.filter((relative) => relative !== stampFile)
|
|
.filter(
|
|
(relative) =>
|
|
!sameBytes(
|
|
path.join(pristineDir, relative),
|
|
path.join(upstreamRoot, packageEntry.packageDir, relative)
|
|
)
|
|
)
|
|
if (drifted.length > 0) {
|
|
throw new Error(
|
|
`Published src/ does not match ${packageEntry.packageDir} at the pinned commit: ${drifted.join(', ')}`
|
|
)
|
|
}
|
|
}
|
|
|
|
function buildPackage(upstreamRoot, packageEntry, manifest) {
|
|
const packageRoot = path.join(upstreamRoot, packageEntry.packageDir)
|
|
for (const directory of ['lib', 'out', 'out-esbuild']) {
|
|
rmSync(path.join(packageRoot, directory), { recursive: true, force: true })
|
|
}
|
|
const stampPath = path.join(packageRoot, packageEntry.versionStampFile)
|
|
writeFileSync(
|
|
stampPath,
|
|
stampVersionSource(readFileSync(stampPath, 'utf8'), packageEntry.version)
|
|
)
|
|
assertBuildStepsAllowed(manifest)
|
|
for (const step of packageEntry.build) {
|
|
run(step.command, step.args, { cwd: path.join(packageRoot, step.cwd), stdio: 'inherit' })
|
|
}
|
|
}
|
|
|
|
/** Proves the pinned toolchain still reproduces the untouched published bundles. */
|
|
function assertReproducesPristineBundles(pristineDir, upstreamRoot, packageEntry) {
|
|
const packageRoot = path.join(upstreamRoot, packageEntry.packageDir)
|
|
const drifted = listFilesRelative(pristineDir)
|
|
.filter((relative) =>
|
|
packageEntry.generatedPaths.some((prefix) => toPosix(relative).startsWith(prefix))
|
|
)
|
|
.filter(
|
|
(relative) => !sameBytes(path.join(pristineDir, relative), path.join(packageRoot, relative))
|
|
)
|
|
if (drifted.length > 0) {
|
|
throw new Error(
|
|
[
|
|
`Rebuilding ${packageEntry.name}@${packageEntry.version} from the pinned commit did not reproduce the published bundles:`,
|
|
...drifted.map((relative) => ` ${relative}`),
|
|
'',
|
|
'Refusing to emit a patch. Either the toolchain drifted or the build ran in the',
|
|
'wrong order (a dev `npm run setup` pass de-minifies lib/*.mjs).'
|
|
].join('\n')
|
|
)
|
|
}
|
|
}
|
|
|
|
function toPosix(value) {
|
|
return value.split(path.sep).join('/')
|
|
}
|
|
|
|
function overlayBuildOutput(pristineDir, upstreamRoot, packageEntry, destination) {
|
|
rmSync(destination, { recursive: true, force: true })
|
|
cpSync(pristineDir, destination, { recursive: true })
|
|
const packageRoot = path.join(upstreamRoot, packageEntry.packageDir)
|
|
for (const relative of listFilesRelative(pristineDir)) {
|
|
// package.json carries the registry's version/commit stamp, which the build
|
|
// tree has no way to reproduce and which we never want to patch.
|
|
if (relative === 'package.json') {
|
|
continue
|
|
}
|
|
const built = path.join(packageRoot, relative)
|
|
if (!existsSync(built)) {
|
|
throw new Error(`Published file has no build-tree counterpart: ${relative}`)
|
|
}
|
|
copyFileSync(built, path.join(destination, relative))
|
|
}
|
|
}
|
|
|
|
function diffFolders(folderA, folderB) {
|
|
let stdout
|
|
try {
|
|
stdout = execFileSync('git', [...PNPM_DIFF_FLAGS, folderA, folderB], {
|
|
encoding: 'utf8',
|
|
maxBuffer: 512 * 1024 * 1024,
|
|
env: pnpmDiffEnvironment(),
|
|
stdio: ['ignore', 'pipe', 'pipe']
|
|
})
|
|
} catch (error) {
|
|
// `git diff --no-index` exits 1 whenever it finds differences.
|
|
if (error.status !== 1 || error.stderr?.length > 0) {
|
|
throw error
|
|
}
|
|
stdout = error.stdout
|
|
}
|
|
return normalizePnpmDiff(stdout, folderA, folderB)
|
|
}
|
|
|
|
/** The source of truth for the hand-written half: what the checkout itself holds. */
|
|
function diffCheckoutSource(packageRoot) {
|
|
return run('git', [...CHECKOUT_DIFF_FLAGS, '--', 'src/'], {
|
|
cwd: packageRoot,
|
|
env: pnpmDiffEnvironment(),
|
|
maxBuffer: 64 * 1024 * 1024
|
|
})
|
|
}
|
|
|
|
function regeneratePackage(packageEntry, manifest, context) {
|
|
const { workDir, repoRoot } = context
|
|
const pristineDir = fetchPristinePackage(packageEntry, workDir)
|
|
const published = JSON.parse(readFileSync(path.join(pristineDir, 'package.json'), 'utf8'))
|
|
assertPublishedCommit(published, packageEntry, manifest.upstream.commit)
|
|
|
|
const upstreamRoot = ensureUpstreamCheckout(manifest, workDir)
|
|
ensureDependencies(upstreamRoot, manifest)
|
|
assertPristineSourceMatches(pristineDir, upstreamRoot, packageEntry)
|
|
|
|
buildPackage(upstreamRoot, packageEntry, manifest)
|
|
assertReproducesPristineBundles(pristineDir, upstreamRoot, packageEntry)
|
|
|
|
run('git', ['reset', '--quiet', '--hard', manifest.upstream.commit], { cwd: upstreamRoot })
|
|
run('git', ['apply', '--whitespace=nowarn', path.join(repoRoot, packageEntry.sourcePatch)], {
|
|
cwd: path.join(upstreamRoot, packageEntry.packageDir)
|
|
})
|
|
buildPackage(upstreamRoot, packageEntry, manifest)
|
|
|
|
const patchedDir = path.join(workDir, 'patched', packageEntry.name.replace(/[@/]/g, '_'))
|
|
overlayBuildOutput(pristineDir, upstreamRoot, packageEntry, patchedDir)
|
|
|
|
// Leave the checkout diffable: the pinned commit plus the source patch, with
|
|
// no publish-time version stamp mixed in, so `git diff` there is the source
|
|
// patch and nothing else.
|
|
run('git', ['checkout', '--', packageEntry.versionStampFile], {
|
|
cwd: path.join(upstreamRoot, packageEntry.packageDir)
|
|
})
|
|
|
|
const source = diffCheckoutSource(path.join(upstreamRoot, packageEntry.packageDir))
|
|
const patch = diffFolders(pristineDir, patchedDir)
|
|
assertSourceDerivationsAgree(source, patch)
|
|
return { patch, source }
|
|
}
|
|
|
|
export function regenerateXtermPatches({
|
|
mode,
|
|
repoRoot = DEFAULT_REPO_ROOT,
|
|
workDir = path.join(tmpdir(), 'orca-xterm-patch-build'),
|
|
log = console.info
|
|
} = {}) {
|
|
const manifest = JSON.parse(readFileSync(path.join(repoRoot, MANIFEST_RELATIVE_PATH), 'utf8'))
|
|
assertBuildStepsAllowed(manifest)
|
|
assertSourcemapPolicy(manifest)
|
|
mkdirSync(workDir, { recursive: true })
|
|
|
|
const lockfilePath = path.join(repoRoot, 'pnpm-lock.yaml')
|
|
let lockfile = readFileSync(lockfilePath, 'utf8')
|
|
let lockfileChanged = false
|
|
|
|
const failures = []
|
|
for (const packageEntry of manifest.packages) {
|
|
const shortCommit = manifest.upstream.commit.slice(0, 12)
|
|
log(`${packageEntry.name}@${packageEntry.version}: regenerating from ${shortCommit}`)
|
|
const { patch: regenerated, source: canonicalSource } = regeneratePackage(
|
|
packageEntry,
|
|
manifest,
|
|
{ workDir, repoRoot }
|
|
)
|
|
const patchPath = path.join(repoRoot, packageEntry.patch)
|
|
const sourcePatchPath = path.join(repoRoot, packageEntry.sourcePatch)
|
|
const packageKey = `${packageEntry.name}@${packageEntry.version}`
|
|
const hash = patchHash(regenerated)
|
|
|
|
if (mode === 'write') {
|
|
writeFileSync(patchPath, regenerated)
|
|
writeFileSync(sourcePatchPath, canonicalSource)
|
|
log(` wrote ${packageEntry.patch} (${Buffer.byteLength(regenerated)} bytes)`)
|
|
log(` wrote ${packageEntry.sourcePatch} (${Buffer.byteLength(canonicalSource)} bytes)`)
|
|
if (lockfilePatchHashIsStale(lockfile, packageKey, hash)) {
|
|
lockfile = updateLockfilePatchHash(lockfile, packageKey, hash)
|
|
lockfileChanged = true
|
|
log(` updated pnpm-lock.yaml patch hash to ${hash}`)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if (lockfilePatchHashIsStale(lockfile, packageKey, hash)) {
|
|
const stale = Array.from(
|
|
new Set(readLockfileResolutionHashes(lockfile, packageKey).filter((v) => v !== hash))
|
|
)
|
|
failures.push(
|
|
[
|
|
`${packageKey}: pnpm-lock.yaml records a stale patch hash.`,
|
|
` patchedDependencies: ${readLockfilePatchHash(lockfile, packageKey)}`,
|
|
` resolution keys: ${stale.length > 0 ? stale.join(', ') : 'in sync'}`,
|
|
` patch: ${hash}`,
|
|
'',
|
|
'pnpm keys the patched package by the sha256 of the patch file, so',
|
|
'`pnpm install --frozen-lockfile` will fail. Rerun with --write.'
|
|
].join('\n')
|
|
)
|
|
}
|
|
|
|
const committed = readFileSync(patchPath, 'utf8')
|
|
if (committed !== regenerated) {
|
|
failures.push(
|
|
formatCheckFailure({
|
|
name: packageEntry.name,
|
|
patchPath: packageEntry.patch,
|
|
committed,
|
|
regenerated
|
|
})
|
|
)
|
|
continue
|
|
}
|
|
const committedSource = readFileSync(sourcePatchPath, 'utf8')
|
|
if (committedSource !== canonicalSource) {
|
|
failures.push(
|
|
formatCheckFailure({
|
|
name: packageEntry.name,
|
|
patchPath: packageEntry.sourcePatch,
|
|
committed: committedSource,
|
|
regenerated: canonicalSource
|
|
})
|
|
)
|
|
continue
|
|
}
|
|
log(` in sync (${Buffer.byteLength(regenerated)} bytes)`)
|
|
}
|
|
|
|
if (lockfileChanged) {
|
|
writeFileSync(lockfilePath, lockfile)
|
|
}
|
|
if (failures.length > 0) {
|
|
throw new Error(failures.join('\n\n'))
|
|
}
|
|
}
|
|
|
|
const USAGE =
|
|
'Usage: regenerate-xterm-patches.mjs [--check | --write] [--work-dir=<path>]\n' +
|
|
' --check (default) verifies the shipped patches match the pinned upstream build;\n' +
|
|
' --write regenerates them from config/patches/xterm-src/. Build outside this repo:\n' +
|
|
' tsc otherwise walks up into our node_modules. See\n' +
|
|
' docs/reference/xterm-patch-regeneration.md.'
|
|
|
|
function main(argv) {
|
|
if (argv.includes('--help') || argv.includes('-h')) {
|
|
console.info(USAGE)
|
|
return
|
|
}
|
|
// --check is the default, so an unrecognised flag would otherwise silently run a full
|
|
// upstream build instead of whatever the caller meant.
|
|
const known = (v) =>
|
|
!v.startsWith('-') || ['--write', '--check'].includes(v) || v.startsWith('--work-dir=')
|
|
const unknown = argv.filter((value) => !known(value))
|
|
if (unknown.length > 0) {
|
|
throw new Error(`Unknown option: ${unknown.join(', ')}\n${USAGE}`)
|
|
}
|
|
const write = argv.includes('--write')
|
|
const check = argv.includes('--check') || !write
|
|
if (write && argv.includes('--check')) {
|
|
throw new Error('Pass either --write or --check, not both')
|
|
}
|
|
const workDirArgument = argv.find((value) => value.startsWith('--work-dir='))
|
|
regenerateXtermPatches({
|
|
mode: write ? 'write' : 'check',
|
|
workDir: workDirArgument ? path.resolve(workDirArgument.slice('--work-dir='.length)) : undefined
|
|
})
|
|
if (check) {
|
|
console.info('xterm patches are in sync with the pinned upstream build.')
|
|
}
|
|
}
|
|
|
|
// realpathSync so a symlinked checkout path still registers as a direct run.
|
|
const invokedPath = process.argv[1] ? pathToFileURL(realpathSync(process.argv[1])).href : null
|
|
if (invokedPath === import.meta.url) {
|
|
try {
|
|
main(process.argv.slice(2))
|
|
} catch (error) {
|
|
console.error(`\n${error.message}\n`)
|
|
process.exit(1)
|
|
}
|
|
}
|