fix(build): polyfill import.meta.url to __filename in CJS worker bundle

The worker bundles ESM dependencies (notably @anthropic-ai/claude-agent-sdk's
*.mjs files) into CJS output. Those modules call createRequire(import.meta.url)
at module-load time. esbuild's CJS output left this as createRequire(ute.url)
— where `ute` is its `import.meta` polyfill `{}` — so `ute.url` was undefined
and module-load crashed with:

  TypeError: The argument 'filename' must be a file URL object, file URL
  string, or absolute path string. Received undefined
  code: ERR_INVALID_ARG_VALUE

Every Stop hook and every worker subprocess invocation hit this. Fix is the
esbuild `define` option mapping `import.meta.url` to `__filename` (provided as
a real absolute path by the existing CJS prelude in the banner).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex Newman
2026-05-09 02:23:38 -07:00
parent c79324ea17
commit e8376f4679

View File

@@ -145,7 +145,14 @@ async function buildHooks() {
'onnxruntime-node'
],
define: {
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`,
// Polyfill import.meta.url for ESM deps bundled into CJS output.
// @anthropic-ai/claude-agent-sdk's *.mjs files use createRequire(import.meta.url);
// without this define esbuild leaves it as `ute.url` (where ute is the {} import.meta polyfill),
// which evaluates to undefined at runtime and crashes module-load with
// "ERR_INVALID_ARG_VALUE: filename must be a file URL ... Received undefined".
// The CJS prelude (banner below) ensures __filename is a real absolute path at runtime.
'import.meta.url': '__filename'
},
banner: {
js: [