diff --git a/src/lib/agent-sdk-assets.spec.ts b/src/lib/agent-sdk-assets.spec.ts index beb2c8be..e6004462 100644 --- a/src/lib/agent-sdk-assets.spec.ts +++ b/src/lib/agent-sdk-assets.spec.ts @@ -2,7 +2,7 @@ import { existsSync } from 'node:fs'; import { gzipSync } from 'node:zlib'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { AGENT_SDK_TARGET, AGENT_SDK_VERSION } from '../generated/agent-sdk-manifest.js'; -import { downloadTarball, ensureClaudeCodeExecutable, extractTarEntry } from './agent-sdk-assets.js'; +import { downloadTarball, ensureClaudeCodeExecutable, extractTarEntry, isBunVirtualFsUrl } from './agent-sdk-assets.js'; function tarHeader(name: string, size: number): Buffer { const header = Buffer.alloc(512); @@ -27,6 +27,25 @@ function makeTarball(entries: Array<[string, Buffer]>): Buffer { return gzipSync(Buffer.concat(parts)); } +describe('isBunVirtualFsUrl', () => { + it('detects the POSIX compiled-binary virtual filesystem', () => { + expect(isBunVirtualFsUrl('file:///$bunfs/root/workos')).toBe(true); + }); + + it('detects the Windows virtual filesystem with the tilde percent-encoded', () => { + expect(isBunVirtualFsUrl('file:///B:/%7EBUN/root/workos-windows-x64.exe')).toBe(true); + }); + + it('detects a raw Windows virtual path', () => { + expect(isBunVirtualFsUrl('B:\\~BUN\\root\\workos-windows-x64.exe')).toBe(true); + }); + + it('rejects source-mode module URLs', () => { + expect(isBunVirtualFsUrl(import.meta.url)).toBe(false); + expect(isBunVirtualFsUrl('file:///home/dev/cli/src/lib/agent-sdk-assets.ts')).toBe(false); + }); +}); + describe('ensureClaudeCodeExecutable', () => { it('resolves the current platform executable from node_modules in source mode', async () => { expect(AGENT_SDK_TARGET).toBe(`${process.platform}-${process.arch}`); diff --git a/src/lib/agent-sdk-assets.ts b/src/lib/agent-sdk-assets.ts index 465f0e27..29a5eccf 100644 --- a/src/lib/agent-sdk-assets.ts +++ b/src/lib/agent-sdk-assets.ts @@ -54,9 +54,19 @@ function runtimeTarget(): string { return `${process.platform}-${process.arch}${isMuslRuntime() ? '-musl' : ''}`; } +/** + * Whether a module URL points into a Bun compiled binary's virtual filesystem: + * `/$bunfs/` on POSIX, `B:\~BUN\` on Windows — which import.meta.url surfaces + * with the tilde percent-encoded (`file:///B:/%7EBUN/root/…`). Exported for + * tests. + */ +export function isBunVirtualFsUrl(url: string): boolean { + return url.includes('$bunfs') || url.includes('~BUN') || url.includes('%7EBUN'); +} + /** Running from a compiled binary: the module graph lives in Bun's virtual filesystem. */ function isCompiledBinary(): boolean { - return import.meta.url.includes('$bunfs') || import.meta.url.includes('~BUN'); + return isBunVirtualFsUrl(import.meta.url); } function agentSdkCacheRoot(): string {