FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

module: fix --check on ambiguous ESM files by bitpshr · Pull Request #65203 · nodejs/node · GitHub

/ node Public

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

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
bitpshr:cli/check-ambiguous-esm
Aug 12, 2026
Merged

module: fix --check on ambiguous ESM files#65203
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
bitpshr:cli/check-ambiguous-esm

Conversation

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 Bot commented Aug 10, 2026
edited
Loading

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.

This comment was marked as resolved.

Copy link
Copy Markdown

@daltino thanks

aduh95 added author ready PRs that have at least one approval, no outstanding review comments, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 11, 2026
github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Collaborator

aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 11, 2026
nodejs-github-bot merged commit 1b2de5e into nodejs:main Aug 12, 2026
89 of 90 checks passed

Copy link
Copy Markdown
Collaborator

Landed in 1b2de5e

nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 12, 2026
aduh95 pushed a commit that referenced this pull request Aug 13, 2026
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: #65202
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
PR-URL: #65203
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
aduh95 pushed a commit that referenced this pull request Aug 25, 2026
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: #65202
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
PR-URL: #65203
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 outstanding review comments, and a CI started. 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


Back | FazBrowse Home | New Git URL