ezolenko / rollup-plugin-typescript2

Rollup plugin for typescript with compiler errors.
MIT License
819 stars 71 forks source link

`typescript-transform-paths` does not run on `.d.ts` files after upgrade to Rollup 4 #466

Closed SimmeNilsson closed 8 months ago

SimmeNilsson commented 8 months ago

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior No, tsc alone works as expected as long as ts-patch successfully ran. @rollup/plugin-typescript as well, but it has other issues so want rollup-plugin-typescript2 to work.

  2. Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate Only got one plugin in example.

  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction https://github.com/SimmeNilsson/rollup_relative_path_repro

What happens and why it is incorrect

I'm using ts-patch to support typescript-transform-paths in order to transform my imports paths into relative paths. Prior to rollup 4 it was working well for both the generated .js and .d.ts files. But after upgrading to rollup 4, the imports of the .d.ts files are not updated.

Comparing logs with verbosity 3 this part is missing in rollup 4 compared to rollup 3 after line:

rpt2: generated declarations for 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts

Missing lines:

rpt2: dependency 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts'
rpt2:     imported by 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts'
rpt2: resolving 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts' imported by 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts'
rpt2:     to 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts'
rpt2: transpiling '

Environment

Windows 11, VS Code, node 20.9.0, yarn 1.22.21

Versions

  System:
    OS: Windows 11 10.0.22621
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 5.85 GB / 31.80 GB
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    rollup: ^4.6.0 => 4.6.0
    rollup-plugin-typescript2: ^0.36.0 => 0.36.0
    typescript: ^5.3.2 => 5.3.2

rollup.config.mjs

:
```js import typescript from "rollup-plugin-typescript2"; export default { input: ["src/index.ts"], output: { format: "es", dir: "dist", preserveModules: true }, plugins: [typescript()] }; ```

tsconfig.json

:
```json5 { "compileOnSave": false, "compilerOptions": { "alwaysStrict": true, "baseUrl": "./", "declaration": true, "forceConsistentCasingInFileNames": true, "module": "ESNext", "moduleResolution": "node", "outDir": "dist", "paths": { "@/*": ["src/*"] }, "plugins": [ // Transform paths in output .js files { "transform": "typescript-transform-paths", "exclude": ["**/node_modules/**"] }, // Transform paths in output .d.ts files (Include this line if you output declarations files) { "transform": "typescript-transform-paths", "afterDeclarations": true, "exclude": ["**/node_modules/**"] } ], "skipLibCheck": true, "target": "ESNext" }, "include": ["src/**/*.ts"], "exclude": [] } ```

package.json

:
```json { "name": "rollup_relative_path_repro", "version": "1.0.0", "main": "index.js", "license": "MIT", "devDependencies": { "@rollup/plugin-typescript": "^11.1.5", "rimraf": "^5.0.5", "rollup": "^4.6.0", "rollup-plugin-typescript2": "^0.36.0", "ts-patch": "^3.0.2", "tslib": "^2.6.2", "typescript": "^5.3.2", "typescript-transform-paths": "^3.4.6" }, "scripts": { "prepare": "ts-patch install -s", "clean": "rimraf dist", "prebuild": "yarn clean", "build": "rollup --config rollup.config.mjs" } } ```

plugin output with verbosity 3

