Drop TypeScript's __esModule marker from the bootstrap scripts #8833
<!-- durable-reviewer:finding -->
💬 SUGGESTION · Code Quality
The sed filter fails silently if TypeScript's emitted __esModule marker stops matching the exact pattern
This sed expression deletes only one exact line. If a TypeScript upgrade or a tsconfig change emits the marker differently (other spacing, or exports.__esModule = true), the genrule still succeeds and the marker comes back without warning. The only coverage is the MessagePort test, and other bundles built with this rule have none. You could add a check after the sed, e.g. && ! grep -q '__esModule' $@, so a pattern mismatch fails the build.
<!-- durable-reviewer:review run=49221446-2db2-40f6-b830-fcc1e33e2b6a -->
Superseded by the review of 5e0d661f.<details>
<summary>This review, of <code>6e220527</code></summary>
💬 Approved with comments — Correct, well-tested fix; one optional suggestion to make the sed strip fail loudly if TypeScript's emitted marker changes.
Findings: 1 suggestion (1 is an inline comment on the diff)
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
📋 Code Quality: 💬 1 suggestion
Sound change with good test coverage; one optional hardening suggestion for the sed filter.
Recommendation: 💬 Optional Nits
__esModule marker stops matching the exact pattern (build/wd_ts_script_bundle.bzl:213)<details>
<summary>View full review</summary>
The genrule strips the exact marker line TypeScript emits, and the new test covers both failure modes (descriptor fields on Object.prototype, and a replaced Object.defineProperty()). It restores global state in finally. $< is valid because each genrule has a single source, and the escaping works out to \. in the shell.
build/wd_ts_script_bundle.bzl:213: the sed expression matches one exact line. If a TypeScript upgrade or a tsconfig change alters the emitted text (spacing, or exports.__esModule = true), the genrule still succeeds and the marker comes back. The only thing that would catch it is the MessagePort test, and the other bundles that go through this rule have no such test. A post-sed check such as ! grep -q __esModule $@ would turn that into a build error.
</details>
---
<details>
<summary>✅ All other checks passed</summary>
📝 Documentation: ✅ LGTM
The new comment in wd_ts_script_bundle.bzl describes the current behaviour and explains why the marker is dropped.
</details>
---
<sub>Reviewed commit: 6e220527</sub>
</details>
<!-- durable-reviewer:review run=2e9e351c-ef92-4f68-9fd3-b6c1fa159de1 -->
Superseded by the review of 22f2a4cb.<details>
<summary>This review, of <code>5e0d661f</code></summary>
✅ Approved — The fix is correct and tested, and the build check added since the last review fixes the one earlier suggestion. No open findings remain.
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
<details>
<summary>✅ All checks passed</summary>
📋 Code Quality: ✅ LGTM
The earlier suggestion about the sed filter failing silently is fixed. build/wd_ts_script_bundle.bzl:214-216 now runs grep -n '__esModule' $@ after the strip and exits 1 on a match, so a change in TypeScript's emitted marker text stops the build. copy_file is still used elsewhere in the file, so its load is still needed. The new test in src/workerd/api/messagechannel-bootstrap-test.c++ covers both failure modes and restores the global state in finally.
---
📝 Documentation: ✅ LGTM
The comment in wd_ts_script_bundle.bzl describes the current behaviour and explains why the marker is dropped.
</details>
---
<sub>Reviewed commit: 5e0d661f</sub>
</details>
<!-- durable-reviewer:review run=88786df8-b758-496f-9330-101c14f82e1c -->
✅ Approved — The fix is correct and tested, and the build check added since the last review fixes the one earlier suggestion. No open findings remain.
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
<details>
<summary>✅ All checks passed</summary>
📋 Code Quality: ✅ LGTM
The earlier suggestion about the sed filter failing silently is fixed. build/wd_ts_script_bundle.bzl:214-216 now runs grep -n '__esModule' $@ after the strip and exits 1 on a match, so a change in TypeScript's emitted marker text stops the build. copy_file is still used elsewhere in the file, so its load is still needed. The new test in src/workerd/api/messagechannel-bootstrap-test.c++ covers both failure modes and restores the global state in finally.
---
📝 Documentation: ✅ LGTM
The comment in wd_ts_script_bundle.bzl describes the current behaviour and explains why the marker is dropped.
</details>
---
<sub>Reviewed commit: 22f2a4cb — same changes as 5e0d661f, so this is that commit's review (no new review was run; line numbers are as of 5e0d661f)</sub>
Approved these changes.
jasnell
nathanwhit
Fixes a bug where lazily loaded per-isolate bootstrap scripts can throw on first use if user code has polluted
Object.prototypeor replacedObject.defineProperty, by stripping TypeScript's emitted__esModulemarker inwd_ts_script_bundle.## Problem
Because
src/per_isolatecompiles with"module": "commonjs"(with"moduleDetection": "force"andimport type/export type), TypeScript prepends this line to every emitted script:For scripts loaded eagerly during isolate creation, this runs before user code. For scripts loaded lazily on first use, it runs after user code, where it can fail in two ways:
- It calls the global
Object.defineProperty(), which user code may have replaced (causing a throw or interceptingexports).- Its descriptor inherits from
Object.prototype, so defininggetorsetonObject.prototypemakes the descriptor invalid.MessageChannelandMessagePortalready load on first use (#8458)—through either the globals (expose_global_message_channel, default since 2025-08-15) orcloudflare-internal:messagechannel(node:worker_threads)—so this throws today:## Change
Replace the
copy_filestep inwd_ts_script_bundle(build/wd_ts_script_bundle.bzl) with agenrulethat drops theObject.defineProperty(exports, "__esModule", { value: true });line before V8 compile caches and the Cap'n Proto bundle aregenerated.
- Why in the build rule: TypeScript has no
tsconfigoption to suppress__esModuleemission for CommonJS modules. Stripping it inwd_ts_script_bundle's existing copy step covers bothsrc/per_isolateand embedder bundles, and ensures compilecaches are built from the stripped source.
- Why it is safe: The bootstrap
require()(src/workerd/api/per-isolate-bootstrap.c++) never reads__esModule, and the scripts themselves already useprimordialsand null-prototype descriptors.#8529 also depends on this for lazily loading
TextEncoderand carries this commit until this PR lands.## Compatibility
No change to the Workers API surface. First access to lazily loaded bootstrap globals (
MessageChannel,MessagePort) no longer throws when a Worker has replacedObject.definePropertyor addedget/settoObject.prototype.## Testing
Added a test case to
src/workerd/api/messagechannel-bootstrap-test.c++that defines all property descriptor fields (get,set,value,writable,enumerable,configurable) onObject.prototype, replacesObject.definePropertywith a throwingfunction, and verifies
MessagePortstill loads. Fails without this change.