From 0c85c6f938e2eac1afa16ec3d1c49e727613fb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Horv=C3=A1th?= <11652096+erikh3@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:24:29 +0100 Subject: [PATCH 1/4] fix: run cli correctly in compiled bun hosts --- README.md | 2 ++ lib/pi/command-runner.js | 2 ++ tests/pi-command-runner.test.js | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/README.md b/README.md index ba0f6d1..f734d65 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ npm ci pi install /absolute/path/to/confluence-cli ``` +The extension supports Node.js, Bun, and compiled Bun hosts such as oh-my-pi. Bun-hosted CLI subprocesses set `BUN_BE_BUN=1` so a compiled executable runs the package-local CLI instead of restarting its embedded application. No global environment export or separate Node launcher is needed. + For a Server/Data Center instance at `confluence.example.com`, start Pi from a shell configured like this when protected writes are intended: ```bash diff --git a/lib/pi/command-runner.js b/lib/pi/command-runner.js index e287c78..9c12632 100644 --- a/lib/pi/command-runner.js +++ b/lib/pi/command-runner.js @@ -158,6 +158,8 @@ 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 outputLimit = Number.isFinite(maxOutputBytes) && maxOutputBytes >= 0 ? maxOutputBytes : 0; const timeoutLimit = Number.isFinite(timeoutMs) && timeoutMs >= 0 ? timeoutMs : 0; diff --git a/tests/pi-command-runner.test.js b/tests/pi-command-runner.test.js index 0fa13e4..f2df616 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,47 @@ test('executes package-local argv and parses complete JSON', async () => { } }); +const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status === 0; + +(bunAvailable ? test : test.skip)('executes the CLI instead of restarting a compiled Bun host', async () => { + const packageRoot = fakePackage(` + process.stdout.write(JSON.stringify({ argv: process.argv.slice(2), cwd: process.cwd() })); + `); + 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(packageRoot)}, + args: ['--json', 'info', '123; echo unsafe'], + env: { PATH: '' }, + 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 { + 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 result = spawnSync(hostBinary, [], { 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(packageRoot), + }); + } finally { + cleanup(packageRoot); + } +}, 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-')); From 2e8c04c0e0e3ce3313cd424b35c216df7b5ed4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Horv=C3=A1th?= <11652096+erikh3@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:31:49 +0100 Subject: [PATCH 2/4] test: cover plain bun runtime compatibility --- README.md | 2 +- tests/pi-command-runner.test.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f734d65..e620da1 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ npm ci pi install /absolute/path/to/confluence-cli ``` -The extension supports Node.js, Bun, and compiled Bun hosts such as oh-my-pi. Bun-hosted CLI subprocesses set `BUN_BE_BUN=1` so a compiled executable runs the package-local CLI instead of restarting its embedded application. No global environment export or separate Node launcher is needed. +The extension supports Node.js and Bun, including compiled Bun executables. CLI subprocesses select the correct runtime automatically. For a Server/Data Center instance at `confluence.example.com`, start Pi from a shell configured like this when protected writes are intended: diff --git a/tests/pi-command-runner.test.js b/tests/pi-command-runner.test.js index f2df616..2878d30 100644 --- a/tests/pi-command-runner.test.js +++ b/tests/pi-command-runner.test.js @@ -49,7 +49,7 @@ test('executes package-local argv and parses complete JSON', async () => { const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status === 0; -(bunAvailable ? test : test.skip)('executes the CLI instead of restarting a compiled Bun host', async () => { +(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() })); `); @@ -71,13 +71,17 @@ const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status = .catch(error => { console.error(error.code); process.exitCode = 1; }); `); try { - const build = spawnSync('bun', ['build', '--compile', hostSource, '--outfile', hostBinary], { - encoding: 'utf8', timeout: 20000, - }); - expect(build.status).toBe(0); + 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 result = spawnSync(hostBinary, [], { env, encoding: 'utf8', timeout: 5000 }); + const executable = runtime === 'compiled' ? hostBinary : 'bun'; + const args = runtime === 'compiled' ? [] : [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'], From 2bafb5ee85b4af4cf7711a48d0801e693e417fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Horv=C3=A1th?= <11652096+erikh3@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:38:10 +0100 Subject: [PATCH 3/4] docs: shorten compiled bun runtime note --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e620da1..af57bad 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ npm ci pi install /absolute/path/to/confluence-cli ``` -The extension supports Node.js and Bun, including compiled Bun executables. CLI subprocesses select the correct runtime automatically. +Compiled Bun hosts run CLI subprocesses automatically without additional configuration. For a Server/Data Center instance at `confluence.example.com`, start Pi from a shell configured like this when protected writes are intended: From 88728dbf6d1ef61b617cac5f1ed677d3aa1214c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Horv=C3=A1th?= <11652096+erikh3@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:58:47 +0100 Subject: [PATCH 4/4] fix: isolate bun cli children from project env files --- .github/workflows/ci.yml | 6 ++++++ README.md | 2 -- lib/pi/command-runner.js | 3 ++- tests/pi-command-runner.test.js | 17 ++++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) 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/README.md b/README.md index af57bad..ba0f6d1 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,6 @@ npm ci pi install /absolute/path/to/confluence-cli ``` -Compiled Bun hosts run CLI subprocesses automatically without additional configuration. - For a Server/Data Center instance at `confluence.example.com`, start Pi from a shell configured like this when protected writes are intended: ```bash diff --git a/lib/pi/command-runner.js b/lib/pi/command-runner.js index 9c12632..cfda88e 100644 --- a/lib/pi/command-runner.js +++ b/lib/pi/command-runner.js @@ -161,6 +161,7 @@ function runCommand({ // 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; @@ -253,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 2878d30..d06dec3 100644 --- a/tests/pi-command-runner.test.js +++ b/tests/pi-command-runner.test.js @@ -51,8 +51,13 @@ const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status = (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() })); + 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, ` @@ -60,9 +65,9 @@ const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status = const { runCommand } = require(${JSON.stringify(require.resolve('../lib/pi/command-runner'))}); runCommand({ packageRoot: ${JSON.stringify(packageRoot)}, - projectRoot: ${JSON.stringify(packageRoot)}, + projectRoot: ${JSON.stringify(projectRoot)}, args: ['--json', 'info', '123; echo unsafe'], - env: { PATH: '' }, + env: { PATH: '', CONFLUENCE_DOMAIN: 'trusted.invalid' }, timeoutMs: 3000, maxOutputBytes: 4096, expectJson: true, @@ -80,15 +85,17 @@ const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status = const env = { ...process.env }; delete env.BUN_BE_BUN; const executable = runtime === 'compiled' ? hostBinary : 'bun'; - const args = runtime === 'compiled' ? [] : [hostSource]; + 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(packageRoot), + cwd: fs.realpathSync(projectRoot), + domain: 'trusted.invalid', }); } finally { cleanup(packageRoot); + cleanup(projectRoot); } }, 30000);