Skip to content

module: fix --check on ambiguous ESM files - #65203

Open
bitpshr wants to merge 1 commit into
nodejs:mainfrom
bitpshr:cli/check-ambiguous-esm
Open

module: fix --check on ambiguous ESM files#65203
bitpshr wants to merge 1 commit into
nodejs:mainfrom
bitpshr:cli/check-ambiguous-esm

Conversation

@bitpshr

@bitpshr bitpshr commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #65202. node --check exits 0 on a .js file that contains module syntax and a genuine syntax error, when the file's format is ambiguous (no "type" in the nearest package.json).

Such a file has no format of its own, and defaultGetFormat() reports it as null. checkSyntax() passed that null straight to wrapSafe(), which parses as CommonJS; the module syntax makes that parse bail out before the rest of the file is parsed, so nothing was reported.

At load time the goal for an ambiguous file is decided by looking for module syntax in the source, so this decides it the same way before choosing how to parse.

$ node --check broken.js ; echo "exit: $?"    # import + `var = ;`
exit: 0                                        # before
...
SyntaxError: Unexpected token '='              # after
exit: 1

Verified against a local build. Files whose format is known are unaffected, and ambiguous files without module syntax still parse as CommonJS, so sloppy-mode sources keep working:

case before after
.js ambiguous + module syntax + error exit 0 exit 1
.js ambiguous, sloppy-mode with exit 0 exit 0
.js ambiguous, plain syntax error exit 1 exit 1
.mjs, or "type" set exit 1 exit 1
.js ambiguous, valid ESM exit 0 exit 0

The added fixture fails the existing test-cli-syntax-bad assertions before this change and passes after. All test-cli-syntax* suites pass locally.

Fixes: #65202

A `.js` file with no `"type"` in the nearest package.json has no format
of its own, and `defaultGetFormat()` reports it as null. `--check`
passed that null straight to `wrapSafe()`, which parses as CommonJS.
Module syntax makes that parse bail out early, so the file was reported
as valid and `--check` exited 0 even though it is not valid JavaScript
under either goal.

At load time the goal for such a file is decided by looking for module
syntax in the source. Decide it the same way here, so the file is
parsed as a module and its real syntax error is reported. Files whose
format is known are unaffected, as are ambiguous files without module
syntax, which are still parsed as CommonJS.

Fixes: nodejs#65202
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.32%. Comparing base (4a5eb1c) to head (3b17952).
⚠️ Report is 259 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65203      +/-   ##
==========================================
+ Coverage   90.15%   90.32%   +0.16%     
==========================================
  Files         744      760      +16     
  Lines      242517   248538    +6021     
  Branches    45688    46893    +1205     
==========================================
+ Hits       218642   224482    +5840     
- Misses      15358    15460     +102     
- Partials     8517     8596      +79     
Files with missing lines Coverage Δ
lib/internal/main/check_syntax.js 100.00% <100.00%> (ø)

... and 188 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

daltino

This comment was marked as resolved.

@MerlijnW70

Copy link
Copy Markdown

@daltino thanks

@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 11, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 11, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@aduh95 aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--check silently passes a .js file with a syntax error when the file contains ESM syntax

6 participants