Merge pull request #3332 from hehuaiyu/fix/windows-bun-shim-resolution

fix(tooling): resolve Bun executable on Windows
This commit is contained in:
Bohdan Triapitsyn
2026-09-05 01:24:34 +03:00
committed by GitHub
11 changed files with 337 additions and 18 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
"scripts": {
"dev": "bun run build:watch",
"dev:server": "OPENCHAMBER_RELAY_HOST=${OPENCHAMBER_RELAY_HOST:-off} bun server/index.js --port ${OPENCHAMBER_PORT:-3001}",
"dev:server:watch": "OPENCHAMBER_RELAY_HOST=${OPENCHAMBER_RELAY_HOST:-off} nodemon --watch server --ext js --exec \"bun server/index.js --port ${OPENCHAMBER_PORT:-3001}\"",
"dev:server:watch": "node ./scripts/dev-server-watch.mjs",
"build": "vite build",
"build:watch": "vite build --watch",
"type-check": "tsc --noEmit",
+76
View File
@@ -0,0 +1,76 @@
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { resolveBunExecutable } from '../../../scripts/lib/bun-executable.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const webRoot = path.resolve(__dirname, '..');
export function createDevServerWatchCommand(options = {}) {
const platform = options.platform ?? process.platform;
const env = options.env ?? process.env;
const bunExecutable = options.bunExecutable ?? resolveBunExecutable({ env, platform });
const configuredPort = env.OPENCHAMBER_PORT?.trim();
const port = configuredPort || '3001';
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535) {
throw new Error(`Invalid OPENCHAMBER_PORT: ${port}`);
}
return {
command: bunExecutable,
args: platform === 'win32'
? ['--watch', 'server/index.js', '--port', port]
: [
'x',
'nodemon',
'--watch',
'server',
'--ext',
'js',
'--exec',
`bun server/index.js --port ${port}`,
],
spawnOptions: {
cwd: webRoot,
stdio: 'inherit',
env: {
...env,
OPENCHAMBER_RELAY_HOST: env.OPENCHAMBER_RELAY_HOST || 'off',
},
windowsHide: true,
},
};
}
export function runDevServerWatch(options = {}) {
const runner = options.runner ?? spawn;
const { command, args, spawnOptions } = createDevServerWatchCommand(options);
const child = runner(command, args, spawnOptions);
child.on('error', (error) => {
console.error('[dev:web:server] Failed to start server watcher:', error);
process.exitCode = 1;
});
child.on('exit', (code, signal) => {
if (signal) {
process.kill(process.pid, signal);
return;
}
process.exitCode = code ?? 1;
});
return child;
}
const isMain = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
if (isMain) {
try {
runDevServerWatch();
} catch (error) {
console.error('[dev:web:server] Unable to configure server watcher:', error);
process.exitCode = 1;
}
}
@@ -0,0 +1,60 @@
import { describe, expect, it } from 'vitest';
import { createDevServerWatchCommand } from './dev-server-watch.mjs';
describe('createDevServerWatchCommand', () => {
it('uses the package default port and disables relay hosting by default', () => {
const command = createDevServerWatchCommand({
platform: 'win32',
env: {},
bunExecutable: '/opt/bun/bin/bun',
});
expect(command.command).toBe('/opt/bun/bin/bun');
expect(command.args).toEqual(['--watch', 'server/index.js', '--port', '3001']);
expect(command.spawnOptions.env.OPENCHAMBER_RELAY_HOST).toBe('off');
expect(command.spawnOptions.windowsHide).toBe(true);
});
it('passes the configured port and relay host to the Bun watcher', () => {
const command = createDevServerWatchCommand({
platform: 'win32',
env: {
OPENCHAMBER_PORT: '58992',
OPENCHAMBER_RELAY_HOST: 'relay.example.test',
},
bunExecutable: 'C:\\Tools\\Bun\\bun.exe',
});
expect(command.args).toEqual(['--watch', 'server/index.js', '--port', '58992']);
expect(command.spawnOptions.env.OPENCHAMBER_RELAY_HOST).toBe('relay.example.test');
});
it('rejects an invalid configured port before spawning', () => {
expect(() => createDevServerWatchCommand({
platform: 'win32',
env: { OPENCHAMBER_PORT: 'not-a-port' },
bunExecutable: 'bun',
})).toThrow(/Invalid OPENCHAMBER_PORT/);
});
it('preserves the nodemon watcher command outside Windows', () => {
const command = createDevServerWatchCommand({
platform: 'linux',
env: { OPENCHAMBER_PORT: '4200' },
bunExecutable: '/opt/bun/bin/bun',
});
expect(command.command).toBe('/opt/bun/bin/bun');
expect(command.args).toEqual([
'x',
'nodemon',
'--watch',
'server',
'--ext',
'js',
'--exec',
'bun server/index.js --port 4200',
]);
});
});
@@ -138,7 +138,7 @@ export const createServerStartupRuntime = (dependencies) => {
// Cover every signal a shell or dev harness may use to stop/restart us, so
// the managed OpenCode child is always torn down gracefully instead of
// orphaned: SIGINT/SIGQUIT (Ctrl+C/Ctrl+\), SIGTERM (kill/default), SIGHUP
// (terminal close), SIGUSR2 (nodemon restart for `dev:server:watch`).
// (terminal close), SIGUSR2 (nodemon restart outside Windows).
process.on('SIGTERM', handleSignal);
process.on('SIGINT', handleSignal);
process.on('SIGQUIT', handleSignal);