Skip to content

test: refuse to hide runtime regions that swallowed user code in snapshots - #10429

Closed
hyfdev wants to merge 1 commit into
mainfrom
snapshot-runtime-hide-guard
Closed

hyfdev wants to merge 1 commit into
mainfrom
snapshot-runtime-hide-guard

Conversation

@hyfdev

@hyfdev hyfdev commented Jul 24, 2026

Copy link
Copy Markdown
Member

Problem

tweak_snapshot hides the rolldown-runtime region of every artifact snapshot by lazily matching //#region \0rolldown/runtime.js … //#endregion. That match trusts the region markers to be balanced — and chunk output does not guarantee that.

Region markers are ordinary comments, and the default minify: 'dce-only' pass re-parses the joined chunk and removes unused statements together with their attached comments. When the removed statement sits at a module boundary (e.g. the hoisted var foo; of a wrapped TLA module whose only write is dead), the adjacent //#endregion + //#region next.js pair vanishes with it. The surviving markers still pair up textually, so the lazy hide silently extends across the following module and the snapshot swallows real user code while claiming it hid only the runtime.

Observed on strict_execution_order/top_level_await_syntax while validating #10414: after the runtime folds into the same chunk, the snapshot's // HIDDEN [\0rolldown/runtime.js] also ate the init_foo wrapper. The emitted chunk is correct — only the snapshot view lied. Verified end-to-end: the pre-DCE output (minify: false) carries fully balanced markers; the imbalance is introduced exclusively by the dce-only pass dropping comments attached to removed statements.

Fix (test harness only)

The three hide regexes are untouched. Before the rolldown-runtime match is replaced with // HIDDEN, a guard now checks that the span really is runtime-only, and otherwise leaves it visible verbatim — a noisy-but-truthful snapshot instead of one that silently hides user code. The span is refused when:

  • it contains another //#region line (the runtime region lost its own closer and the lazy match ran into the next module), or
  • a column-zero declaration binds a name that is neither __-prefixed (all runtime helpers) nor one of the known dev-runtime glue names (Module, DevRuntime, … — a literal list with a pointer to the composing sources). This is the only signal that survives the eaten-adjacent-pair case, where the remaining markers still pair up and marker structure looks perfectly well-formed.

Indented (iife/umd) regions have no column-zero declarations and keep their existing hiding behavior; the marker corruption this guards against arises from esm layouts.

The guard immediately caught five pre-existing swallows

Re-running the full fixture suite updates five snapshots where the old hide was already swallowing user code on main — each one the eaten-boundary shape described above, where the "runtime" span also contained user statements (var init_a = __esmMin(...) wrappers, var main_default = __toESM(...), const value = …):

  • esbuild/default/external_es6_converted_to_common_js
  • function/module_types/asset
  • resolve/attach_correct_package_json
  • sourcemap/issue_6099
  • topics/runtime/transform

Those snapshots now show what the chunks actually contain. The full-suite failure list is byte-identical to unmodified main on the same machine (the pre-existing local css/legal-comments environment set); four unit tests cover the hide, the dev-runtime hide, the swallowed-span refusal, and the crossed-region refusal.