:
```text rpt2: built-in options overrides: { "noEmitHelpers": false, "importHelpers": true, "noResolve": false, "noEmit": false, "noEmitOnError": false, "inlineSourceMap": false, "outDir": "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/node_modules/.cache/rollup-plugin-typescript2/placeholder", "allowNonTsExtensions": true } rpt2: parsed tsconfig: { "options": { "alwaysStrict": true, "baseUrl": "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro", "declaration": true, "forceConsistentCasingInFileNames": true, "module": 99, "moduleResolution": 2, "outDir": "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/node_modules/.cache/rollup-plugin-typescript2/placeholder", "paths": { "@/*": [ "src/*" ] }, "plugins": [ { "transform": "typescript-transform-paths", "exclude": [ "**/node_modules/**" ] }, { "transform": "typescript-transform-paths", "afterDeclarations": true, "exclude": [ "**/node_modules/**" ] } ], "skipLibCheck": true, "target": 99, "configFilePath": "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/tsconfig.json", "pathsBasePath": "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro", "noEmitHelpers": false, "importHelpers": true, "noResolve": false, "noEmit": false, "noEmitOnError": false, "inlineSourceMap": false, "allowNonTsExtensions": true }, "fileNames": [ "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts", "C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts" ], "typeAcquisition": { "enable": false, "include": [], "exclude": [] }, "raw": { "compileOnSave": false, "compilerOptions": { "alwaysStrict": true, "baseUrl": "./", "declaration": true, "forceConsistentCasingInFileNames": true, "module": "ESNext", "moduleResolution": "node", "outDir": "dist", "paths": { "@/*": [ "src/*" ] }, "plugins": [ { "transform": "typescript-transform-paths", "exclude": [ "**/node_modules/**" ] }, { "transform": "typescript-transform-paths", "afterDeclarations": true, "exclude": [ "**/node_modules/**" ] } ], "skipLibCheck": true, "target": "ESNext" }, "include": [ "src/**/*.ts" ], "exclude": [] }, "errors": [], "wildcardDirectories": { "c:/developerarea/troubleshooting/rollup_relative_path_repro/src": 1 }, "compileOnSave": false } rpt2: typescript version: 5.3.2 rpt2: tslib version: 2.6.2 rpt2: rollup version: 4.6.0 rpt2: rollup-plugin-typescript2 version: 0.36.0 rpt2: plugin options: { "check": true, "verbosity": 3, "clean": false, "cacheRoot": "C:\\DeveloperArea\\Troubleshooting\\rollup_relative_path_repro\\node_modules\\.cache\\rollup-plugin-typescript2", "include": [ "*.ts+(|x)", "**/*.ts+(|x)", "**/*.cts", "**/*.mts" ], "exclude": [ "*.d.ts", "**/*.d.ts", "**/*.d.cts", "**/*.d.mts" ], "abortOnError": true, "rollupCommonJSResolveHack": false, "useTsconfigDeclarationDir": false, "tsconfigOverride": {}, "transformers": [], "tsconfigDefaults": {}, "objectHashIgnoreUnknownHack": false, "cwd": "C:\\DeveloperArea\\Troubleshooting\\rollup_relative_path_repro", "typescript": "version 5.3.2" } rpt2: rollup config: { "external": [], "input": [ "src/index.ts" ], "plugins": [ { "name": "rpt2" }, { "name": "stdin" } ], "output": [ { "dir": "dist", "format": "es", "plugins": [], "preserveModules": true } ] } rpt2: tsconfig path: C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/tsconfig.json rpt2: included: [ "*.ts+(|x)", "**/*.ts+(|x)", "**/*.cts", "**/*.mts" ] rpt2: excluded: [ "*.d.ts", "**/*.d.ts", "**/*.d.cts", "**/*.d.mts" ] rpt2: Ambient types: rpt2: C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/node_modules/@types/estree/index.d.ts rpt2: transpiling 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\src\index.ts' rpt2: cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/code/cache/48695b338c4cbedb3e73a531f32fc02244a4adba' rpt2: cache hit rpt2: cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/syntacticDiagnostics/cache/f5704ba76695d1d71abc2edf3685bb735f949b9a' rpt2: cache hit rpt2: cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/semanticDiagnostics/cache/f5704ba76695d1d71abc2edf3685bb735f949b9a' rpt2: cache hit rpt2: generated declarations for 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts' rpt2: transpiling 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\src\dep.ts' rpt2: cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/code/cache/f1b786e531223aa87a70d8d21829932f5be42e1a' rpt2: cache hit rpt2: cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/syntacticDiagnostics/cache/a5404b1a8021cd8bc29e53d56a6fb0f5b47a4c36' rpt2: cache hit rpt2: cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/semanticDiagnostics/cache/a5404b1a8021cd8bc29e53d56a6fb0f5b47a4c36' rpt2: cache hit rpt2: generated declarations for 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts' rpt2: dependency 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts' rpt2: imported by 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts' rpt2: resolving '@/dep' imported by 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts' rpt2: to 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts' rpt2: rolling caches rpt2: generating target 1 rpt2: emitting declarations for 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts' to 'index.d.ts' rpt2: emitting declarations for 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts' to 'dep.d.ts' created dist in 665ms Done in 1.83s. ```
agilgur5 commented 8 months ago

Thanks for reporting this with many details and a helpful repro!

Rollup changes

There were very few changes in Rollup v4 for plugins, so the only one that I can think might affect this is that skipSelf: true is now the default for this.resolve (from https://github.com/rollup/rollup/pull/5142).

The resolveId hook is what prints out the rpt2: dependency ... logs (these lines). If a resolution is being skipped due to the new default, then those lines missing would make sense.

However, that means that those files were only resolved because rpt2 resolved them itself, and otherwise they were not picked up by Rollup's JS resolver (after they were compiled to JS).

this.resolve usage

Crucially, rpt2 only uses this.resolve for type-only imports, which I added support for in #406. It's currently on this line. (Also the very next line performs circular reference detection. Rollup mentions that skipSelf is primarily used to avoid infinite loops; rpt2 correctly handles them already and so could explicitly use skipSelf: false. That was added in https://github.com/rollup/rollup/pull/2844 and released in Rollup v1.12.0 and so is within rpt2's existing peerDep range of Rollup >=1.26.3).

Neither index.ts nor dep.ts from your repro are type-only files though. Type-only files are also sometimes called "emit-less" files as they output no JS (only declarations / types). Those files are the same in (M)JS as they are in TS. So that's confusing...

logs

But if dep.ts was not resolved, then your logs would look a bit different. You actually do still have the dep.ts resolved log, it's just further down now:

rpt2: transpiling 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\src\dep.ts'
rpt2:     cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/code/cache/f1b786e531223aa87a70d8d21829932f5be42e1a'
rpt2:     cache hit
rpt2:     cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/syntacticDiagnostics/cache/a5404b1a8021cd8bc29e53d56a6fb0f5b47a4c36'
rpt2:     cache hit
rpt2:     cache: 'C:\DeveloperArea\Troubleshooting\rollup_relative_path_repro\node_modules\.cache\rollup-plugin-typescript2/rpt2_0a69b19586e5d8bec6386b448d0a542c0e27d0ee/semanticDiagnostics/cache/a5404b1a8021cd8bc29e53d56a6fb0f5b47a4c36'
rpt2:     cache hit
rpt2: generated declarations for 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts'
rpt2: dependency 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts'
rpt2:     imported by 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts'
rpt2: resolving '@/dep' imported by 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/index.ts'
rpt2:     to 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts'

Two things of note I see there:

  1. there's a cache hit, so the declaration you're getting is from the cache. Have you tried disabling the cache with clean: true? If you had a cache entry from before running ts-patch then it would just be a stale version (as rpt2 doesn't know you used ts-patch in the middle)
  2. the Rollup v4 log has rpt2: resolving '@/dep' instead of Rollup v3's log of rpt2: resolving 'C:/DeveloperArea/Troubleshooting/rollup_relative_path_repro/src/dep.ts'. That suggests that Rollup v3 had a pre-resolved version -- the JS file that Rollup received already had the resolved path, potentially due to ts-transform-paths? 🤔
agilgur5 commented 8 months ago

Yea I actually can't reproduce this in your repro, so I'm gonna guess you had a cache issue.

❯ yarn && yarn build
# ...
yarn run v1.22.19
$ yarn clean
$ rimraf dist
$ rollup --config rollup.config.mjs

src/index.ts → dist...
created dist in 665ms
✨  Done in 2.18s.

❯ cat dist/index.d.ts 
export { print } from "./dep";

With verbosity: 4, my logs include:

rpt2: cleaning cache: /insert-dir-here/rollup_relative_path_repro/node_modules/.cache/rollup-plugin-typescript2/rpt2_7bce521227b9e56193f5193a09435b6e0649d412
rpt2: transpiling '/insert-dir-here/rollup_relative_path_repro/src/index.ts'
rpt2: generated declarations for '/insert-dir-here/rollup_relative_path_repro/src/index.ts'
rpt2: transpiling '/insert-dir-here/rollup_relative_path_repro/src/dep.ts'
rpt2: generated declarations for '/insert-dir-here/rollup_relative_path_repro/src/dep.ts'
rpt2: dependency '/insert-dir-here/rollup_relative_path_repro/src/dep.ts'
rpt2:     imported by '/insert-dir-here/rollup_relative_path_repro/src/index.ts'
rpt2: resolving './dep' imported by '/insert-dir-here/rollup_relative_path_repro/src/index.ts'
rpt2:     to '/insert-dir-here/rollup_relative_path_repro/src/dep.ts'
rpt2: generating target 1
rpt2: emitting declarations for '/insert-dir-here/rollup_relative_path_repro/src/index.ts' to 'index.d.ts'
rpt2: emitting declarations for '/insert-dir-here/rollup_relative_path_repro/src/dep.ts' to 'dep.d.ts'

Same ordering (might be a Rollup v4 thing?), but it does say rpt2: resolving './dep' instead of @/dep

Also thought it's worth mentioning that your yarn.lock includes internal Artifactory references. I had to re-construct it myself (as I of course don't have access to those), and that could be a security concern for your organization; you may want to re-write the git history of your repo with public references.

SimmeNilsson commented 8 months ago

Sorry for late reply. I've been out sick.

Thank you for the in-depth analysis! It indeed seems to be a cache issue in combination with ts-patch not being applied at times.

Thanks for the tip. I forgot I had set repo globally on my computer. I've fix the yarn.lock file now.