Skip to content

perf(core): check a CommonJS result's source before reading the file again - #830

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/827-double-read
Oct 13, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/827-double-read

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Summary

Fixes #827. Since #822, a file in a CommonJS package that uses ES module syntax was read from disk twice more before being transformed on the registerHooks() path: once by source_matches_file's byte compare and once by load_commonjs_esm's syntax check — yet the answer for such a file only depends on the source defaultLoadSync already handed the hook.

load_commonjs_esm and reject_cts_module_syntax now take an Option<&str> and only touch the disk when the caller has no source. transform_output checks the in-hand source's syntax first and runs source_matches_file only for a file that stays CommonJS:

  • A CJS-package file with ESM syntax costs zero extra reads, as in v0.1.6.
  • A file that stays CommonJS costs one extra read (the compare) instead of two.
  • .cts still compares bytes before the syntax check, so a hook-supplied .cts source keeps the error ordering it had before this reorder.
  • Non-UTF-8 sources still reach the "Not UTF-8" deferral untouched.
  • As the issue notes, a hook-supplied source that flips now goes through load_commonjs_esm's Module::Esm transform (matching the module.register() path) instead of Preserve; documented in the block comment, along with the parse-twice cost for the narrow differing-source-stays-CJS case.

Test plan

  • DYLD open() interpose (repro script) went from 3 opens per ESM-syntax .ts file to 1 on Node 26.11 — parity with the type: "module" control.
  • .cts with export still rejected with the same error; .cts CommonJS entry still defers; data:/custom-scheme sources still keep their source.
  • pnpm test, vp check, cargo clippy pass; fixtures verified on Node 24.21 (module.register()) and 26.11 (registerHooks()).

…again

Since #822, transform_output compared a commonjs result's source with the
file — a read — and then load_commonjs_esm read it once more to check its
syntax, so a CommonJS-package file with ES module syntax was read twice
more than v0.1.6 for no benefit: the flip only depends on the source in
hand. load_commonjs_esm and reject_cts_module_syntax now take an optional
source and read the file only when given none, and transform_output runs
the syntax check first and compares bytes only for a file that stays
CommonJS. .cts keeps the byte compare before the check so hook-supplied
source keeps its error ordering.

Fixes #827
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T04:39:16.221214Z 1b822be PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Brooooooklyn
Brooooooklyn merged commit a953b6b into main Oct 13, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/827-double-read branch October 13, 2026 06:10
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.

CommonJS-package files with ES module syntax are read twice more since #822

1 participant