ezolenko / rollup-plugin-typescript2

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

Extra types path in monorepo -- rootDir #464

Closed mokone91 closed 10 months ago

mokone91 commented 11 months ago

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior N/A

  2. Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate yes

  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction N/A

What happens and why it is incorrect

Hi! After upgrading from 0.31.1 to 0.35.0 build for types containing extra 'src' dir, code transpiled fine(same as before package update). I use pnpm workspaces in my project. After investigation I found that id I remove import from another package(import screens from @mycompany/styles) - types generated correctly. I do not have any 'path' or 'rootDir' configured in any repos tsconfigs, and I do not use aliases.

my repo is huge, not sure if I can to extract working example..

UPD: Have test different package versions, seems extra types folder starts to appear in 0.33.0

Environment

pnpm monorepo

Versions

 System:
    OS: macOS 13.5.2
    CPU: (12) arm64 Apple M2 Pro
    Memory: 550.98 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.19.1 - ~/Library/Caches/fnm_multishells/57093_1695717420463/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/57093_1695717420463/bin/yarn
    npm: 8.19.3 - ~/Library/Caches/fnm_multishells/57093_1695717420463/bin/npm
  npmPackages:
    typescript: 5.1.6 => 5.1.6
    rollup: 2.79.1
    rollup-plugin-typescript2: 0.35.0

rollup.config.js

:
```js return { input: getInput(config), // simple glob to 'src/' output, plugins: [ ...getDepsPlugins(config), // depends on build target we add nodeExternals or not json(), replace({ 'process.env.NODE_ENV': JSON.stringify('production'), preventAssignment: true, }), // Only use typescript for declarations - babel will // do actual js transformations typescript({ useTsconfigDeclarationDir: false, emitDeclarationOnly: true, }), vue(vueConfig), postcss(getPostCSSConfig(config)), babel({ exclude: 'node_modules/**', extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'], babelHelpers: 'bundled', presets: [ [ '@babel/preset-env', { targets: 'last 2 versions and > 2%', }, ], ], }), ...(isWatch ? [] : [ cleaner({ targets: [output.dir], }), ]), ], }; ```

tsconfig.options.json

:
```json5 { "compilerOptions": { "target": "es2019", "strict": true, "lib": [ "DOM", "DOM.Iterable", "ES6" ], "module": "esnext", "moduleResolution": "node", "allowJs": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "resolveJsonModule": true, "downlevelIteration": true, "baseUrl": ".", "outDir": "./dist", "sourceMap": true, "declaration": true, "preserveSymlinks": true } } ```

tsconfig.json

:
```json5 { "extends": "./tsconfig.options.json", "exclude": [ "public", "node_modules", ] } ```

/packages/components/tsconfig.json

:
```json5 { "extends": "../../tsconfig.options.json", "include": [ "./src/**/*.vue", "./src/**/*.ts" ], "exclude": ["./dist"], "compilerOptions": { "baseUrl": ".", "jsx": "react", } } ```
agilgur5 commented 11 months ago

Please do not remove the issue template, it is there for a reason.

There are too few details here since the issue template was not used and no repro was provided to really be able to diagnose in any way.

That being said, 0.31.1 is ~2 years old. #332 was a sizeable fix to monorepo symlinks that was released in 0.32.0 and there were many, many other critical fixes since then (since I became a maintainer). The previous behavior could very well have been a bug.

mokone91 commented 11 months ago

Sorry for that, next time i will use template.. Yeah, that's legacy build config and we do not touch it long time. but now one of our dependency add new modules tyes so I start to catch errors like

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
node_modules/@vueuse/core/index.d.cts (13:5)
11:  * @param cancelCallback The provided callback is invoked when a re-evaluation of the computed value is triggered before the previous one finished
12:  */
13: type AsyncComputedOnCancel = (cancelCallback: Fn) => void;

version 0.35 handles this fine, but extra folder for types appear.. Im not sure if it was a bug before - I do not use declarationDir so types should appear in the same place as transpired code - so behavior was fine before.

image

This is not reproduces in the single repo with exactly the same build config(it extracted to separate package and reused around multiple repos) And 'src' folder disappears when i remove import from another package(i import simple .json with tailwind theme) Package from which I import is added to target package dependency

