Skip to content

Validate JSON imports into ESM in --module nodenext #60019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 24, 2024

Conversation

andrewbranch
Copy link
Member

Fixes #59684

This does two main things:

  1. Enforces use of import attributes on JSON-file imports in --module nodenext, since Node.js crashes without them
  2. Errors on named imports of JSON-file imports in --module nodenext, since those don't work in Node.js. We already sort of considered this information when building up the type of a namespace import of a JSON file, but named imports bypassed that check.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 20, 2024
// In Node.js, JSON modules don't get named exports
if (ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
const usageMode = getEmitSyntaxForModuleSpecifierExpression(usage);
return usageMode === ModuleKind.ESNext && endsWith((usage as StringLiteralLike).text, Extension.Json);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text of the module specifier is not sufficient to know whether we're importing a JSON file; this produced both false positives and false negatives (see imports of "actually-json" and "not.json" in the test)

if (index >= 0) {
return fileName.substring(index);
return baseName.substring(index);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was extremely bad; I guess we were only really using this to determine whether something was a declaration file name, not to actually use the extension.

@jakebailey
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 20, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started 👀 Results
user test this ✅ Started 👀 Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

@typescript-bot
Copy link
Collaborator

Hey @jakebailey, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user tests with tsc comparing main and refs/pull/60019/merge:

Something interesting changed - please have a look.

Details

effect

tsconfig.json

tsconfig.base.json

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,153 62,153 ~ ~ ~ p=1.000 n=6
Types 50,242 50,242 ~ ~ ~ p=1.000 n=6
Memory used 193,106k (± 0.80%) 193,076k (± 0.73%) ~ 192,393k 195,940k p=0.471 n=6
Parse Time 1.31s (± 0.94%) 1.30s (± 0.63%) ~ 1.29s 1.31s p=0.730 n=6
Bind Time 0.71s 0.71s ~ ~ ~ p=1.000 n=6
Check Time 9.55s (± 0.42%) 9.59s (± 0.59%) ~ 9.52s 9.67s p=0.169 n=6
Emit Time 2.72s (± 0.38%) 2.70s (± 1.60%) ~ 2.61s 2.73s p=0.101 n=6
Total Time 14.29s (± 0.28%) 14.30s (± 0.45%) ~ 14.24s 14.39s p=1.000 n=6
angular-1 - node (v18.15.0, x64)
Errors 7 7 ~ ~ ~ p=1.000 n=6
Symbols 945,753 945,753 ~ ~ ~ p=1.000 n=6
Types 410,067 410,067 ~ ~ ~ p=1.000 n=6
Memory used 1,222,766k (± 0.00%) 1,222,762k (± 0.00%) ~ 1,222,714k 1,222,839k p=0.689 n=6
Parse Time 6.64s (± 0.78%) 6.64s (± 0.43%) ~ 6.60s 6.68s p=0.936 n=6
Bind Time 1.86s (± 0.34%) 1.86s (± 0.63%) ~ 1.84s 1.87s p=0.485 n=6
Check Time 31.19s (± 0.55%) 31.17s (± 0.50%) ~ 31.03s 31.39s p=0.575 n=6
Emit Time 15.08s (± 0.26%) 15.06s (± 0.46%) ~ 14.97s 15.12s p=0.810 n=6
Total Time 54.78s (± 0.23%) 54.73s (± 0.29%) ~ 54.54s 54.98s p=0.575 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,529,472 2,529,472 ~ ~ ~ p=1.000 n=6
Types 936,018 936,018 ~ ~ ~ p=1.000 n=6
Memory used 2,364,738k (± 0.00%) 2,364,744k (± 0.00%) ~ 2,364,702k 2,364,769k p=0.936 n=6
Parse Time 9.42s (± 0.22%) 9.41s (± 0.15%) ~ 9.38s 9.42s p=0.366 n=6
Bind Time 2.17s (± 0.48%) 2.18s (± 0.54%) ~ 2.17s 2.20s p=0.121 n=6
Check Time 72.96s (± 0.42%) 73.03s (± 0.70%) ~ 72.28s 73.74s p=0.688 n=6
Emit Time 0.28s (± 1.86%) 0.28s (± 1.86%) ~ 0.27s 0.28s p=1.000 n=6
Total Time 84.82s (± 0.37%) 84.89s (± 0.59%) ~ 84.15s 85.57s p=0.689 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,246,550 1,246,583 +33 (+ 0.00%) ~ ~ p=0.001 n=6
Types 264,444 264,461 +17 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 2,399,186k (± 0.02%) 2,399,512k (± 0.04%) ~ 2,398,425k 2,400,960k p=0.471 n=6
Parse Time 5.09s (± 0.35%) 5.10s (± 0.89%) ~ 5.04s 5.15s p=0.630 n=6
Bind Time 1.92s (± 0.33%) 1.90s (± 0.72%) -0.02s (- 0.87%) 1.88s 1.92s p=0.024 n=6
Check Time 34.79s (± 0.42%) 35.07s (± 0.85%) ~ 34.75s 35.43s p=0.093 n=6
Emit Time 3.04s (± 2.68%) 3.00s (± 4.26%) ~ 2.84s 3.23s p=0.295 n=6
Total Time 44.85s (± 0.40%) 45.07s (± 0.63%) ~ 44.64s 45.36s p=0.128 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,246,550 1,246,583 +33 (+ 0.00%) ~ ~ p=0.001 n=6
Types 264,444 264,461 +17 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 2,473,347k (± 0.02%) 2,473,929k (± 0.03%) ~ 2,473,116k 2,474,940k p=0.128 n=6
Parse Time 6.33s (± 0.53%) 6.29s (± 0.75%) ~ 6.21s 6.34s p=0.092 n=6
Bind Time 2.05s (± 1.10%) 2.04s (± 0.59%) ~ 2.03s 2.06s p=0.627 n=6
Check Time 41.41s (± 0.73%) 41.55s (± 0.85%) ~ 41.23s 42.01s p=0.689 n=6
Emit Time 3.63s (± 3.03%) 3.51s (± 1.66%) 🟩-0.12s (- 3.26%) 3.43s 3.60s p=0.045 n=6
Total Time 53.43s (± 0.59%) 53.40s (± 0.64%) ~ 53.00s 53.76s p=0.936 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 259,902 259,913 +11 (+ 0.00%) ~ ~ p=0.001 n=6
Types 106,141 106,156 +15 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 435,611k (± 0.02%) 435,638k (± 0.02%) ~ 435,510k 435,725k p=0.575 n=6
Parse Time 3.42s (± 0.82%) 3.43s (± 0.65%) ~ 3.41s 3.46s p=0.252 n=6
Bind Time 1.30s (± 1.02%) 1.31s (± 0.57%) ~ 1.30s 1.32s p=0.230 n=6
Check Time 18.20s (± 0.21%) 18.20s (± 0.20%) ~ 18.15s 18.24s p=1.000 n=6
Emit Time 1.53s (± 1.37%) 1.52s (± 1.44%) ~ 1.49s 1.55s p=0.464 n=6
Total Time 24.45s (± 0.27%) 24.47s (± 0.14%) ~ 24.42s 24.52s p=0.686 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 68 68 ~ ~ ~ p=1.000 n=6
Symbols 225,018 225,018 ~ ~ ~ p=1.000 n=6
Types 94,249 94,249 ~ ~ ~ p=1.000 n=6
Memory used 370,216k (± 0.01%) 370,344k (± 0.03%) +128k (+ 0.03%) 370,224k 370,536k p=0.030 n=6
Parse Time 2.76s (± 1.11%) 2.75s (± 0.61%) ~ 2.73s 2.77s p=0.569 n=6
Bind Time 1.55s (± 1.20%) 1.59s (± 1.48%) ~ 1.57s 1.62s p=0.056 n=6
Check Time 15.76s (± 0.35%) 15.75s (± 0.24%) ~ 15.69s 15.80s p=1.000 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.07s (± 0.43%) 20.08s (± 0.31%) ~ 20.03s 20.19s p=1.000 n=6
vscode - node (v18.15.0, x64)
Errors 1 1 ~ ~ ~ p=1.000 n=6
Symbols 3,093,507 3,093,507 ~ ~ ~ p=1.000 n=6
Types 1,066,180 1,066,180 ~ ~ ~ p=1.000 n=6
Memory used 3,191,308k (± 0.00%) 3,191,300k (± 0.00%) ~ 3,191,191k 3,191,443k p=0.810 n=6
Parse Time 13.93s (± 0.38%) 13.90s (± 0.21%) ~ 13.86s 13.94s p=0.376 n=6
Bind Time 4.45s (± 2.66%) 4.45s (± 2.68%) ~ 4.36s 4.61s p=1.000 n=6
Check Time 82.16s (± 0.51%) 82.20s (± 0.47%) ~ 81.66s 82.76s p=0.936 n=6
Emit Time 22.40s (± 0.30%) 22.38s (± 0.62%) ~ 22.24s 22.62s p=0.630 n=6
Total Time 122.94s (± 0.23%) 122.93s (± 0.37%) ~ 122.37s 123.47s p=0.936 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 277,196 277,196 ~ ~ ~ p=1.000 n=6
Types 112,950 112,950 ~ ~ ~ p=1.000 n=6
Memory used 427,080k (± 0.02%) 426,984k (± 0.02%) ~ 426,879k 427,123k p=0.092 n=6
Parse Time 4.89s (± 0.47%) 4.90s (± 0.35%) ~ 4.88s 4.93s p=0.369 n=6
Bind Time 2.14s (± 0.62%) 2.15s (± 0.49%) ~ 2.13s 2.16s p=0.451 n=6
Check Time 21.89s (± 0.34%) 21.81s (± 0.27%) ~ 21.70s 21.87s p=0.065 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 28.92s (± 0.27%) 28.85s (± 0.19%) ~ 28.76s 28.91s p=0.173 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 539,943 539,943 ~ ~ ~ p=1.000 n=6
Types 181,263 181,263 ~ ~ ~ p=1.000 n=6
Memory used 483,313k (± 0.01%) 483,352k (± 0.01%) ~ 483,304k 483,398k p=0.149 n=6
Parse Time 3.23s (± 0.48%) 3.22s (± 0.38%) ~ 3.20s 3.23s p=0.166 n=6
Bind Time 1.16s (± 1.09%) 1.16s (± 0.65%) ~ 1.15s 1.17s p=1.000 n=6
Check Time 18.34s (± 0.26%) 18.35s (± 0.24%) ~ 18.29s 18.41s p=1.000 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 22.74s (± 0.24%) 22.72s (± 0.18%) ~ 22.67s 22.79s p=0.810 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top 400 repos with tsc comparing main and refs/pull/60019/merge:

Something interesting changed - please have a look.

Details

ikatyang/emoji-cheat-sheet

tsconfig.json

  • error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
  • error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'Node16'.

refined-github/refined-github

tsconfig.json

@andrewbranch andrewbranch added the Breaking Change Would introduce errors in existing code label Sep 20, 2024
@andrewbranch andrewbranch merged commit 9ad213f into microsoft:main Sep 24, 2024
30 checks passed
@andrewbranch andrewbranch deleted the bug/59684 branch September 24, 2024 20:02
@sandersn sandersn removed this from PR Backlog Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Breaking Change Would introduce errors in existing code For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Module resolution: Named imports not supported with import attributes
4 participants