Skip to content
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

Fix ESM/CJS resolution cache collision in non-nodenext resolution modes #60910

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

andrewbranch
Copy link
Member

Fixes #60243 for me, and more directly an incorrect resolution result shown in the conformance test. The crash at #60243 occurs when a single file contains a duplicated resolution stored under two different mode-aware cache keys. In the test case, the result for import type pkg from "pkg" with { "resolution-mode": "require" } is stored in the node_modules directory cache under 1|pkg. That result is returned for the "resolution-mode": "import" resolution because the mode is recomputed incorrectly in loadModuleFromNearestNodeModulesDirectoryWorker. This returns a wrong result, but it also
results in that resolution occurring twice in the per-file resolution, once under 1|pkg and once under 99|pkg, so when we iterate over resolutions to stop watching them, we try to process the same resolution twice, causing the crash.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jan 3, 2025
@@ -2989,7 +2989,7 @@ function loadModuleFromNearestNodeModulesDirectoryTypesScope(moduleName: string,
}

function loadModuleFromNearestNodeModulesDirectoryWorker(extensions: Extensions, moduleName: string, directory: string, state: ModuleResolutionState, typesScopeOnly: boolean, cache: ModuleResolutionCache | undefined, redirectedReference: ResolvedProjectReference | undefined): SearchResult<Resolved> {
const mode = state.features === 0 ? undefined : state.features & NodeResolutionFeatures.EsmMode ? ModuleKind.ESNext : ModuleKind.CommonJS;
const mode = state.features === 0 ? undefined : (state.features & NodeResolutionFeatures.EsmMode || state.conditions.includes("import")) ? ModuleKind.ESNext : ModuleKind.CommonJS;
Copy link
Member Author

Choose a reason for hiding this comment

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

We should possibly just plumb resolutionMode through from the top, but this should work too. EsmMode is never set outside of node16nodenext; it’s only used to disable directory lookups and extension searching. conditions is where we see the actual difference in different import modes in --moduleResolution bundler.

@andrewbranch
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 3, 2025

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

Command Status Results
pack this ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 3, 2025

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/164461/artifacts?artifactName=tgz&fileId=12E0C2B8F3DB612B425BC38C307E1FA25B7C2209351D95BF7506225ED87E9FB302&fileName=/typescript-5.8.0-insiders.20250103.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@andrewbranch
Copy link
Member Author

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 8, 2025

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

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

@typescript-bot
Copy link
Collaborator

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

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Timeout"
  • 2 instances of "Package install failed"

Otherwise...

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@andrewbranch
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 34 34 ~ ~ ~ p=1.000 n=6
Symbols 62,363 62,363 ~ ~ ~ p=1.000 n=6
Types 50,395 50,395 ~ ~ ~ p=1.000 n=6
Memory used 194,272k (± 0.95%) 193,685k (± 0.75%) ~ 193,019k 196,625k p=0.298 n=6
Parse Time 1.31s (± 0.57%) 1.31s (± 0.48%) ~ 1.30s 1.32s p=0.718 n=6
Bind Time 0.73s 0.73s ~ ~ ~ p=1.000 n=6
Check Time 9.78s (± 0.59%) 9.79s (± 0.26%) ~ 9.75s 9.82s p=0.871 n=6
Emit Time 2.71s (± 2.04%) 2.73s (± 0.85%) ~ 2.71s 2.77s p=0.935 n=6
Total Time 14.53s (± 0.46%) 14.56s (± 0.30%) ~ 14.50s 14.63s p=0.376 n=6
angular-1 - node (v18.15.0, x64)
Errors 37 37 ~ ~ ~ p=1.000 n=6
Symbols 947,936 947,936 ~ ~ ~ p=1.000 n=6
Types 410,955 410,955 ~ ~ ~ p=1.000 n=6
Memory used 1,225,809k (± 0.00%) 1,225,829k (± 0.00%) ~ 1,225,747k 1,225,904k p=0.575 n=6
Parse Time 6.67s (± 0.76%) 6.64s (± 0.61%) ~ 6.60s 6.70s p=0.468 n=6
Bind Time 1.89s (± 0.55%) 1.89s (± 0.40%) ~ 1.88s 1.90s p=0.437 n=6
Check Time 31.96s (± 0.51%) 31.93s (± 0.23%) ~ 31.80s 32.01s p=0.378 n=6
Emit Time 15.19s (± 0.25%) 15.19s (± 0.61%) ~ 15.09s 15.33s p=0.630 n=6
Total Time 55.70s (± 0.28%) 55.65s (± 0.32%) ~ 55.43s 55.92s p=0.471 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,446,507 2,446,507 ~ ~ ~ p=1.000 n=6
Types 897,064 897,064 ~ ~ ~ p=1.000 n=6
Memory used 2,311,188k (± 0.00%) 2,311,197k (± 0.00%) ~ 2,311,124k 2,311,241k p=0.575 n=6
Parse Time 8.98s (± 0.32%) 8.95s (± 0.37%) ~ 8.91s 8.99s p=0.288 n=6
Bind Time 2.13s (± 0.46%) 2.13s (± 0.66%) ~ 2.11s 2.15s p=0.932 n=6
Check Time 73.36s (± 0.54%) 73.27s (± 0.52%) ~ 72.80s 73.71s p=0.810 n=6
Emit Time 0.29s (± 1.92%) 0.28s (± 2.26%) ~ 0.27s 0.29s p=0.201 n=6
Total Time 84.76s (± 0.48%) 84.63s (± 0.45%) ~ 84.15s 85.04s p=0.810 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,226,852 1,226,938 +86 (+ 0.01%) ~ ~ p=0.001 n=6
Types 266,743 266,762 +19 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 3,088,967k (± 0.02%) 3,088,057k (± 0.03%) ~ 3,087,030k 3,089,347k p=0.066 n=6
Parse Time 8.43s (± 0.89%) 8.49s (± 0.48%) ~ 8.44s 8.54s p=0.128 n=6
Bind Time 2.65s (± 0.67%) 2.63s (± 0.76%) ~ 2.61s 2.66s p=0.127 n=6
Check Time 53.08s (± 0.24%) 53.03s (± 0.16%) ~ 52.88s 53.12s p=0.298 n=6
Emit Time 4.24s (± 1.73%) 4.32s (± 2.69%) ~ 4.15s 4.48s p=0.230 n=6
Total Time 68.39s (± 0.28%) 68.46s (± 0.24%) ~ 68.27s 68.66s p=0.689 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,226,852 1,226,938 +86 (+ 0.01%) ~ ~ p=0.001 n=6
Types 266,743 266,762 +19 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 2,425,319k (± 0.03%) 2,424,944k (± 0.02%) ~ 2,424,396k 2,425,983k p=0.298 n=6
Parse Time 5.47s (± 0.51%) 5.47s (± 1.10%) ~ 5.41s 5.57s p=0.748 n=6
Bind Time 1.82s (± 0.54%) 1.82s (± 0.41%) ~ 1.81s 1.83s p=0.554 n=6
Check Time 35.37s (± 0.41%) 35.34s (± 0.49%) ~ 35.21s 35.66s p=0.575 n=6
Emit Time 3.05s (± 2.49%) 3.09s (± 4.35%) ~ 3.01s 3.36s p=0.688 n=6
Total Time 45.72s (± 0.36%) 45.72s (± 0.64%) ~ 45.46s 46.17s p=0.689 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 262,441 262,441 ~ ~ ~ p=1.000 n=6
Types 106,628 106,628 ~ ~ ~ p=1.000 n=6
Memory used 440,191k (± 0.01%) 440,172k (± 0.01%) ~ 440,147k 440,199k p=0.378 n=6
Parse Time 3.55s (± 0.52%) 3.55s (± 1.59%) ~ 3.48s 3.61s p=1.000 n=6
Bind Time 1.32s (± 1.24%) 1.33s (± 0.78%) ~ 1.31s 1.34s p=1.000 n=6
Check Time 18.93s (± 0.19%) 18.95s (± 0.31%) ~ 18.86s 19.00s p=0.572 n=6
Emit Time 1.53s (± 0.76%) 1.54s (± 0.67%) ~ 1.52s 1.55s p=0.456 n=6
Total Time 25.33s (± 0.18%) 25.35s (± 0.42%) ~ 25.22s 25.48s p=0.810 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 70 70 ~ ~ ~ p=1.000 n=6
Symbols 226,062 226,062 ~ ~ ~ p=1.000 n=6
Types 94,488 94,488 ~ ~ ~ p=1.000 n=6
Memory used 371,613k (± 0.01%) 371,813k (± 0.05%) ~ 371,572k 371,993k p=0.128 n=6
Parse Time 2.89s (± 1.27%) 2.88s (± 1.29%) ~ 2.82s 2.92s p=1.000 n=6
Bind Time 1.59s (± 0.92%) 1.58s (± 1.59%) ~ 1.53s 1.60s p=0.452 n=6
Check Time 16.48s (± 0.35%) 16.49s (± 0.56%) ~ 16.31s 16.58s p=0.423 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.96s (± 0.09%) 20.95s (± 0.54%) ~ 20.73s 21.03s p=0.375 n=6
vscode - node (v18.15.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 3,233,157 3,233,157 ~ ~ ~ p=1.000 n=6
Types 1,112,963 1,112,963 ~ ~ ~ p=1.000 n=6
Memory used 3,296,719k (± 0.01%) 3,296,739k (± 0.01%) ~ 3,296,397k 3,297,250k p=1.000 n=6
Parse Time 17.47s (± 0.41%) 17.43s (± 0.31%) ~ 17.36s 17.50s p=0.520 n=6
Bind Time 5.63s (± 2.86%) 5.71s (± 2.89%) ~ 5.53s 5.91s p=0.336 n=6
Check Time 106.52s (± 0.44%) 108.50s (± 3.13%) ~ 105.76s 114.33s p=0.471 n=6
Emit Time 34.84s (± 2.56%) 33.81s (± 8.23%) ~ 28.30s 35.60s p=0.575 n=6
Total Time 164.46s (± 0.66%) 165.45s (± 1.71%) ~ 162.59s 170.92s p=0.630 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 291,562 291,562 ~ ~ ~ p=1.000 n=6
Types 118,971 118,971 ~ ~ ~ p=1.000 n=6
Memory used 445,365k (± 0.03%) 445,428k (± 0.03%) ~ 445,205k 445,590k p=0.471 n=6
Parse Time 4.10s (± 0.84%) 4.11s (± 0.91%) ~ 4.04s 4.14s p=0.329 n=6
Bind Time 1.78s (± 1.91%) 1.78s (± 1.20%) ~ 1.74s 1.80s p=0.871 n=6
Check Time 18.81s (± 0.39%) 18.82s (± 0.75%) ~ 18.64s 19.00s p=0.810 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.69s (± 0.46%) 24.72s (± 0.67%) ~ 24.45s 24.87s p=0.688 n=6
xstate-main - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 555,017 555,017 ~ ~ ~ p=1.000 n=6
Types 186,115 186,115 ~ ~ ~ p=1.000 n=6
Memory used 494,037k (± 0.01%) 494,051k (± 0.02%) ~ 493,934k 494,261k p=0.810 n=6
Parse Time 3.41s (± 0.16%) 3.39s (± 0.88%) ~ 3.35s 3.43s p=0.250 n=6
Bind Time 1.17s (± 0.35%) 1.16s (± 0.89%) ~ 1.15s 1.18s p=0.257 n=6
Check Time 19.56s (± 0.30%) 19.53s (± 0.20%) ~ 19.49s 19.59s p=0.470 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.14s (± 0.24%) 24.09s (± 0.17%) ~ 24.05s 24.16s p=0.107 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

TS Server fatal error: Debug Failure.
2 participants