[api-extractor] ts.getResolvedModule with TypeScript 5.3
JoshuaKGoldberg opened this issue · 11 comments
Summary
I'm trying to run api-extractor on a repository with TypeScript 5.3 as a dev dependency. It was previously working with TypeScript 5.2.
Repro steps
See typescript-eslint/typescript-eslint#7821:
- Check out that PR branch & commit
yarn
yarn build
(specifically, thepackages/ast-spec
task)
Expected result: Successful API extraction / build?
Actual result:
packages/ast-spec $ yarn build
api-extractor 7.38.0 - https://api-extractor.com/
Using configuration from ./api-extractor.json
Analysis will use the bundled TypeScript version 5.3.0-beta
ERROR: ts.getResolvedModule is not a function
packages/ast-spec $
Details
I'm guessing this is because of the (ts as any).getResolvedModule
used to access TypeScript compiler internals:
rushstack/apps/api-extractor/src/analyzer/TypeScriptInternals.ts
Lines 89 to 92 in 4f15a3c
microsoft/TypeScript#55790 removed that internal API.
Standard questions
Please answer these questions to help us investigate your issue more quickly:
Question | Answer |
---|---|
@microsoft/api-extractor version? |
Both 3.6.1 and 3.8.0 |
Operating system? | Mac |
API Extractor scenario? | rollups (.d.ts) |
Would you consider contributing a PR? | Yes |
TypeScript compiler version? | 5.3.0-beta |
Node.js version (node -v )? |
20 (n/a) |
Just to clarify, did you encounter this error while making a PR for API Extractor to upgrade its bundled version? Looking at the code, the bundled version is currently supposed to be 5.0.x. (We do need to update that.)
(In other words, are you reporting a bug that API Extractor loaded the wrong TypeScript version, instead of the bundled version? Or are you asking for help making an API Extractor PR to support the latest TypeScript?)
Or are you asking for help making an API Extractor PR to support the latest TypeScript?
This one: that it's crashing on the latest beta version of TypeScript.
Ok, I can take a look at it tomorrow morning. Most likely we just need to find what happened to that internal API in the latest branch.
Right, the getResolvedModule()
API was removed from utilities.ts
in microsoft/TypeScript#55790 (which is not been officially released yet, and only affects the daily/beta releases to date).
It seems that @sheetalkamat later exposed an equivalent API as an /** @internal */
member of ts.program
in microsoft/TypeScript#55818. That means we now need a ts.program
object in order to call the API, but this is conveniently already available in the two places in ExportAnalyzer
that need it.
API Extractor's tests seem to pass with this small change. I'll make a PR for you to try.
We don't usually do official releases of API Extractor bundling beta versions of the TypeScript compiler. And (for exactly the reasons seen here) we don't provide a way for API Extractor users to override the compiler version.
@JoshuaKGoldberg are you just doing investigative testing? Or are you planning to publish a release of your project? If so, maybe we need to publish a "beta" release of API Extractor that you can depend on? Let me know.
planning to publish a release of your project
Yup, this one. We publish a new version of typescript-eslint every Monday, and aim to have support for each new version of TypeScript as soon as possible (https://typescript-eslint.io/users/dependency-versions#typescript).
Try this: #4408
After manually applying the changes in local node_modules/
:
✖ nx run ast-spec:build
api-extractor 7.38.0 - https://api-extractor.com/
Using configuration from ./api-extractor.json
Analysis will use the bundled TypeScript version 5.3.0-beta
ERROR: Internal Error: getResolvedModule() could not resolve module name "./base/Accessibility"
/Users/josh/repos/typescript-eslint/packages/ast-spec/dist/index.d.ts:1:1
You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.
Warning: run-commands command "yarn build" exited with non-zero status code
Yup, this one. We publish a new version of typescript-eslint every Monday, and aim to have support for each new version of TypeScript as soon as possible (https://typescript-eslint.io/users/dependency-versions#typescript).
@JoshuaKGoldberg okay but in general, API Extractor's bundled version of typescript does NOT need to be >=
the version in use by your project. API Extractor reads the .d.ts
files of your project, the way an external consumer would, and of course you don't mandate that consumers of typescript-eslint
must have the absolute latest version of typescript
. The TypeScript compiler normalizes .d.ts
files during emit, and also they have a simplified syntax compared to regular .ts
files. And also, API Extractor is designed so that its analysis (including .d.ts
rollups) will generally work correctly even the file contains unrecognized syntaxes.
In fact, looking at your branch, I think the whole problem is that your yarn
resolutions are producing an incorrect installation of API Extractor:
API Extractor's package.json
file requests:
"dependencies": {
"@microsoft/tsdoc": "0.14.2",
"@microsoft/tsdoc-config": "~0.16.1",
"colors": "~1.2.1",
"lodash": "~4.17.15",
"resolve": "~1.22.1",
"semver": "~7.5.4",
"source-map": "~0.6.1",
"typescript": "~5.0.4", // 👈👈👈
"@microsoft/api-extractor-model": "7.28.2",
"@rushstack/node-core-library": "3.61.0",
"@rushstack/rig-package": "0.5.1",
"@rushstack/ts-command-line": "4.16.1"
},
I tried doing a normal npm install @microsoft/api-extractor
in another folder and then symlinking your API Extractor installation to use that typescript
version:
And then your project builds just fine using TypeScript 5.0.4:
Conclusion
- We should improve API Extractor to work with the latest TypeScript 👍
- HOWEVER, your project has a wrong installation that does not respect the SemVer ranges requested by
package.json
files. This is most likely caused by the"resolutions"
field from thepackage.json
file in your project root folder. In trying to override some versions, those overrides are overreaching and breaking the functionality of dependencies. If you fix that problem, you can use the current API Extractor without any changes.
Got it, thanks! I really appreciate you debugging this for us ❤️. Filed an issue on our end to not take up too much space on the rushstack issue tracker. typescript-eslint/typescript-eslint#7839
After manually applying the changes in local
node_modules/
:
@JoshuaKGoldberg with the PR build of API Extractor, I am not able to reproduce the error that you reported.
C:\GitEx\typescript-eslint> nx run ast-spec:build
> nx run ast-spec:build
api-extractor 7.38.0 - https://api-extractor.com/
Using configuration from ./api-extractor.json
Analysis will use the bundled TypeScript version 5.3.0-beta
API Extractor completed successfully
———————————————————————————————
> NX Successfully ran target build for project ast-spec (7s)
Could you provide more detailed repro instructions? I used typescript-eslint/typescript-eslint@e198efb
Hmm, at this point it's likely I misunderstood something and/or the fix you suggested around resolutions would fix things.