The underlying output-level issue — dce-only corrupting attachDebugInfo region markers by dropping comments attached to removed statements — is a separate product bug (fix belongs in oxc's comment preservation); this PR only stops the test harness from being misled by it.

…shots

The snapshot harness hides the rolldown-runtime region by lazily matching
//#region ... //#endregion. Region markers are ordinary comments, and the
default dce-only pass drops comments attached to removed statements: a removed
statement at a module boundary takes the adjacent endregion/region pair with
it, the surviving markers still pair up textually, and the hide silently
extends across the next module's code.

Guard the replacement instead of trusting the markers: a span is hidden only
when it contains no other region opener and every column-zero declaration
binds a __-prefixed helper or a known dev-runtime glue name; anything else
stays visible verbatim. The guard immediately reveals five snapshots on main
whose hidden "runtime" spans already contained user code.
@netlify

netlify Bot commented Jul 24, 2026

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit e134a38
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a62e5d4f968320008dba131

@hyfdev hyfdev closed this Jul 24, 2026
graphite-app Bot pushed a commit that referenced this pull request Jul 24, 2026
…e region (#10431)

Artifact snapshots can silently present user code as part of the hidden runtime region — six fixtures on main do this today. This PR keeps fixture output's region structure intact through the default dce pass, so the hide applies exactly to the runtime and everything else stays visible. `topics/runtime/transform` before and after:

```js
// before: the fixture's own code is invisible
// HIDDEN [\0rolldown/runtime.js]
export { value };
```

```js
// after: the chunk contained this all along
// HIDDEN [\0rolldown/runtime.js]
//#region dep.cjs
//#endregion
//#region entry.js
const value = (/* @__PURE__ */ __commonJSMin(((exports) => {
	exports.value = "cjs-value";
})))().value;
//#endregion
export { value };
```

### Problem

The snapshot harness hides the runtime by matching `//#region \0rolldown/runtime.js … //#endregion`, trusting the markers to delimit it. But markers are ordinary comments, and under the default `minify: 'dce-only'` a statement removed at a module boundary takes the adjacent `//#endregion` + `//#region` pair with it — the surviving markers still pair up textually, so the hide silently extends across the next module's code.

The comment loss is oxc behavior: a comment rides the statement that follows it, and the minifier drops a removed statement's comments (the pre-dce output is fully balanced, verified with `minify: false`). Filed upstream as oxc-project/oxc#24885.

Blast radius: the snapshot review surface. Production builds and the public API are untouched by this PR — the change lives entirely in the test harness. Real builds keep the corrupted markers until the upstream fix.

### Fix

Keep the markers alive across the dce pass, from inside the harness only: a plugin registered for every fixture build rewrites marker lines to legal-comment form (`//!#region`) in `renderChunk`, which runs before the internal minify pass — legal comments survive the removal of their host statement, that is their license-text contract — and rewrites them back in `generateBundle`. The rewrite is line-anchored (only a line starting with a marker token matches) and gated to dce-only builds; full minification strips all normal comments on purpose and is left alone.

Two alternatives were rejected: preserving the markers inside the minify pipeline itself would put workaround logic in production code paths, and hardening the snapshot hide heuristically (#10429, superseded) cannot recover a boundary the text no longer contains.

Non-goal: fixing emitted output for real builds — that is oxc-project/oxc#24885; this plugin gets deleted when it lands.

### Sourcemap impact

No mapping is invalidated, by construction: the rewrite is line-preserving (one `!` per marker line, line count unchanged), marker lines are comments carrying no mapping segments, and every other position is untouched. `renderChunk` returns `map: Null` — the explicit "no remapping needed" form, covered by an existing test asserting it raises no `SOURCEMAP_BROKEN`. Known precision edge: the emitted `.map` is finalized against the disguised text and the restore runs after, so marker lines' columns differ from the map by one character — those lines have no segments, so no lookup can observe it.

### Validation

- Unit test pins the rewrite as line-anchored and reversible (`//# sourceMappingURL` and string contents never match).
- Full fixture suite: the failure list is byte-identical to unmodified main on the same machine (the pre-existing local environment set). Every snapshot change was classified: restored marker pairs (the bulk), the runtime hide turning precise again (previously-leaked helper definitions hidden), the six swallow fixtures revealing their user code (`esbuild/default/external_es6_converted_to_common_js`, `function/module_types/asset`, `resolve/attach_correct_package_json`, `sourcemap/issue_6099`, `topics/runtime/transform`, `issues/4782`), and line/hash shifts that follow mechanically (visualizer sections shift by the restored lines; content hashes in eight hash-bearing fixtures change deterministically, since hashing runs while the disguise is in place).

Supersedes #10429. Long-term fix tracked in oxc-project/oxc#24885.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant