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

Support resolving @typescript/[lib] in node modules #45771

Merged
merged 8 commits into from
Sep 15, 2021
Merged

Conversation

orta
Copy link
Contributor

@orta orta commented Sep 7, 2021

Note that #46046 updated this implementation with adding @typescript/lib-[x].


Closes #45518 as an alternative which leaves the responsibilities of configuring the environment to your package manager. Re #45688 #45685 and #44795

This answer to supporting your own versions of in-built libraries uses the node_modules resolution structure to let you define libraries. The path lookup looks like:

  • lib.dom.d.ts -> @typescript/dom
  • lib.dom.iterable.d.ts -> @typescript/dom/iterable
  • lib.es2015.symbol.wellknown.d.ts -> @typescript/es2015/symbol-wellknown

If you made a package which just sets up @typescript/es2015/symbol-wellknown.d.ts without @typescript/es2015/index.d.ts, then es2015 would still resolve to the TypeScript hosted version.

All JS dependency managers support this syntax:

{
  "devDependencies": {
    "@typescript/lib-dom": "npm:@types/web"
  }
}

This gives people the chance to make their own package with the .d.ts files in the right place and then set it up safely in their own project similar to @types. As we own @typescript/x on npm (and only have a few modules like @typescript/vfs, none of which clash) - then we don't need to worry about 'what happens if someone accidentally includes it twice' via the dependency tree.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 7, 2021
@orta
Copy link
Contributor Author

orta commented Sep 14, 2021

@typescript-bot perf test
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 14, 2021

Heya @orta, I've started to run the perf test suite on this PR at daa99ce. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 14, 2021

Heya @orta, I've started to run the tarball bundle task on this PR at daa99ce. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 14, 2021

Hey @orta, 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/110462/artifacts?artifactName=tgz&fileId=2DE81E5CC943F129DD37FCDD4A05363F2460D8444D5A74006585CA7254DA912A02&fileName=/typescript-4.5.0-insiders.20210914.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@4.5.0-pr-45771-3".;

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..45771

