|
| 1 | +#!/usr/bin/env node |
| 2 | +// Pins the contract that the installed `extension` canary resolves its |
| 3 | +// `extension-develop` runtime from this workspace's own node_modules, |
| 4 | +// never from a surrounding monorepo's programs/develop. Captures the |
| 5 | +// resolver-escape regression where the workspace walker climbed past |
| 6 | +// node_modules into an outer extension.js checkout and either threw |
| 7 | +// "Local extension-develop runtime is not built" or silently loaded the |
| 8 | +// wrong runtime. |
| 9 | + |
| 10 | +import fs from 'node:fs' |
| 11 | +import os from 'node:os' |
| 12 | +import path from 'node:path' |
| 13 | +import {spawn} from 'node:child_process' |
| 14 | +import {fileURLToPath} from 'node:url' |
| 15 | + |
| 16 | +const __filename = fileURLToPath(import.meta.url) |
| 17 | +const __dirname = path.dirname(__filename) |
| 18 | +const repoRoot = path.resolve(__dirname, '..') |
| 19 | +const probePath = path.join(__dirname, 'probe-extension-develop-resolve.cjs') |
| 20 | + |
| 21 | +const installedExtensionBin = path.join( |
| 22 | + repoRoot, |
| 23 | + 'node_modules', |
| 24 | + 'extension', |
| 25 | + 'bin', |
| 26 | + 'extension.cjs' |
| 27 | +) |
| 28 | +const installedExtensionDevelopRoot = path.join( |
| 29 | + repoRoot, |
| 30 | + 'node_modules', |
| 31 | + 'extension-develop' |
| 32 | +) |
| 33 | + |
| 34 | +if (!fs.existsSync(installedExtensionBin)) { |
| 35 | + console.error(`✖ Canary not installed: missing ${installedExtensionBin}`) |
| 36 | + console.error(' Run `pnpm install` in _FUTURE/examples first.') |
| 37 | + process.exit(1) |
| 38 | +} |
| 39 | + |
| 40 | +if (!fs.existsSync(installedExtensionDevelopRoot)) { |
| 41 | + console.error(`✖ Missing installed runtime: ${installedExtensionDevelopRoot}`) |
| 42 | + process.exit(1) |
| 43 | +} |
| 44 | + |
| 45 | +const fakeProject = fs.mkdtempSync( |
| 46 | + path.join(os.tmpdir(), 'extjs-resolve-probe-') |
| 47 | +) |
| 48 | +fs.writeFileSync( |
| 49 | + path.join(fakeProject, 'manifest.json'), |
| 50 | + JSON.stringify( |
| 51 | + {manifest_version: 3, name: 'probe', version: '0.0.1'}, |
| 52 | + null, |
| 53 | + 2 |
| 54 | + ) |
| 55 | +) |
| 56 | + |
| 57 | +const result = await new Promise((resolve) => { |
| 58 | + const child = spawn( |
| 59 | + process.execPath, |
| 60 | + [installedExtensionBin, 'dev', fakeProject, '--no-telemetry'], |
| 61 | + { |
| 62 | + cwd: repoRoot, |
| 63 | + env: { |
| 64 | + ...process.env, |
| 65 | + NODE_OPTIONS: |
| 66 | + `${process.env.NODE_OPTIONS ?? ''} --require ${probePath}`.trim(), |
| 67 | + // Strip any stale env override that could mask resolver behavior. |
| 68 | + EXTENSION_DEVELOP_ROOT: '' |
| 69 | + }, |
| 70 | + stdio: ['ignore', 'pipe', 'pipe'] |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + let stdout = '' |
| 75 | + let stderr = '' |
| 76 | + child.stdout.on('data', (d) => (stdout += d.toString())) |
| 77 | + child.stderr.on('data', (d) => (stderr += d.toString())) |
| 78 | + |
| 79 | + const killTimer = setTimeout(() => { |
| 80 | + child.kill('SIGKILL') |
| 81 | + }, 60_000) |
| 82 | + |
| 83 | + child.on('close', (code) => { |
| 84 | + clearTimeout(killTimer) |
| 85 | + resolve({code: code ?? 1, stdout, stderr}) |
| 86 | + }) |
| 87 | + child.on('error', (err) => { |
| 88 | + clearTimeout(killTimer) |
| 89 | + resolve({code: 1, stdout, stderr: String(err?.message || err)}) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +fs.rmSync(fakeProject, {recursive: true, force: true}) |
| 94 | + |
| 95 | +const match = result.stderr.match(/__EXT_DEV_RESOLVED__::(.+)$/m) |
| 96 | + |
| 97 | +if (!match) { |
| 98 | + if ( |
| 99 | + /Local extension-develop runtime is not built/.test( |
| 100 | + `${result.stdout}\n${result.stderr}` |
| 101 | + ) |
| 102 | + ) { |
| 103 | + console.error(result.stdout) |
| 104 | + console.error(result.stderr) |
| 105 | + console.error( |
| 106 | + '✖ Resolver-escape regression: the canary CLI walked into an outer ' + |
| 107 | + 'workspace and demanded its programs/develop be compiled. The ' + |
| 108 | + 'walker must bail when startDir is inside node_modules.' |
| 109 | + ) |
| 110 | + process.exit(1) |
| 111 | + } |
| 112 | + |
| 113 | + console.error(result.stdout) |
| 114 | + console.error(result.stderr) |
| 115 | + console.error( |
| 116 | + '✖ Probe never observed extension-develop being loaded ' + |
| 117 | + `(exit ${result.code})` |
| 118 | + ) |
| 119 | + process.exit(1) |
| 120 | +} |
| 121 | + |
| 122 | +const resolved = match[1].trim() |
| 123 | +const expectedPrefix = installedExtensionDevelopRoot + path.sep |
| 124 | + |
| 125 | +if (!resolved.startsWith(expectedPrefix)) { |
| 126 | + console.error( |
| 127 | + '✖ extension-develop resolved outside the local install:\n' + |
| 128 | + ` expected prefix: ${expectedPrefix}\n` + |
| 129 | + ` resolved: ${resolved}` |
| 130 | + ) |
| 131 | + process.exit(1) |
| 132 | +} |
| 133 | + |
| 134 | +console.log(`✔ extension-develop resolved to ${resolved}`) |
0 commit comments