microsoft/rushstack

[api-extractor] Inline imports sometimes incorrectly hoisted (unmodified) into rollups

Josmithr opened this issue · 1 comments

There are cases where TSC decides it's okay to inline import calls.
In most cases, API-Extractor seems to handle these correctly when generating its rollups, but there are some cases where it fails to do so, potentially hoisting relative file path imports as-is into the rollup.

A contrived example

A package contains the following file structure:

src
* index.ts
* Bar.ts
* foo
  * Foo.ts
 ```

Bar.ts
```typescript
export enum Bar {
	A = 'A',
}

foo/Foo.ts

export type Foo = import('../Bar').Bar.A;

index.ts

export { Foo } from './foo/Foo';

TSC generates the following foo/Foo.d.ts:

export type Foo = import('../Bar').Bar.A;

API-Extractor then generates the following (untrimmed) rollup:

export declare type Foo = import('../Bar').Bar.A;

Note that the untrimmed rollup file lives one level higher in the directory hierarchy than foo, but it hoisted the ../Bar import unmodified, resulting in an invalid rollup.

 
The above example has also been captured here: https://github.com/Josmithr/api-extractor-playground/tree/relative-path-import-repro

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@microsoft/api-extractor version? 7.39.4
Operating system? Linux
API Extractor scenario? rollups (.d.ts)
Would you consider contributing a PR? Yes
TypeScript compiler version? 5.3.2
Node.js version (node -v)? 20.11.0

@octogonz In our last chat, we discussed this issue (or at least a manifestation of it). I was finally able to minify a repro, which I've outlined above and in a sandbox here: https://github.com/Josmithr/api-extractor-playground/tree/relative-path-import-repro.

Let me know if you have any questions.