Metric main 45771 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 351,531k (± 0.02%) 351,585k (± 0.02%) +54k (+ 0.02%) 351,465k 351,753k
Parse Time 1.90s (± 0.30%) 1.92s (± 0.56%) +0.02s (+ 1.00%) 1.90s 1.94s
Bind Time 0.85s (± 0.80%) 0.85s (± 1.09%) -0.00s (- 0.23%) 0.84s 0.88s
Check Time 5.43s (± 0.42%) 5.43s (± 0.37%) +0.00s (+ 0.00%) 5.39s 5.48s
Emit Time 5.81s (± 0.50%) 5.85s (± 0.51%) +0.03s (+ 0.53%) 5.79s 5.94s
Total Time 14.00s (± 0.27%) 14.04s (± 0.34%) +0.05s (+ 0.35%) 13.94s 14.19s
Compiler-Unions - node (v10.16.3, x64)
Memory used 203,538k (± 0.04%) 203,558k (± 0.02%) +19k (+ 0.01%) 203,416k 203,639k
Parse Time 0.78s (± 0.87%) 0.79s (± 0.94%) +0.01s (+ 0.64%) 0.77s 0.80s
Bind Time 0.52s (± 1.11%) 0.53s (± 1.44%) +0.00s (+ 0.57%) 0.51s 0.54s
Check Time 7.88s (± 0.61%) 7.88s (± 0.60%) -0.01s (- 0.08%) 7.76s 8.01s
Emit Time 2.45s (± 0.69%) 2.48s (± 1.15%) +0.03s (+ 1.27%) 2.43s 2.56s
Total Time 11.64s (± 0.53%) 11.67s (± 0.52%) +0.04s (+ 0.30%) 11.49s 11.83s
Monaco - node (v10.16.3, x64)
Memory used 340,597k (± 0.02%) 340,642k (± 0.03%) +44k (+ 0.01%) 340,364k 340,803k
Parse Time 1.44s (± 0.99%) 1.46s (± 0.61%) +0.02s (+ 1.04%) 1.44s 1.48s
Bind Time 0.75s (± 1.04%) 0.75s (± 0.82%) +0.00s (+ 0.00%) 0.74s 0.76s
Check Time 5.45s (± 0.48%) 5.46s (± 0.35%) +0.01s (+ 0.20%) 5.42s 5.50s
Emit Time 3.17s (± 0.87%) 3.16s (± 0.83%) -0.00s (- 0.09%) 3.11s 3.23s
Total Time 10.80s (± 0.44%) 10.82s (± 0.30%) +0.02s (+ 0.18%) 10.74s 10.88s
TFS - node (v10.16.3, x64)
Memory used 304,064k (± 0.02%) 304,081k (± 0.02%) +17k (+ 0.01%) 303,892k 304,208k
Parse Time 1.18s (± 0.34%) 1.19s (± 0.68%) +0.01s (+ 0.76%) 1.17s 1.20s
Bind Time 0.71s (± 1.05%) 0.72s (± 0.69%) +0.00s (+ 0.42%) 0.70s 0.72s
Check Time 4.95s (± 0.66%) 4.95s (± 0.57%) +0.00s (+ 0.06%) 4.88s 5.00s
Emit Time 3.31s (± 0.95%) 3.37s (± 1.73%) +0.06s (+ 1.84%) 3.28s 3.51s
Total Time 10.15s (± 0.51%) 10.22s (± 0.82%) +0.08s (+ 0.75%) 10.08s 10.42s
material-ui - node (v10.16.3, x64)
Memory used 468,283k (± 0.02%) 468,332k (± 0.01%) +49k (+ 0.01%) 468,231k 468,407k
Parse Time 1.73s (± 0.22%) 1.75s (± 0.47%) +0.02s (+ 1.28%) 1.73s 1.76s
Bind Time 0.67s (± 1.12%) 0.67s (± 0.97%) +0.00s (+ 0.15%) 0.65s 0.68s
Check Time 14.29s (± 0.32%) 14.34s (± 0.72%) +0.05s (+ 0.38%) 14.08s 14.59s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.68s (± 0.25%) 16.76s (± 0.58%) +0.07s (+ 0.43%) 16.52s 16.98s
Angular - node (v12.1.0, x64)
Memory used 329,505k (± 0.02%) 329,556k (± 0.03%) +51k (+ 0.02%) 329,424k 329,852k
Parse Time 1.87s (± 0.61%) 1.90s (± 0.34%) +0.03s (+ 1.60%) 1.89s 1.92s
Bind Time 0.84s (± 0.59%) 0.84s (± 0.79%) +0.00s (+ 0.48%) 0.83s 0.86s
Check Time 5.29s (± 0.55%) 5.28s (± 0.50%) -0.01s (- 0.17%) 5.22s 5.33s
Emit Time 6.10s (± 0.70%) 6.11s (± 0.98%) +0.01s (+ 0.20%) 6.02s 6.32s
Total Time 14.10s (± 0.40%) 14.13s (± 0.60%) +0.03s (+ 0.21%) 13.99s 14.40s
Compiler-Unions - node (v12.1.0, x64)
Memory used 191,055k (± 0.12%) 191,080k (± 0.07%) +25k (+ 0.01%) 190,554k 191,257k
Parse Time 0.78s (± 0.51%) 0.78s (± 0.97%) +0.01s (+ 0.77%) 0.77s 0.80s
Bind Time 0.53s (± 1.09%) 0.53s (± 0.89%) +0.00s (+ 0.57%) 0.52s 0.54s
Check Time 7.43s (± 0.56%) 7.44s (± 0.58%) +0.01s (+ 0.07%) 7.36s 7.56s
Emit Time 2.46s (± 0.51%) 2.47s (± 0.74%) +0.01s (+ 0.49%) 2.43s 2.51s
Total Time 11.20s (± 0.44%) 11.23s (± 0.48%) +0.03s (+ 0.23%) 11.10s 11.38s
Monaco - node (v12.1.0, x64)
Memory used 323,687k (± 0.08%) 323,766k (± 0.05%) +79k (+ 0.02%) 323,136k 323,949k
Parse Time 1.44s (± 0.78%) 1.44s (± 1.02%) +0.00s (+ 0.28%) 1.41s 1.47s
Bind Time 0.73s (± 1.18%) 0.73s (± 0.84%) -0.00s (- 0.14%) 0.72s 0.75s
Check Time 5.33s (± 0.60%) 5.32s (± 0.41%) -0.00s (- 0.04%) 5.29s 5.37s
Emit Time 3.22s (± 0.97%) 3.22s (± 0.53%) -0.00s (- 0.12%) 3.18s 3.25s
Total Time 10.72s (± 0.58%) 10.71s (± 0.30%) -0.00s (- 0.02%) 10.65s 10.81s
TFS - node (v12.1.0, x64)
Memory used 288,824k (± 0.03%) 288,819k (± 0.02%) -5k (- 0.00%) 288,661k 288,941k
Parse Time 1.21s (± 0.51%) 1.22s (± 0.77%) +0.01s (+ 0.50%) 1.20s 1.24s
Bind Time 0.70s (± 0.42%) 0.70s (± 1.71%) +0.00s (+ 0.43%) 0.68s 0.73s
Check Time 4.90s (± 0.56%) 4.90s (± 0.42%) -0.00s (- 0.06%) 4.86s 4.95s
Emit Time 3.39s (± 1.11%) 3.40s (± 0.55%) +0.01s (+ 0.27%) 3.36s 3.45s
Total Time 10.21s (± 0.52%) 10.23s (± 0.37%) +0.02s (+ 0.18%) 10.14s 10.31s
material-ui - node (v12.1.0, x64)
Memory used 447,022k (± 0.05%) 447,014k (± 0.07%) -8k (- 0.00%) 445,824k 447,300k
Parse Time 1.73s (± 0.67%) 1.75s (± 0.50%) +0.02s (+ 1.22%) 1.73s 1.76s
Bind Time 0.65s (± 0.89%) 0.65s (± 0.77%) -0.01s (- 1.23%) 0.63s 0.65s
Check Time 12.96s (± 1.00%) 13.06s (± 0.71%) +0.10s (+ 0.74%) 12.81s 13.23s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.34s (± 0.84%) 15.45s (± 0.65%) +0.11s (+ 0.71%) 15.19s 15.64s
Angular - node (v14.15.1, x64)
Memory used 328,057k (± 0.01%) 328,079k (± 0.01%) +21k (+ 0.01%) 328,034k 328,141k
Parse Time 1.90s (± 0.63%) 1.93s (± 0.60%) +0.02s (+ 1.26%) 1.91s 1.96s
Bind Time 0.88s (± 0.65%) 0.88s (± 0.66%) +0.00s (+ 0.34%) 0.87s 0.90s
Check Time 5.31s (± 0.55%) 5.34s (± 0.30%) +0.03s (+ 0.56%) 5.31s 5.39s
Emit Time 6.22s (± 0.46%) 6.21s (± 0.70%) -0.02s (- 0.29%) 6.13s 6.34s
Total Time 14.32s (± 0.27%) 14.36s (± 0.31%) +0.04s (+ 0.29%) 14.30s 14.51s
Compiler-Unions - node (v14.15.1, x64)
Memory used 191,905k (± 0.56%) 191,597k (± 0.60%) -308k (- 0.16%) 189,629k 192,945k
Parse Time 0.80s (± 0.62%) 0.81s (± 0.64%) +0.01s (+ 0.75%) 0.80s 0.83s
Bind Time 0.56s (± 1.00%) 0.56s (± 0.40%) +0.00s (+ 0.18%) 0.55s 0.56s
Check Time 7.55s (± 0.66%) 7.56s (± 0.54%) +0.01s (+ 0.11%) 7.49s 7.64s
Emit Time 2.44s (± 1.03%) 2.45s (± 0.67%) +0.01s (+ 0.45%) 2.42s 2.49s
Total Time 11.36s (± 0.56%) 11.39s (± 0.43%) +0.03s (+ 0.24%) 11.28s 11.51s
Monaco - node (v14.15.1, x64)
Memory used 322,563k (± 0.00%) 322,566k (± 0.00%) +3k (+ 0.00%) 322,543k 322,589k
Parse Time 1.49s (± 0.68%) 1.49s (± 0.86%) +0.01s (+ 0.40%) 1.47s 1.53s
Bind Time 0.75s (± 0.40%) 0.75s (± 0.69%) -0.00s (- 0.13%) 0.74s 0.76s
Check Time 5.26s (± 0.53%) 5.27s (± 0.55%) +0.01s (+ 0.13%) 5.21s 5.34s
Emit Time 3.24s (± 0.73%) 3.23s (± 0.77%) -0.00s (- 0.09%) 3.19s 3.29s
Total Time 10.73s (± 0.38%) 10.74s (± 0.40%) +0.01s (+ 0.13%) 10.65s 10.83s
TFS - node (v14.15.1, x64)
Memory used 287,738k (± 0.00%) 287,753k (± 0.01%) +15k (+ 0.01%) 287,720k 287,787k
Parse Time 1.23s (± 1.87%) 1.23s (± 1.79%) +0.00s (+ 0.08%) 1.20s 1.29s
Bind Time 0.74s (± 3.03%) 0.76s (± 4.64%) +0.02s (+ 3.13%) 0.72s 0.84s
Check Time 4.91s (± 0.38%) 4.90s (± 0.44%) -0.01s (- 0.31%) 4.85s 4.95s
Emit Time 3.48s (± 0.49%) 3.47s (± 0.58%) -0.02s (- 0.46%) 3.43s 3.52s
Total Time 10.36s (± 0.24%) 10.36s (± 0.55%) -0.00s (- 0.01%) 10.23s 10.47s
material-ui - node (v14.15.1, x64)
Memory used 445,503k (± 0.00%) 445,507k (± 0.00%) +4k (+ 0.00%) 445,487k 445,542k
Parse Time 1.77s (± 0.57%) 1.78s (± 0.37%) +0.01s (+ 0.85%) 1.76s 1.79s
Bind Time 0.69s (± 0.64%) 0.70s (± 0.68%) +0.01s (+ 0.72%) 0.69s 0.71s
Check Time 13.05s (± 0.56%) 13.08s (± 0.46%) +0.03s (+ 0.24%) 12.96s 13.19s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.51s (± 0.52%) 15.56s (± 0.39%) +0.05s (+ 0.32%) 15.43s 15.67s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory9 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v14.15.1, x64)
Benchmark Name Iterations
Current 45771 10
Baseline main 10

