From 76b84a0b52b5a505c89a5c1b72a743e6c1903817 Mon Sep 17 00:00:00 2001 From: weishu Date: Fri, 26 Dec 2025 11:29:12 +0800 Subject: [PATCH] refactor: unify bun compiled detection with windows support - Add shared isBunCompiled() function for consistent runtime detection - Support Windows virtual filesystem paths (/~BUN/) alongside Linux/macOS (/$bunfs/) - Use Bun.main for reliable detection instead of process.argv[1] - Remove redundant $bunfs checks from cli/src/index.ts - Update cli/src/utils/bunRuntime.ts to use shared detection function - Create server/src/utils/bunCompiled.ts for server-side compilation check --- cli/src/index.ts | 3 --- cli/src/projectPath.ts | 7 +++---- cli/src/utils/bunRuntime.ts | 8 +++----- server/src/utils/bunCompiled.ts | 4 ++++ server/src/web/server.ts | 3 ++- 5 files changed, 12 insertions(+), 13 deletions(-) create mode 100644 server/src/utils/bunCompiled.ts diff --git a/cli/src/index.ts b/cli/src/index.ts index b08b1646..b113f620 100755 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -42,9 +42,6 @@ import { withBunRuntimeEnv } from './utils/bunRuntime' if (arg1 === process.execPath) { return process.argv.slice(2) } - if (arg1.includes('$bunfs')) { - return process.argv.slice(2) - } if (arg1.endsWith('.js') || arg1.endsWith('.mjs') || arg1.endsWith('.ts')) { return process.argv.slice(2) } diff --git a/cli/src/projectPath.ts b/cli/src/projectPath.ts index 14f4100d..a3a9eace 100644 --- a/cli/src/projectPath.ts +++ b/cli/src/projectPath.ts @@ -4,10 +4,9 @@ import { configuration } from '@/configuration'; import packageJson from '../package.json'; const __dirname = dirname(fileURLToPath(import.meta.url)); -const bunRuntime = (globalThis as typeof globalThis & { Bun?: { isCompiled?: boolean } }).Bun; -const argv1 = process.argv[1] ?? ''; -const bunFsMarker = argv1.includes('$bunfs'); -const isCompiled = Boolean(bunRuntime?.isCompiled) || bunFsMarker; + +/** Bun embeds compiled code in a virtual filesystem: /$bunfs/ (Linux/macOS) or /~BUN/ (Windows) */ +const isCompiled = Bun.main.includes('$bunfs') || Bun.main.includes('/~BUN/'); export function projectPath(): string { return resolve(__dirname, '..'); diff --git a/cli/src/utils/bunRuntime.ts b/cli/src/utils/bunRuntime.ts index eb86101d..6230b898 100644 --- a/cli/src/utils/bunRuntime.ts +++ b/cli/src/utils/bunRuntime.ts @@ -1,3 +1,5 @@ +import { isBunCompiled } from '@/projectPath'; + export type BunRuntimeEnvOptions = { allowBunBeBun?: boolean; }; @@ -16,11 +18,7 @@ export function withBunRuntimeEnv( env: NodeJS.ProcessEnv = process.env, options: BunRuntimeEnvOptions = {} ): NodeJS.ProcessEnv { - const bunRuntime = (globalThis as typeof globalThis & { Bun?: { isCompiled?: boolean } }).Bun; - const argv1 = process.argv[1] ?? ''; - const isCompiled = Boolean(bunRuntime?.isCompiled) || argv1.includes('$bunfs'); - - if (!isCompiled) { + if (!isBunCompiled()) { return env; } diff --git a/server/src/utils/bunCompiled.ts b/server/src/utils/bunCompiled.ts new file mode 100644 index 00000000..ee2ae4cf --- /dev/null +++ b/server/src/utils/bunCompiled.ts @@ -0,0 +1,4 @@ +/** Bun embeds compiled code in a virtual filesystem: /$bunfs/ (Linux/macOS) or /~BUN/ (Windows) */ +export function isBunCompiled(): boolean { + return Bun.main.includes('$bunfs') || Bun.main.includes('/~BUN/'); +} diff --git a/server/src/web/server.ts b/server/src/web/server.ts index b8343986..2b40f4ed 100644 --- a/server/src/web/server.ts +++ b/server/src/web/server.ts @@ -20,6 +20,7 @@ import type { Server as BunServer } from 'bun' import type { Server as SocketEngine } from '@socket.io/bun-engine' import type { WebSocketData } from '@socket.io/bun-engine' import { loadEmbeddedAssetMap, type EmbeddedWebAsset } from './embeddedAssets' +import { isBunCompiled } from '../utils/bunCompiled' function findWebappDistDir(): { distDir: string; indexHtmlPath: string } { const candidates = [ @@ -163,7 +164,7 @@ export async function startWebServer(options: { jwtSecret: Uint8Array socketEngine: SocketEngine }): Promise> { - const isCompiled = Bun.main?.startsWith('/$bunfs') ?? false + const isCompiled = isBunCompiled() const embeddedAssetMap = isCompiled ? await loadEmbeddedAssetMap() : null const app = createWebApp({ getSyncEngine: options.getSyncEngine,