Summary
There is currently no way to get source-mapped stack traces for Workers loaded via the Worker Loader binding (dynamic workers). Errors thrown inside a dynamically loaded, bundled Worker surface with positions in the bundled module, and none of the existing source-map machinery applies:
Error.stack inside the isolate is never remapped at runtime (expected — this matches deployed Workers).
- The deployed-Worker remapping pipeline (
upload_source_maps = true) doesn't apply, because loader-provided code never goes through the upload path.
WorkerLoaderModule has no field to attach a source map: { js, cjs, text, data, json, py, wasm }.
- Exceptions delivered to
tails / streamingTails attached via WorkerLoaderWorkerCode also arrive with raw (bundled) positions.
Use case
We run user-submitted code in dynamic workers, bundled with @cloudflare/worker-bundler (bundle: true is needed for npm dependency support). The bundler's sourcemap: true option inlines an esbuild source map into the emitted module, but nothing consumes it: when the user's code throws, the stack we report back to them points at bundle line numbers instead of their own source, which makes errors materially harder for end users to act on.
This seems like it will be a common need for the Worker Loader's headline use case (running dynamically generated / user-provided code, often bundled from TypeScript or multiple files).
Proposal
Some options, roughly in order of preference:
- Accept a source map alongside a module, e.g. a
sourceMap field on WorkerLoaderModule (or honor inline //# sourceMappingURL=data:... comments), and use it to remap stack traces in exceptions delivered to the loader's tails / streamingTails and to Workers Logs — mirroring the out-of-band remapping that deployed Workers get with upload_source_maps.
- Expose the remapping as an API the host Worker can call (give the loader/stub a way to remap a stack string for a worker it loaded).
Workaround today
Host-side remapping: emit an inline source map (sourcemap: true in worker-bundler), have the dynamic worker return e.stack to the host, and remap frames in the host Worker with @jridgewell/trace-mapping against the map extracted from the bundled module. This works but every Worker Loader consumer has to rebuild it.
Summary
There is currently no way to get source-mapped stack traces for Workers loaded via the Worker Loader binding (dynamic workers). Errors thrown inside a dynamically loaded, bundled Worker surface with positions in the bundled module, and none of the existing source-map machinery applies:
Error.stackinside the isolate is never remapped at runtime (expected — this matches deployed Workers).upload_source_maps = true) doesn't apply, because loader-provided code never goes through the upload path.WorkerLoaderModulehas no field to attach a source map:{ js, cjs, text, data, json, py, wasm }.tails/streamingTailsattached viaWorkerLoaderWorkerCodealso arrive with raw (bundled) positions.Use case
We run user-submitted code in dynamic workers, bundled with
@cloudflare/worker-bundler(bundle: trueis needed for npm dependency support). The bundler'ssourcemap: trueoption inlines an esbuild source map into the emitted module, but nothing consumes it: when the user's code throws, the stack we report back to them points at bundle line numbers instead of their own source, which makes errors materially harder for end users to act on.This seems like it will be a common need for the Worker Loader's headline use case (running dynamically generated / user-provided code, often bundled from TypeScript or multiple files).
Proposal
Some options, roughly in order of preference:
sourceMapfield onWorkerLoaderModule(or honor inline//# sourceMappingURL=data:...comments), and use it to remap stack traces in exceptions delivered to the loader'stails/streamingTailsand to Workers Logs — mirroring the out-of-band remapping that deployed Workers get withupload_source_maps.Workaround today
Host-side remapping: emit an inline source map (
sourcemap: truein worker-bundler), have the dynamic worker returne.stackto the host, and remap frames in the host Worker with@jridgewell/trace-mappingagainst the map extracted from the bundled module. This works but every Worker Loader consumer has to rebuild it.