Developer Information:

Download Benchmark

@orta
Copy link
Contributor Author

orta commented Sep 14, 2021

Actually trying this IRL is giving me unexpected results,

Code:
var localOverride = ts.resolveModuleName("@typescript/" + path, currentDirectory, { moduleResolution: ts.ModuleResolutionKind.NodeJs }, host);
console.log({ libFileName, localOverride, currentDirectory  })

Result:
{
  libFileName: 'lib.dom.d.ts',
  localOverride: {
    resolvedModule: undefined,
    failedLookupLocations: [
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.ts',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.tsx',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.d.ts',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.ts',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.tsx',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.d.ts',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@types/typescript__dom/package.json',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@types/typescript__dom.d.ts',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@types/typescript__dom/index.d.ts',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.ts',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.tsx',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.d.ts',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.ts',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.tsx',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.d.ts',
      '/Users/ortatherox/dev/typescript/node_modules/@types/typescript__dom/package.json',
      '/Users/ortatherox/dev/typescript/node_modules/@types/typescript__dom.d.ts',
      '/Users/ortatherox/dev/typescript/node_modules/@types/typescript__dom/index.d.ts',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/dev/node_modules/@typescript/dom.ts',
      '/Users/ortatherox/dev/node_modules/@typescript/dom.tsx',
      '/Users/ortatherox/dev/node_modules/@typescript/dom.d.ts',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/index.ts',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/index.tsx',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/index.d.ts',
      '/Users/ortatherox/dev/node_modules/@types/typescript__dom/package.json',
      '/Users/ortatherox/dev/node_modules/@types/typescript__dom.d.ts',
      '/Users/ortatherox/dev/node_modules/@types/typescript__dom/index.d.ts',
      '/Users/ortatherox/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/node_modules/@typescript/dom.ts',
      '/Users/ortatherox/node_modules/@typescript/dom.tsx',
      '/Users/ortatherox/node_modules/@typescript/dom.d.ts',
      '/Users/ortatherox/node_modules/@typescript/dom/index.ts',
      '/Users/ortatherox/node_modules/@typescript/dom/index.tsx',
      '/Users/ortatherox/node_modules/@typescript/dom/index.d.ts',
      '/Users/ortatherox/node_modules/@types/typescript__dom/package.json',
      '/Users/ortatherox/node_modules/@types/typescript__dom.d.ts',
      '/Users/ortatherox/node_modules/@types/typescript__dom/index.d.ts',
      '/Users/node_modules/@typescript/dom/package.json',
      '/Users/node_modules/@typescript/dom.ts',
      '/Users/node_modules/@typescript/dom.tsx',
      '/Users/node_modules/@typescript/dom.d.ts',
      '/Users/node_modules/@typescript/dom/index.ts',
      '/Users/node_modules/@typescript/dom/index.tsx',
      '/Users/node_modules/@typescript/dom/index.d.ts',
      '/Users/node_modules/@types/typescript__dom/package.json',
      '/Users/node_modules/@types/typescript__dom.d.ts',
      '/Users/node_modules/@types/typescript__dom/index.d.ts',
      '/node_modules/@typescript/dom/package.json',
      '/node_modules/@typescript/dom.ts',
      '/node_modules/@typescript/dom.tsx',
      '/node_modules/@typescript/dom.d.ts',
      '/node_modules/@typescript/dom/index.ts',
      '/node_modules/@typescript/dom/index.tsx',
      '/node_modules/@typescript/dom/index.d.ts',
      '/node_modules/@types/typescript__dom/package.json',
      '/node_modules/@types/typescript__dom.d.ts',
      '/node_modules/@types/typescript__dom/index.d.ts',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.js',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.jsx',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.js',
      '/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.jsx',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.js',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.jsx',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.js',
      '/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.jsx',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/dev/node_modules/@typescript/dom.js',
      '/Users/ortatherox/dev/node_modules/@typescript/dom.jsx',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/index.js',
      '/Users/ortatherox/dev/node_modules/@typescript/dom/index.jsx',
      '/Users/ortatherox/node_modules/@typescript/dom/package.json',
      '/Users/ortatherox/node_modules/@typescript/dom.js',
      '/Users/ortatherox/node_modules/@typescript/dom.jsx',
      '/Users/ortatherox/node_modules/@typescript/dom/index.js',
      '/Users/ortatherox/node_modules/@typescript/dom/index.jsx',
      '/Users/node_modules/@typescript/dom/package.json',
      '/Users/node_modules/@typescript/dom.js',
      '/Users/node_modules/@typescript/dom.jsx',
      '/Users/node_modules/@typescript/dom/index.js',
      '/Users/node_modules/@typescript/dom/index.jsx',
      '/node_modules/@typescript/dom/package.json',
      '/node_modules/@typescript/dom.js',
      '/node_modules/@typescript/dom.jsx',
      '/node_modules/@typescript/dom/index.js',
      '/node_modules/@typescript/dom/index.jsx'
    ]
  },
  currentDirectory: '/Users/ortatherox/dev/typescript/repros/tsLibOverrides'
}

