Skip to content

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

Merged
Andrew Branch (andrewbranch) merged 3 commits into
microsoft:mainfrom
andrewbranch:bug/59684
Sep 24, 2024
Merged

Validate JSON imports into ESM in --module nodenext#60019
Andrew Branch (andrewbranch) merged 3 commits into
microsoft:mainfrom
andrewbranch:bug/59684

Conversation

@andrewbranch

Copy link
Copy Markdown
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.

// 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
Copy Markdown
MemberAuthor

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)

const index = baseName.lastIndexOf(".d.");
if (index >= 0) {
return fileName.substring(index);
return baseName.substring(index);

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test it

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 20, 2024

Copy link
Copy Markdown
Contributor

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

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

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@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
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Compiler-Unions - node (v18.15.0, x64)
Errors3030~~~p=1.000 n=6
Symbols62,15362,153~~~p=1.000 n=6
Types50,24250,242~~~p=1.000 n=6
Memory used193,106k (± 0.80%)193,076k (± 0.73%)~192,393k195,940kp=0.471 n=6
Parse Time1.31s (± 0.94%)1.30s (± 0.63%)~1.29s1.31sp=0.730 n=6
Bind Time0.71s0.71s~~~p=1.000 n=6
Check Time9.55s (± 0.42%)9.59s (± 0.59%)~9.52s9.67sp=0.169 n=6
Emit Time2.72s (± 0.38%)2.70s (± 1.60%)~2.61s2.73sp=0.101 n=6
Total Time14.29s (± 0.28%)14.30s (± 0.45%)~14.24s14.39sp=1.000 n=6
angular-1 - node (v18.15.0, x64)
Errors77~~~p=1.000 n=6
Symbols945,753945,753~~~p=1.000 n=6
Types410,067410,067~~~p=1.000 n=6
Memory used1,222,766k (± 0.00%)1,222,762k (± 0.00%)~1,222,714k1,222,839kp=0.689 n=6
Parse Time6.64s (± 0.78%)6.64s (± 0.43%)~6.60s6.68sp=0.936 n=6
Bind Time1.86s (± 0.34%)1.86s (± 0.63%)~1.84s1.87sp=0.485 n=6
Check Time31.19s (± 0.55%)31.17s (± 0.50%)~31.03s31.39sp=0.575 n=6
Emit Time15.08s (± 0.26%)15.06s (± 0.46%)~14.97s15.12sp=0.810 n=6
Total Time54.78s (± 0.23%)54.73s (± 0.29%)~54.54s54.98sp=0.575 n=6
mui-docs - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols2,529,4722,529,472~~~p=1.000 n=6
Types936,018936,018~~~p=1.000 n=6
Memory used2,364,738k (± 0.00%)2,364,744k (± 0.00%)~2,364,702k2,364,769kp=0.936 n=6
Parse Time9.42s (± 0.22%)9.41s (± 0.15%)~9.38s9.42sp=0.366 n=6
Bind Time2.17s (± 0.48%)2.18s (± 0.54%)~2.17s2.20sp=0.121 n=6
Check Time72.96s (± 0.42%)73.03s (± 0.70%)~72.28s73.74sp=0.688 n=6
Emit Time0.28s (± 1.86%)0.28s (± 1.86%)~0.27s0.28sp=1.000 n=6
Total Time84.82s (± 0.37%)84.89s (± 0.59%)~84.15s85.57sp=0.689 n=6
self-build-src - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols1,246,5501,246,583+33 (+ 0.00%)~~p=0.001 n=6
Types264,444264,461+17 (+ 0.01%)~~p=0.001 n=6
Memory used2,399,186k (± 0.02%)2,399,512k (± 0.04%)~2,398,425k2,400,960kp=0.471 n=6
Parse Time5.09s (± 0.35%)5.10s (± 0.89%)~5.04s5.15sp=0.630 n=6
Bind Time1.92s (± 0.33%)1.90s (± 0.72%)-0.02s (- 0.87%)1.88s1.92sp=0.024 n=6
Check Time34.79s (± 0.42%)35.07s (± 0.85%)~34.75s35.43sp=0.093 n=6
Emit Time3.04s (± 2.68%)3.00s (± 4.26%)~2.84s3.23sp=0.295 n=6
Total Time44.85s (± 0.40%)45.07s (± 0.63%)~44.64s45.36sp=0.128 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols1,246,5501,246,583+33 (+ 0.00%)~~p=0.001 n=6
Types264,444264,461+17 (+ 0.01%)~~p=0.001 n=6
Memory used2,473,347k (± 0.02%)2,473,929k (± 0.03%)~2,473,116k2,474,940kp=0.128 n=6
Parse Time6.33s (± 0.53%)6.29s (± 0.75%)~6.21s6.34sp=0.092 n=6
Bind Time2.05s (± 1.10%)2.04s (± 0.59%)~2.03s2.06sp=0.627 n=6
Check Time41.41s (± 0.73%)41.55s (± 0.85%)~41.23s42.01sp=0.689 n=6
Emit Time3.63s (± 3.03%)3.51s (± 1.66%)🟩-0.12s (- 3.26%)3.43s3.60sp=0.045 n=6
Total Time53.43s (± 0.59%)53.40s (± 0.64%)~53.00s53.76sp=0.936 n=6
self-compiler - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols259,902259,913+11 (+ 0.00%)~~p=0.001 n=6
Types106,141106,156+15 (+ 0.01%)~~p=0.001 n=6
Memory used435,611k (± 0.02%)435,638k (± 0.02%)~435,510k435,725kp=0.575 n=6
Parse Time3.42s (± 0.82%)3.43s (± 0.65%)~3.41s3.46sp=0.252 n=6
Bind Time1.30s (± 1.02%)1.31s (± 0.57%)~1.30s1.32sp=0.230 n=6
Check Time18.20s (± 0.21%)18.20s (± 0.20%)~18.15s18.24sp=1.000 n=6
Emit Time1.53s (± 1.37%)1.52s (± 1.44%)~1.49s1.55sp=0.464 n=6
Total Time24.45s (± 0.27%)24.47s (± 0.14%)~24.42s24.52sp=0.686 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors6868~~~p=1.000 n=6
Symbols225,018225,018~~~p=1.000 n=6
Types94,24994,249~~~p=1.000 n=6
Memory used370,216k (± 0.01%)370,344k (± 0.03%)+128k (+ 0.03%)370,224k370,536kp=0.030 n=6
Parse Time2.76s (± 1.11%)2.75s (± 0.61%)~2.73s2.77sp=0.569 n=6
Bind Time1.55s (± 1.20%)1.59s (± 1.48%)~1.57s1.62sp=0.056 n=6
Check Time15.76s (± 0.35%)15.75s (± 0.24%)~15.69s15.80sp=1.000 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time20.07s (± 0.43%)20.08s (± 0.31%)~20.03s20.19sp=1.000 n=6
vscode - node (v18.15.0, x64)
Errors11~~~p=1.000 n=6
Symbols3,093,5073,093,507~~~p=1.000 n=6
Types1,066,1801,066,180~~~p=1.000 n=6
Memory used3,191,308k (± 0.00%)3,191,300k (± 0.00%)~3,191,191k3,191,443kp=0.810 n=6
Parse Time13.93s (± 0.38%)13.90s (± 0.21%)~13.86s13.94sp=0.376 n=6
Bind Time4.45s (± 2.66%)4.45s (± 2.68%)~4.36s4.61sp=1.000 n=6
Check Time82.16s (± 0.51%)82.20s (± 0.47%)~81.66s82.76sp=0.936 n=6
Emit Time22.40s (± 0.30%)22.38s (± 0.62%)~22.24s22.62sp=0.630 n=6
Total Time122.94s (± 0.23%)122.93s (± 0.37%)~122.37s123.47sp=0.936 n=6
webpack - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols277,196277,196~~~p=1.000 n=6
Types112,950112,950~~~p=1.000 n=6
Memory used427,080k (± 0.02%)426,984k (± 0.02%)~426,879k427,123kp=0.092 n=6
Parse Time4.89s (± 0.47%)4.90s (± 0.35%)~4.88s4.93sp=0.369 n=6
Bind Time2.14s (± 0.62%)2.15s (± 0.49%)~2.13s2.16sp=0.451 n=6
Check Time21.89s (± 0.34%)21.81s (± 0.27%)~21.70s21.87sp=0.065 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time28.92s (± 0.27%)28.85s (± 0.19%)~28.76s28.91sp=0.173 n=6
xstate-main - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols539,943539,943~~~p=1.000 n=6
Types181,263181,263~~~p=1.000 n=6
Memory used483,313k (± 0.01%)483,352k (± 0.01%)~483,304k483,398kp=0.149 n=6
Parse Time3.23s (± 0.48%)3.22s (± 0.38%)~3.20s3.23sp=0.166 n=6
Bind Time1.16s (± 1.09%)1.16s (± 0.65%)~1.15s1.17sp=1.000 n=6
Check Time18.34s (± 0.26%)18.35s (± 0.24%)~18.29s18.41sp=1.000 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time22.74s (± 0.24%)22.72s (± 0.18%)~22.67s22.79sp=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)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@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

refined-github/refined-github

tsconfig.json

@andrewbranchAndrew Branch (andrewbranch) added the Breaking Change Would introduce errors in existing code label Sep 20, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamBreaking ChangeWould introduce errors in existing codeFor Milestone BugPRs 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

5 participants

@andrewbranch@jakebailey@typescript-bot@weswigham@sandersn