diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ef29f50..1ef053b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,12 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' + - name: Setup Bun for runtime regression tests + if: matrix.node-version == '24.x' + uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + with: + bun-version: '1.3.14' + - run: npm ci - run: npm run lint - run: npm test diff --git a/lib/pi/command-runner.js b/lib/pi/command-runner.js index e287c78..cfda88e 100644 --- a/lib/pi/command-runner.js +++ b/lib/pi/command-runner.js @@ -158,7 +158,10 @@ function runCommand({ const entryPoint = path.resolve(requireString(packageRoot, 'packageRoot'), 'bin/index.js'); const cwd = path.resolve(requireString(projectRoot, 'projectRoot')); const childEnv = buildCliEnvironment(env); + // Compiled Bun hosts must run the CLI, not their embedded application entrypoint. + if (process.versions.bun) childEnv.BUN_BE_BUN = '1'; const argv = Array.isArray(args) ? args.map((arg) => String(arg)) : []; + const runtimeArgs = process.versions.bun ? ['--no-env-file'] : []; const outputLimit = Number.isFinite(maxOutputBytes) && maxOutputBytes >= 0 ? maxOutputBytes : 0; const timeoutLimit = Number.isFinite(timeoutMs) && timeoutMs >= 0 ? timeoutMs : 0; @@ -251,7 +254,7 @@ function runCommand({ } try { - child = spawn(process.execPath, [entryPoint, ...argv], { + child = spawn(process.execPath, [...runtimeArgs, entryPoint, ...argv], { cwd, env: childEnv, shell: false, diff --git a/tests/pi-command-runner.test.js b/tests/pi-command-runner.test.js index 0fa13e4..d06dec3 100644 --- a/tests/pi-command-runner.test.js +++ b/tests/pi-command-runner.test.js @@ -1,6 +1,7 @@ const fs = require('fs'); const os = require('os'); const path = require('path'); +const { spawnSync } = require('child_process'); const { CONFIG_ENV_KEYS, ConfluencePiError, @@ -46,6 +47,58 @@ test('executes package-local argv and parses complete JSON', async () => { } }); +const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status === 0; + +(bunAvailable ? test.each : test.skip.each)(['plain', 'compiled'])('executes the CLI from a %s Bun host', async (runtime) => { + const packageRoot = fakePackage(` + process.stdout.write(JSON.stringify({ argv: process.argv.slice(2), cwd: process.cwd(), + domain: process.env.CONFLUENCE_DOMAIN, insecure: process.env.CONFLUENCE_ALLOW_INSECURE_HTTP, + cookie: process.env.CONFLUENCE_COOKIE, injected: process.env.PROJECT_INJECTION })); + `); + const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'pi-untrusted-project-')); + fs.writeFileSync(path.join(projectRoot, '.env'), 'CONFLUENCE_DOMAIN=untrusted.invalid\nCONFLUENCE_ALLOW_INSECURE_HTTP=true\n'); + fs.writeFileSync(path.join(projectRoot, '.env.local'), 'CONFLUENCE_COOKIE=untrusted-cookie\nPROJECT_INJECTION=loaded\n'); + const hostSource = path.join(packageRoot, 'host.js'); + const hostBinary = path.join(packageRoot, process.platform === 'win32' ? 'host.exe' : 'host'); + fs.writeFileSync(hostSource, ` + if (process.argv.length > 2) throw new Error('Compiled host restarted instead of running the CLI'); + const { runCommand } = require(${JSON.stringify(require.resolve('../lib/pi/command-runner'))}); + runCommand({ + packageRoot: ${JSON.stringify(packageRoot)}, + projectRoot: ${JSON.stringify(projectRoot)}, + args: ['--json', 'info', '123; echo unsafe'], + env: { PATH: '', CONFLUENCE_DOMAIN: 'trusted.invalid' }, + timeoutMs: 3000, + maxOutputBytes: 4096, + expectJson: true, + mutation: false, + }).then(result => process.stdout.write(JSON.stringify(result.json))) + .catch(error => { console.error(error.code); process.exitCode = 1; }); + `); + try { + if (runtime === 'compiled') { + const build = spawnSync('bun', ['build', '--compile', hostSource, '--outfile', hostBinary], { + encoding: 'utf8', timeout: 20000, + }); + expect(build.status).toBe(0); + } + const env = { ...process.env }; + delete env.BUN_BE_BUN; + const executable = runtime === 'compiled' ? hostBinary : 'bun'; + const args = runtime === 'compiled' ? [] : ['--no-env-file', hostSource]; + const result = spawnSync(executable, args, { env, encoding: 'utf8', timeout: 5000 }); + expect(result.status).toBe(0); + expect(JSON.parse(result.stdout)).toEqual({ + argv: ['--json', 'info', '123; echo unsafe'], + cwd: fs.realpathSync(projectRoot), + domain: 'trusted.invalid', + }); + } finally { + cleanup(packageRoot); + cleanup(projectRoot); + } +}, 30000); + test('rejects malformed JSON with INVALID_JSON', async () => { const packageRoot = fakePackage('process.stdout.write(\'{not json\');'); const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'pi-project-'));