What I want: /Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom/index.d.ts
Closest I get: /Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.d.ts

Perhaps instead of currentDirectory I need to append a filename to force it to look in node_modules of the currentDir.

@orta
Copy link
Contributor Author

orta commented Sep 14, 2021

Confirming to myself:

Revised code:

var localOverride = ts.resolveModuleName("@typescript/" + path, currentDirectory + "/tsconfig.json", { moduleResolution: ts.ModuleResolutionKind.NodeJs }, host);
console.log({ libFileName, localOverride, currentDirectory  })

Does:

$ /Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/.bin/tsc
{
  libFileName: 'lib.dom.d.ts',
  localOverride: {
    resolvedModule: {
      resolvedFileName: '/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom/index.d.ts',
      originalPath: undefined,
      extension: '.d.ts',
      isExternalLibraryImport: true,
      packageId: [Object]
    },
    failedLookupLocations: [
      '/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom.ts',
      '/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom.tsx',
      '/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom.d.ts'
    ]
  },
  currentDirectory: '/Users/ortatherox/dev/typescript/repros/tsLibOverrides'
}

The fact that whether the file exists is irrelevant (and so I think tsconfig.json is a bad call because it would feel like a mis-direction for someone reading) but it's not a very satisfying answer.

@orta
Copy link
Contributor Author