agilgur5 commented 11 months ago

version 0.35 handles this fine, but extra folder for types appear.. Im not sure if it was a bug before - I do not use declarationDir so types should appear in the same place as transpired code - so behavior was fine before.

That sounds like a rootDir misconfiguration. That's not a bug though. See https://github.com/ezolenko/rollup-plugin-typescript2/issues/275#issuecomment-1107881058 for an explanation of how rootDir works.

And 'src' folder disappears when i remove import from another package(i import simple .json with tailwind theme)

TS infers the rootDir if it is not specified. In this case, it sounds like you're importing from a sibling directory, which changes the inferred rootDir. For monorepos, if you're using paths, that can also cause some issues. See https://github.com/ezolenko/rollup-plugin-typescript2/issues/112#issuecomment-1120350360 and #201. As you removed the issue template, you also did not provide a tsconfig or your rpt2 or Rollup configurations. This causes a lot of unnecessary and inefficient blind guesswork as a result.

I'm guessing that older versions probably didn't pick up the path correctly as symlinks just didn't work. You might've been missing some typings or had some incorrect typings when importing due to that.

Sorry for that, next time i will use template..

It would be good to edit your current issue as well. It's still missing plenty of relevant details, as I have mentioned a few times. Keep in mind that most of OSS is run by volunteers (including me). Please be respectful of volunteers' time, such as by filling out the minimum details that a repository asks for in an issue template.

mokone91 commented 11 months ago

ticket updated with more details I have try to add rootDir: './src' to package tsconfig.json - and now it getting error:

[!] (plugin rpt2) Error: src/hooks/useResponsive/useResponsive.ts:1:21 - error TS6059: File '/Users/...../ui-next/packages/ui-components/node_modules/@mycompany/styles/screens.json' is not under 'rootDir' '/Users/..../ui-next/packages/ui-components/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Imported via '@mycompany/styles/screens.json' from file '/Users/..../ui-next/packages/ui-components/src/hooks/useResponsive/useResponsive.ts' with packageId '@mycompany/styles/screens.json@6.8.1'
    Root file specified for compilation

1 import screens from '@mycompany/styles/screens.json';
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/hooks/useResponsive/index.ts

that was expected, but the resolution(If i get it right) is to add 'reference' path to tsconfig file in related package - but there is no tsconfig there as this is simple package with .css and .json anyway, when i added symple tsconfig to styles package - build error still appear

agilgur5 commented 10 months ago

Resolution

Thank you for filling out the issue template. You are missing the rpt2 verbose log, but I don't think that's necessary in this case.

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior N/A

This is applicable and tsc does have the same output. The error is directly from the TS compiler. The error is correct.

So this isn't a bug, but you can workaround it in a few different ways:

  1. set your package.json#types to src
  2. I think you can just add @mycompany/styles/screens.json to your tsconfig exclude and then the rootDir will now be calculated as src again.
    • Since you're just importing a JSON file, I think this workaround is fine. If you were importing TS, for instance, you wouldn't be able to workaround it this way, as the TS rootDir is indeed up one level
    • It's been a while since I've seen this type of issue, so I'm forgetting some of the nuances. If that doesn't work, a tiny repro should jog my memory

Based on your error message, I think this indeed just failed to resolve the symlink in older versions, so it couldn't tell where the directory was.

3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction N/A

It actually would not be difficult to reproduce this, although the pnpm bits wouldn't work with Stackblitz specifically. This would happen with any directory structure like yours where you import from a sibling directory.

Config issues

Now that you've included your configuration, I can see there are some issues in it. Although I don't think they're related, they may cause other issues. > ```js > typescript({ > useTsconfigDeclarationDir: false, > emitDeclarationOnly: true, > }), > ``` this is not correct as `emitDeclarationOnly` is a `tsconfig` option, not an rpt2 option. You probably want to use `tsconfigOverride`: ```diff js - emitDeclarationOnly: true, + tsconfigOverride: { + emitDeclarationOnly: true, + }, ``` also you have two `exclude`s in your config chain, these will overwrite each other. Your second one should specify all of the options > ```js > "exclude": ["./dist"], > ``` > ```js > "exclude": [ > "public", > "node_modules", > ] > ```