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

Do not perserve references in declaration emit, unless preserve=true #57681

Merged
merged 22 commits into from
Mar 20, 2024

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Mar 7, 2024

At the moment, declaration emit does a funky mixture of preserving some references, eliding unused references, adding of referenced files for ambients (like node's builtins), but also sometimes imports (react), and so on. For external implementations of isolatedDeclarations (and honestly, the human brain), this is either difficult to reason about or totally impossible to replicate.

This PR greatly changes the way we handle references. References are no longer preserved in the output from the input, nor are they ever added based on any analysis. If a reference is intended to be preserved, one must write preserve="true" to have it copied to the output file. This gives a "what you see is what you get" format, not unlike what verbatimModuleSyntax does for imported values.

This is technically a break, as packages which re-export from something like @types/node will no longer have implicitly added reference directives for those types, however this change is in line with how we handle lib.d.ts; it's assumed that the user will have actually included the environment's types into their project. Testing in prep for this change (#57569, #57656) showed that most instances of added references fit into this bucket and should continue to work (and why we are shipping this without a new flag).

This change seems to improve performance a bit (1-4% in benchmarks with dts emit enabled), likely due to the reduced bookkeeping.

The only non-obvious thing that remains is the rewriting of file references, which includes the changing of import extensions (funny) and the shifting of paths to be relative to the output paths. This specific analysis is more reasonable to implement externally as it's just a function of file paths and compiler options.

Fixes #57439 (though does the opposite by default)

It also fixes these issues by nature of us no longer synthesizing new references, or just keeping the references as written:

Fixes #15488
Fixes #48143
Fixes #52110
Fixes #56590
Fixes #57482

Then in terms of design, the direction is now "we don't generate references out of thin air anymore", in which case this also closes:

Closes #28195

Related: #56571

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@jakebailey
Copy link
Member Author

A significant number of our tests break because they don't include the new references; is this an indicator that the default should actually be preserve=true? Or that our tests are "weird code heavy"?

@sheetalkamat
Copy link
Member

@jakebailey our tests are written such that just including d.ts files should not result in error. You would need to look at what all changes need to happen in the config for building those declaration file.

@jakebailey
Copy link
Member Author

The errors are declaration errors in the emit, since references that were needed were not preserved by default.

src/compiler/parser.ts Outdated Show resolved Hide resolved
@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 20, 2024

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

Command Status Results
perf test this faster ✅ Started 👀 Results

@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
Angular - node (v18.15.0, x64)
Memory used 295,741k (± 0.01%) 295,605k (± 0.01%) -136k (- 0.05%) 295,561k 295,628k p=0.005 n=6
Parse Time 2.66s (± 0.44%) 2.66s (± 0.48%) ~ 2.64s 2.67s p=1.000 n=6
Bind Time 0.83s (± 0.49%) 0.83s (± 0.49%) ~ 0.83s 0.84s p=1.000 n=6
Check Time 8.19s (± 0.26%) 8.23s (± 0.39%) ~ 8.18s 8.26s p=0.090 n=6
Emit Time 7.12s (± 0.55%) 7.04s (± 0.31%) -0.09s (- 1.22%) 7.01s 7.06s p=0.005 n=6
Total Time 18.81s (± 0.16%) 18.75s (± 0.18%) -0.06s (- 0.31%) 18.71s 18.80s p=0.025 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 193,205k (± 0.90%) 192,528k (± 0.75%) ~ 191,853k 195,479k p=0.230 n=6
Parse Time 1.36s (± 0.46%) 1.36s (± 1.23%) ~ 1.33s 1.38s p=0.720 n=6
Bind Time 0.72s (± 0.00%) 0.72s (± 0.00%) ~ 0.72s 0.72s p=1.000 n=6
Check Time 9.51s (± 0.81%) 9.51s (± 0.75%) ~ 9.42s 9.62s p=1.000 n=6
Emit Time 2.65s (± 0.44%) 2.63s (± 0.32%) -0.02s (- 0.88%) 2.61s 2.63s p=0.010 n=6
Total Time 14.24s (± 0.64%) 14.22s (± 0.57%) ~ 14.12s 14.34s p=0.688 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,384k (± 0.01%) 347,378k (± 0.01%) ~ 347,346k 347,416k p=0.689 n=6
Parse Time 2.47s (± 0.44%) 2.49s (± 0.55%) ~ 2.47s 2.51s p=0.054 n=6
Bind Time 0.93s (± 0.56%) 0.93s (± 0.00%) ~ 0.93s 0.93s p=0.174 n=6
Check Time 7.02s (± 0.37%) 7.04s (± 0.24%) ~ 7.01s 7.06s p=0.291 n=6
Emit Time 4.06s (± 0.40%) 4.07s (± 0.48%) ~ 4.04s 4.10s p=1.000 n=6
Total Time 14.49s (± 0.23%) 14.52s (± 0.24%) ~ 14.47s 14.57s p=0.169 n=6
TFS - node (v18.15.0, x64)
Memory used 302,753k (± 0.01%) 302,711k (± 0.00%) -42k (- 0.01%) 302,701k 302,725k p=0.005 n=6
Parse Time 2.00s (± 0.93%) 2.01s (± 0.54%) ~ 2.00s 2.03s p=0.411 n=6
Bind Time 1.01s (± 1.08%) 1.00s (± 0.41%) ~ 1.00s 1.01s p=0.090 n=6
Check Time 6.30s (± 0.39%) 6.32s (± 0.48%) ~ 6.29s 6.36s p=0.332 n=6
Emit Time 3.60s (± 0.55%) 3.62s (± 0.48%) ~ 3.60s 3.64s p=0.075 n=6
Total Time 12.92s (± 0.15%) 12.96s (± 0.25%) +0.04s (+ 0.31%) 12.90s 12.99s p=0.036 n=6
material-ui - node (v18.15.0, x64)
Memory used 509,926k (± 0.00%) 509,880k (± 0.00%) -46k (- 0.01%) 509,858k 509,902k p=0.005 n=6
Parse Time 2.66s (± 0.41%) 2.65s (± 0.78%) ~ 2.63s 2.68s p=0.801 n=6
Bind Time 0.99s (± 0.83%) 0.99s (± 0.76%) ~ 0.98s 1.00s p=0.729 n=6
Check Time 17.23s (± 0.16%) 17.23s (± 0.25%) ~ 17.16s 17.28s p=1.000 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.88s (± 0.13%) 20.87s (± 0.19%) ~ 20.81s 20.92s p=0.871 n=6
mui-docs - node (v18.15.0, x64)
Memory used 1,719,308k (± 0.00%) 1,719,280k (± 0.00%) ~ 1,719,241k 1,719,331k p=0.173 n=6
Parse Time 6.52s (± 0.38%) 6.54s (± 0.47%) ~ 6.49s 6.58s p=0.226 n=6
Bind Time 2.36s (± 0.35%) 2.36s (± 0.35%) ~ 2.35s 2.37s p=1.000 n=6
Check Time 56.42s (± 0.54%) 56.20s (± 0.33%) ~ 55.96s 56.40s p=0.128 n=6
Emit Time 0.13s (± 3.87%) 0.13s (± 3.10%) ~ 0.13s 0.14s p=0.595 n=6
Total Time 65.43s (± 0.49%) 65.23s (± 0.27%) ~ 65.00s 65.39s p=0.127 n=6
self-build-src - node (v18.15.0, x64)
Memory used 2,396,016k (± 0.04%) 2,391,983k (± 0.04%) -4,033k (- 0.17%) 2,391,032k 2,393,513k p=0.005 n=6
Parse Time 4.99s (± 1.17%) 5.00s (± 0.76%) ~ 4.95s 5.05s p=1.000 n=6
Bind Time 1.89s (± 1.19%) 1.90s (± 0.62%) ~ 1.88s 1.91s p=0.514 n=6
Check Time 33.51s (± 0.28%) 33.63s (± 0.44%) ~ 33.48s 33.80s p=0.092 n=6
Emit Time 2.73s (± 1.30%) 2.62s (± 0.88%) 🟩-0.11s (- 3.91%) 2.59s 2.66s p=0.005 n=6
Total Time 43.14s (± 0.36%) 43.16s (± 0.37%) ~ 42.99s 43.37s p=0.936 n=6
self-compiler - node (v18.15.0, x64)
Memory used 416,105k (± 0.01%) 415,087k (± 0.01%) -1,019k (- 0.24%) 415,037k 415,123k p=0.005 n=6
Parse Time 2.85s (± 1.01%) 2.81s (± 1.57%) ~ 2.76s 2.88s p=0.196 n=6
Bind Time 1.07s (± 0.48%) 1.07s (± 0.59%) ~ 1.06s 1.08s p=0.386 n=6
Check Time 15.30s (± 0.48%) 15.28s (± 0.21%) ~ 15.24s 15.33s p=0.808 n=6
Emit Time 1.15s (± 0.89%) 1.11s (± 1.23%) 🟩-0.04s (- 3.47%) 1.10s 1.13s p=0.005 n=6
Total Time 20.36s (± 0.32%) 20.28s (± 0.33%) ~ 20.19s 20.34s p=0.065 n=6
vscode - node (v18.15.0, x64)
Memory used 2,887,474k (± 0.00%) 2,887,473k (± 0.00%) ~ 2,887,365k 2,887,571k p=0.575 n=6
Parse Time 10.83s (± 0.40%) 10.84s (± 0.30%) ~ 10.78s 10.87s p=0.686 n=6
Bind Time 3.46s (± 0.38%) 3.46s (± 0.47%) ~ 3.43s 3.48s p=0.805 n=6
Check Time 61.51s (± 0.49%) 61.50s (± 0.44%) ~ 61.26s 61.99s p=0.873 n=6
Emit Time 16.98s (± 7.80%) 16.97s (± 8.48%) ~ 16.21s 19.90s p=0.630 n=6
Total Time 92.78s (± 1.70%) 92.75s (± 1.84%) ~ 91.72s 96.21s p=0.810 n=6
webpack - node (v18.15.0, x64)
Memory used 408,073k (± 0.02%) 408,060k (± 0.02%) ~ 407,987k 408,175k p=0.936 n=6
Parse Time 3.21s (± 0.65%) 3.24s (± 0.13%) +0.03s (+ 0.88%) 3.23s 3.24s p=0.006 n=6
Bind Time 1.38s (± 0.30%) 1.38s (± 0.37%) ~ 1.37s 1.38s p=0.114 n=6
Check Time 14.29s (± 0.31%) 14.31s (± 0.23%) ~ 14.28s 14.35s p=0.685 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 18.88s (± 0.31%) 18.92s (± 0.20%) ~ 18.88s 18.97s p=0.470 n=6
xstate - node (v18.15.0, x64)
Memory used 513,081k (± 0.01%) 513,034k (± 0.01%) ~ 512,907k 513,111k p=0.423 n=6
Parse Time 3.28s (± 0.17%) 3.28s (± 0.12%) ~ 3.28s 3.29s p=0.054 n=6
Bind Time 1.54s (± 0.26%) 1.54s (± 0.33%) ~ 1.54s 1.55s p=0.595 n=6
Check Time 2.85s (± 0.36%) 2.86s (± 0.82%) ~ 2.84s 2.90s p=0.220 n=6
Emit Time 0.07s (± 0.00%) 0.07s (±11.12%) ~ 0.07s 0.09s p=0.405 n=6
Total Time 7.74s (± 0.15%) 7.76s (± 0.23%) ~ 7.74s 7.79s p=0.089 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@jakebailey
Copy link
Member Author

Nice, now nearly 4% faster emit for the self benchmarks.

@jakebailey
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 20, 2024

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 Mar 20, 2024

Hey @jakebailey, 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/160635/artifacts?artifactName=tgz&fileId=F9E2BFE8B70C68AFEF79932BEEF547DC885DD9935ECCD0249B1D1A4B70EC193402&fileName=/typescript-5.5.0-insiders.20240320.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/pr-build@5.5.0-pr-57681-18".;

@jakebailey
Copy link
Member Author

Tested jupyterlab out; adding preserve=true fixes all problems. All of the other errors seem like oddities with how we attempt to build the project without actually invoking their build system.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Mar 20, 2024
@jakebailey jakebailey merged commit 7d50455 into microsoft:main Mar 20, 2024
25 checks passed
@jakebailey jakebailey deleted the new-references-behavior branch March 20, 2024 22:06
timocov added a commit to timocov/dts-bundle-generator that referenced this pull request Mar 24, 2024
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 Backlog Bug PRs that fix a backlog bug For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
4 participants