orta commented Sep 14, 2021

Looking through the code, this isn't necessarily on a hot route but this does affect isSourceFileDefaultLibrary which is called a bit in the watcher. I did successfully get a module cache not crashing via createModuleResolutionCache real late in the process but never resolved a successful result.

@orta
Copy link
Contributor Author

orta commented Sep 14, 2021

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 14, 2021

Heya @orta, I've started to run the tarball bundle task on this PR at e2ca4b5. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 14, 2021

Hey @orta, 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/110482/artifacts?artifactName=tgz&fileId=D48E94F3C6C5280C83D295B2BD91EAE9FE2072B484565ED195437AEED0CAB59C02&fileName=/typescript-4.5.0-insiders.20210914.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@4.5.0-pr-45771-10".;

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

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

One nit then excited to try this out

src/compiler/program.ts Outdated Show resolved Hide resolved
if (localOverride?.resolvedModule) {
return localOverride.resolvedModule.resolvedFileName;

const resolveFrom = combinePaths(currentDirectory, `/__lib_node_modules_lookup_${libFileName}__.ts`);
Copy link
Member

Choose a reason for hiding this comment

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

This always ignores currentDirectory because the latter argument is absolute.

Copy link
Member

Choose a reason for hiding this comment

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

Guessing this didn’t break tests only because all the tests are rooted at /

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice spot!

@orta
Copy link
Contributor Author

orta commented Sep 15, 2021

I explored setting the node_modules to not be root, but the currentDir is src in tests while unrooted files live at tests/cases/compiler/ in tests - so I either make it all arbitrary or I keep it just as arbitrary with a rooted node_modules

@mikob
Copy link

mikob commented Oct 20, 2021

@orta

All JS dependency managers support this syntax:

has this been tested with yarn 3+? I get this error:

➤ YN0000: ┌ Resolution step
➤ YN0001: │ Error: @typescript/lib-dom@npm:@types/web isn't supported by any available resolver
    at Xc.getResolverByDescriptor (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:294:5330)
    at Xc.bindDescriptor (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:294:4719)
    at p (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:303:6959)
    at async Promise.all (index 1)
    at async Fe.resolveEverything (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:303:8166)

@mikob
Copy link

mikob commented Oct 20, 2021

seems like a yarn issue, workaround here: yarnpkg/berry#1621

@orta
Copy link
Contributor Author

orta commented Oct 21, 2021

Did you use yarnpkg/berry#1621 (comment) e.g.:

"resolutions": {
  "@typescript/lib-dom": "npm:@types/web@*"
}

With yarn add @typescript/lib-dom after? (We can add this to the types/web README)

@mikob
Copy link

mikob commented Oct 21, 2021

I went the other route and just specified @* at the end of the dependency line.

@alshdavid
Copy link

Can this be imported explicitly without globals being set?
eg

import { window, Window, console } from '@typescript/lib-dom'

@dlemfh
Copy link

dlemfh commented Nov 3, 2021

Great work! 👍

May I ask is it possible to use this feature to enforce my project to use specific node types e.g. @types/node@^14.17.0, while my dependency uses @types/node@^16?

What would I need to do if I wanted to not use node types altogether, even when my dependency is using node types?

Concrete use case would be: a dependency library (which uses node types) forcibly introduces type overrides for setTimeout, while my project (a web project) only ever needs the DOM version of setTimeout. I want to prevent @types/node from polluting my "type space".

@orta
Copy link
Contributor Author

orta commented Nov 3, 2021

Can this be imported explicitly without globals being set?

No, that's being researched over at #43972

May I ask is it possible to use this feature to enforce my project to use specific node types?

I don't think this can be too useful there - I think you might need some sort of pre-install script which checks. In theory #40468 could allow for it, but the underlaying issue is that everything in TS lives in a global type space and this at least lets you edit the 'ships with typescript' globals

@cdaringe
Copy link

cdaringe commented Jan 25, 2023

it's not clear to me exactly how to use this, having read the associated blog post.

  • i have a custom runtime (an emulator for a homegrown system with some globals)
  • it looks like we can only use existing lib names, so I'll attempt to squash "dom" as a proxy for my emulator types
  • using pnpm syntax, i'm linking in my lib DOM replacement via:
  "dependencies": {
     // ...snip...
    "@typescript/lib-dom": "workspace:concordance-env@*"
  },
$ tree node_modules/\@typescript/lib-dom/src/
node_modules/@typescript/lib-dom/src/
└── index.d.ts
# all my good stuff!
// index.d.ts
declare var foobar: number;

and specifying:

{
  "extends": "concordance-tsconfig/tsconfig.base.json",
  "compilerOptions": {
    // DOM is squashed with concordance env :)
    "lib": ["DOM"]
  }
}

but the compiler isn't picking up my symbols 🤔, such as foobar: number, as declared above.

did I miss a crucial step?

update--i had an import in my .d.ts which broke it :). removing the import made the symbols pick up.

