diff --git a/packages/void/src/cli/cf-build-output.ts b/packages/void/src/cli/cf-build-output.ts index 9a88551ca..dd6bcb1c4 100644 --- a/packages/void/src/cli/cf-build-output.ts +++ b/packages/void/src/cli/cf-build-output.ts @@ -127,9 +127,24 @@ export async function prepareCfBuildOutput( if (!config.worker) { throw new Error('cf did not produce a Worker configuration.'); } - const main = raw.main; - if (typeof main !== 'string' || basename(main) !== main || !existsSync(join(source, main))) { - return null; + if (typeof raw.main !== 'string' || !raw.main) { + throw new Error('The built Worker configuration is missing its main entry. Rebuild the app.'); + } + const entry = raw.main.replaceAll('\\', '/'); + const main = relative(source, resolve(source, entry)); + if ( + isAbsolute(entry) || + /^[a-z]:/i.test(entry) || + !main || + main === '..' || + main.startsWith('../') + ) { + throw new Error('The built Worker main entry must be inside its output directory.'); + } + if (!existsSync(join(source, main))) { + throw new Error( + `The built Worker main entry ${JSON.stringify(main)} is missing. Rebuild the app.`, + ); } const assets = object(raw.assets); diff --git a/packages/void/test/unit/cf-build-output.test.ts b/packages/void/test/unit/cf-build-output.test.ts index e63757143..347b4bc97 100644 --- a/packages/void/test/unit/cf-build-output.test.ts +++ b/packages/void/test/unit/cf-build-output.test.ts @@ -254,6 +254,67 @@ it( CF_BUILD_TIMEOUT_MS, ); +it( + 'accepts a nested hashed Worker entry with a relative module import', + async () => { + const output = worker({ + name: 'void-test', + compatibility_date: '2026-09-29', + main: './assets/index-hash.js', + }); + mkdirSync(join(output, 'assets')); + writeFileSync( + join(output, 'assets/index-hash.js'), + "import { message } from './message.js'; export default { fetch() { return new Response(message) } };\n", + ); + writeFileSync(join(output, 'assets/message.js'), 'export const message = "nested entry";\n'); + rmSync(join(output, 'index.js')); + const prepared = await prepareCfBuildOutput(output, 'account-id'); + expect(prepared).not.toBeNull(); + if (!prepared) { + return; + } + try { + const built = await readBuildOutput(prepared.directory); + expect(built.workers.default.config.manifest).toMatchObject({ + mainModule: 'assets/index-hash.js', + modules: { 'assets/message.js': { type: 'esm' } }, + }); + const cf = join(dirname(fileURLToPath(import.meta.resolve('cf/package.json'))), 'bin', 'cf'); + const dryRun = spawnSync( + process.execPath, + [cf, 'workers', 'versions', 'create', '--prebuilt', '--dry-run'], + { cwd: prepared.directory, encoding: 'utf8' }, + ); + expect(dryRun.status, `${dryRun.stdout}\n${dryRun.stderr}`).toBe(0); + } finally { + prepared.dispose(); + } + }, + CF_BUILD_TIMEOUT_MS, +); + +it.each(['../index.js', '/index.js', 'C:\\index.js'])( + 'rejects an out-of-bundle entry %s', + async (main) => { + const output = worker({ name: 'void-test', compatibility_date: '2026-09-29', main }); + await expect(prepareCfBuildOutput(output, 'account-id')).rejects.toThrow( + 'inside its output directory', + ); + }, +); + +it('reports a missing built entry', async () => { + const output = worker({ + name: 'void-test', + compatibility_date: '2026-09-29', + main: 'assets/missing.js', + }); + await expect(prepareCfBuildOutput(output, 'account-id')).rejects.toThrow( + '"assets/missing.js" is missing', + ); +}); + it('preserves worker settings that the previous bridge dropped', async () => { const output = worker({ name: 'void-test',