[api-extractor] API Extractor should treat ambient modules as it did before.#3339
Merged
Conversation
zelliott
requested review from
D4N14L,
apostolisms,
iclanton,
octogonz and
sachinjoseph
as code owners
April 9, 2022 17:58
zelliott
force-pushed
the
ambient-module-fix
branch
from
April 9, 2022 18:21
5c8e6cc to
97a7a81
Compare
Collaborator
|
✔ I confirmed that this patch fixes the error in PR #3338, unblocking the upgrade for the Rush Stack repo. ✔ I also tested this branch against our large monorepo at work. |
octogonz
approved these changes
Apr 9, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pathMappingstest scenario.Background
Before #3321, API Extractor always treated ambient modules as "external" (unless they were specified as bundled packages, but this would be a misuse of that configuration). This meant that API Extractor would never attempt to resolve an ambient module with
ts.getResolvedModule, and thus no errors would be thrown (asts.getResolvedModulereturnsundefinedfor ambient modules, and API Extractor throws ifts.getResolvedModulereturnsundefined).(Note that it's not correct to always treat ambient modules as "external", it's possible for a project to have ambient modules within it, API Extractor just didn't support that use case yet.)
After #3321, API Extractor now uses the
ts.getResolvedModulehelper to determine whether a module is "external". This means that API Extractor also uses this helper to determine if ambient modules are external, which unfortunately doesn't work as stated above. The end result of this was thatts.getResolvedModulewould returnundefinedfor ambient modules, and then we'd consequently throw anInternalError. This is the root cause of the build breakage at #3338.Fix
The fix in this PR is to simply revert to how API Extractor previously handled ambient modules. We know that
ts.getResolvedModulereturnsundefinedfor ambient modules. Thus, within_isExternalModulePath, simply returntrueifts.getResolvedModulereturnsundefined. This results in all ambient modules being treated as external, whichmatches API Extractor's previous behavior.
This PR also altogether removes the
pathMappingstest scenario. This is because this test scenario is not working as expected - in short - the TS compiler used by API Extractor is resolving statements such asexport { AbstractClass } from 'path-mapping';to.tsfiles instead of.d.tsfiles. You can tell this is the case by looking at some of the artifacts generated by API Extractor for that test scenario and noticing that they include syntax from.tsfiles. I think there's some issue with how the path mappings are breaking the generated.d.tsfile import/export statements. I'll need more time to look into this, and the test scenario isn't accurate as-is, so I removed it.Follow up tasks
ts.getResolvedModulereturnsundefinedand (2) whenisExternalLibraryImportisundefined. Can the TypeScript team help with this? There's little to no documentation about this helper.ts.getResolvedModulecan't be used.build-testssub-directories instead of within theapi-extractor-scenariostest case as before, as they require more involved project setup.How it was tested