@andrewbranch
Copy link
Member

Glad you figured it out, but it sounds like this feature isn’t what you want anyway. You’ve always been able to add arbitrary global type declaration files via the "types" key in tsconfig.json if they’re in a node_modules package, or "files"/"include" if they’re local, or /// <reference path="./path/to/globals.d.ts" /> to name a few. What this feature allows you to do is replace existing built-in lib types, like lib.dom.d.ts, while not breaking other dependencies that declare a dependency on those built-ins. If you just want to add global types to your compilation, just include your .d.ts file as you would any other, via one of the methods I mentioned above.

@cdaringe
Copy link

cdaringe commented Feb 2, 2023

hey @andrewbranch, thanks!

You've always been able to add arbitrary global types

kind of! set me straight. these are the kind of global types inclusion by types config field and/or triple-slashes:

/// <reference path="..." /> - assumes types are local to my project (they're not!)
/// <reference types="..." /> - assumes they are @types/ prefixed node packages
/// <reference lib="..." /> - isn't a general "lib", is an enum of env specific libs

what's missing is:

/// <reference package="..." from my perspective.

due to reasons, i cannot publish to DefTyped. feasibly i could path="../../node_modules/foo/bar/index.d.ts, but that's cumbersome in my particular codebase.

for that reason, i thought "well, i could feasibly intercept one of the lib resolvers!". it ended up having other quirks, so i may pick a new path. but i'd love to have a <reference package-name='...' /> for this reason

@andrewbranch
Copy link
Member

andrewbranch commented Feb 2, 2023

Off the top of my head, I’m pretty sure both the "types" array and /// <reference types="..." /> prefer looking up in typeRoots (i.e. node_modules/@types by default) first, but will fall back to looking for the plain package name in node_modules. Am I wrong about that? You can use tsc --traceResolution to audit the process used for every import and triple-slash directive to be sure.

@cdaringe
Copy link

cdaringe commented Feb 2, 2023

Holy guacamole, why didnt i even try!? The docs are adequate, but I misinterpreted them. Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants