microsoft / rushstack

Monorepo for tools developed by the Rush Stack community
https://rushstack.io/
Other
5.75k stars 588 forks source link

[api-extractor] Support import statements inside namespaces (new in TypeScript 3.9) #1914

Open chudongvip opened 4 years ago

chudongvip commented 4 years ago

error message:

Error: Internal Error: getResolvedModule() could not resolve module name "../enums/local-invitation-failure-reason"
You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.

env:

code

local-invitation-failure-reason.ts

export enum LocalInvitationFailureReason {
    TEXT:  "TEXT"
}

remote-invitation-failure-reason.ts

export enum RemoteInvitationFailureReason {
    ACCEPT_FAILURE:  "ACCEPT_FAILURE"
}

rtm.ts

import { RemoteInvitationFailureReason } from "./remote-invitation-failure-reason"
import { LocalInvitationFailureReason } from "./local-invitation-failure-reason"
let defaultVal:any;

export module ArRTM {
  export const RemoteInvitationFailureReason:RemoteInvitationFailureReason = defaultVal;
  export const LocalInvitationFailureReason :LocalInvitationFailureReason = defaultVal;
  export const VERSION                      :string = "";
}

main.ts

export { RTM } from './rtm';

command line

  1. tsc
  2. api-extractor run
  3. the shell client throw an exception
octogonz commented 4 years ago

I am not able to repro this.

In order to get the code to compile, I had to change the enum definitions to use = like this:

export enum LocalInvitationFailureReason {
    TEXT:  "TEXT"
}

and I needed to rename ArRTM to RTM. But otherwise API Extractor processed it without any trouble.

Could you share a complete branch that repros this problem? Thanks!

chudongvip commented 4 years ago

I am not able to repro this.

In order to get the code to compile, I had to change the enum definitions to use = like this:

export enum LocalInvitationFailureReason {
    TEXT:  "TEXT"
}

and I needed to rename ArRTM to RTM. But otherwise API Extractor processed it without any trouble.

Could you share a complete branch that repros this problem? Thanks!

thanks for reply, sure, i will create private repository and then i will invite you as a collaborator.

octogonz commented 4 years ago

I saw that you created an api-extractor-demo repo. Let me know when it's set up and you have some repro steps. Thanks!

chudongvip commented 4 years ago

I saw that you created an api-extractor-demo repo. Let me know when it's set up and you have some repro steps. Thanks!

ok. you can follow the setp:

  1. npm install
  2. npm run dev or npm run build
  3. api-extractor run

and then you can saw the error show on your shell client

chudongvip commented 4 years ago

I saw that you created an api-extractor-demo repo. Let me know when it's set up and you have some repro steps. Thanks!

Hey bro, how it is going.

octogonz commented 4 years ago

ok. you can follow the setp:

1. npm install
2. npm run dev or npm run build
3. api-extractor run

I don't see any scripts in your package.json for npm run dev or npm run build. I am looking at the master branch.

octogonz commented 4 years ago

I tried running tsc manually and then api-extractor run, but I didn't encounter any error:

api-extractor 7.8.10  - https://api-extractor.com/

Using configuration from api-extractor-demo\config\api-extractor.json

Warning: The API report file is missing. Please copy the file "temp/ar-rtm-sdk.api.md" to "etc/ar-rtm-sdk.api.md", or perform a local build (which does this automatically). See the Git repo documentation for more info.
Warning: src/classes/localInvitation.ts:11:5 - (tsdoc-undefined-tag) The TSDoc tag "@hidden" is not defined in this configuration
Warning: src/classes/localInvitation.ts:17:1 - (ae-missing-release-tag) "LocalInvitation" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Warning: src/classes/localInvitation.ts:23:6 - (tsdoc-undefined-tag) The TSDoc tag "@hidden" is not defined in this configuration
Warning: src/modules/anyrtc-rtm.ts:21:4 - (tsdoc-undefined-tag) The TSDoc tag "@hidden" is not defined in this configuration
Warning: src/modules/anyrtc-rtm.ts:41:1 - (ae-missing-release-tag) "ArRTM" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)

API Extractor completed with warnings

Please provide more detailed repro instructions. Thanks!

chudongvip commented 4 years ago

I tried running tsc manually and then api-extractor run, but I didn't encounter any error:

api-extractor 7.8.10  - https://api-extractor.com/

Using configuration from api-extractor-demo\config\api-extractor.json

Warning: The API report file is missing. Please copy the file "temp/ar-rtm-sdk.api.md" to "etc/ar-rtm-sdk.api.md", or perform a local build (which does this automatically). See the Git repo documentation for more info.
Warning: src/classes/localInvitation.ts:11:5 - (tsdoc-undefined-tag) The TSDoc tag "@hidden" is not defined in this configuration
Warning: src/classes/localInvitation.ts:17:1 - (ae-missing-release-tag) "LocalInvitation" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Warning: src/classes/localInvitation.ts:23:6 - (tsdoc-undefined-tag) The TSDoc tag "@hidden" is not defined in this configuration
Warning: src/modules/anyrtc-rtm.ts:21:4 - (tsdoc-undefined-tag) The TSDoc tag "@hidden" is not defined in this configuration
Warning: src/modules/anyrtc-rtm.ts:41:1 - (ae-missing-release-tag) "ArRTM" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)

API Extractor completed with warnings

Please provide more detailed repro instructions. Thanks!

ooh, sorry,the correct procedure is:

chudongvip commented 4 years ago
PS E:\workbench\anyrtc\anyRTC-SDK\anyRTC-SDK-TS> cd .\ArRtmWebSDK\
PS E:\workbench\anyrtc\anyRTC-SDK\anyRTC-SDK-TS\ArRtmWebSDK> webpack
i 「atl」: Using typescript@3.9.3 from typescript
i 「atl」: Using tsconfig.json from E:/workbench/anyrtc/anyRTC-SDK/anyRTC-SDK-TS/ArRtmWebSDK/tsconfig.json
i 「atl」: Checking started in a separate process...
i 「atl」: Time: 213ms
Hash: 97e5263ea550c849d7a9
Version: webpack 4.43.0
Time: 2333ms
Built at: 2020-06-09 10:20:31
                              Asset       Size  Chunks             Chunk Names
..\lib\classes\localInvitation.d.ts  419 bytes          [emitted]
                   ..\lib\main.d.ts  125 bytes          [emitted]
     ..\lib\modules\anyrtc-rtm.d.ts   1.98 KiB          [emitted]
                           ArRTM.js   2.64 KiB       0  [emitted]  main
Entrypoint main = ArRTM.js
[0] ./src/main.ts 902 bytes {0} [built]
[1] ./src/modules/anyrtc-rtm.ts 1.54 KiB {0} [built]
[2] ./src/classes/localInvitation.ts 1.65 KiB {0} [built]
PS E:\workbench\anyrtc\anyRTC-SDK\anyRTC-SDK-TS\ArRtmWebSDK> api-extractor run

api-extractor 7.8.10  - https://api-extractor.com/

Using configuration from E:\workbench\anyrtc\anyRTC-SDK\anyRTC-SDK-TS\ArRtmWebSDK\config\api-extractor.json

Error: Internal Error: getResolvedModule() could not resolve module name "../enums/local-invitation-failure-reason"
You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.
PS E:\workbench\anyrtc\anyRTC-SDK\anyRTC-SDK-TS\ArRtmWebSDK>
toyobayashi commented 4 years ago

The same problem.

Repo: https://github.com/toyobayashi/denostd/tree/dts-rollup-error

Branch: dts-rollup-error

> npm run prepare

> @tybys/denostd@0.56.0-dev prepare E:\Github\denostd
> node ./scripts/index.js

Output cjs ... (tsc)
Output esm ... (tsc)
Output umd ... (rollup esm output code)
Output umd .d.ts ... (invoke api-extractor dts rollup. entry: dist/esm/std/path/mod.d.ts)

InternalError: Internal Error: getResolvedModule() could not resolve module name "./_interface"

You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.
    at ExportAnalyzer._fetchSpecifierAstModule (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\ExportAnalyzer.js:536:19)
    at ExportAnalyzer._getExportOfSpecifierAstModule (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\ExportAnalyzer.js:451:41)
    at ExportAnalyzer._tryMatchImportDeclaration (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\ExportAnalyzer.js:380:29)
    at ExportAnalyzer.fetchReferencedAstEntity (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\ExportAnalyzer.js:251:45)
    at AstSymbolTable._analyzeChildTree (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\AstSymbolTable.js:282:76)
    at AstSymbolTable._analyzeChildTree (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\AstSymbolTable.js:310:18)
    at AstSymbolTable._analyzeChildTree (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\AstSymbolTable.js:310:18)
    at AstSymbolTable._analyzeChildTree (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\AstSymbolTable.js:310:18)
    at AstSymbolTable._analyzeChildTree (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\AstSymbolTable.js:310:18)
    at AstSymbolTable._analyzeChildTree (E:\Github\denostd\node_modules\@microsoft\api-extractor\lib\analyzer\AstSymbolTable.js:310:18) {
  unformattedMessage: 'getResolvedModule() could not resolve module name "./_interface"'
}
octogonz commented 4 years ago

api-extractor-demo still doesn't repro for me, even using webpack.

But denostd does fail -- I can debug that. Thanks!

octogonz commented 4 years ago

Okay this file is the problem:

C:\GitR\denostd\dist\esm\std\path\win32.d.ts

declare namespace _win32 {
/** This module is browser compatible. */
import { FormatInputPathObject, ParsedPath } from "./_interface";  <=== ERROR
export const sep = "\\";
export const delimiter = ";";
export function resolve(...pathSegments: string[]): string;
. . .
}
export default _win32;

API Extractor uses the TypeScript 3.7 compiler (see issue https://github.com/microsoft/rushstack/issues/1866), which reports an error for that import statement:

TS 1147: Import declarations in a namespace cannot reference a module

Whereas you are using TypeScript 3.9.

When I move the import statement to the top of the file (in both win32.d.ts and posix.d.ts), then API Extractor completes successfully.

We need to update API Extractor to formally support TypeScript 3.9, but sheesh, this year the compiler has been introducing tons of fundamentally new syntax that is difficult to keep up with.

I'll also note that by targeting the bleeding edge compiler, you may reduce the audience who can consume your library. For some users, migrating to latest TypeScript version can involve nontrivial effort.

Could I ask you to keep your denostd repo around until we fix this? It's a pretty useful test case.

toyobayashi commented 4 years ago

Thank you to figure out what is the problem, welcome to use this repo to debug anytime. By the way now api-extractor still does not support import * as ___ from ___ for local paths yet, when can this feature complete?

octogonz commented 4 years ago

The current progress is in https://github.com/microsoft/rushstack/pull/1796, and that branch has been published as 7.8.2-pr1796.0 which you can use until we get the PR merged.