Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra types path in monorepo -- rootDir #464

Closed
mokone91 opened this issue Sep 26, 2023 · 5 comments
Closed

Extra types path in monorepo -- rootDir #464

mokone91 opened this issue Sep 26, 2023 · 5 comments
Labels
kind: support Asking for support with something or a specific use case problem: no repro No reproduction was provided (and have not tried to repro without one) solution: duplicate This issue or pull request already exists solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc)

Comments

@mokone91
Copy link

mokone91 commented Sep 26, 2023

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

:
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

:
{
  "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

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

/packages/components/tsconfig.json

:
{
  "extends": "../../tsconfig.options.json",
  "include": [
    "./src/**/*.vue",
    "./src/**/*.ts"
  ],
  "exclude": ["./dist"],
  "compilerOptions": {
    "baseUrl": ".",
    "jsx": "react",
  }
}
@agilgur5 agilgur5 added problem: removed issue template OP removed the issue template without good cause problem: no repro No reproduction was provided (and have not tried to repro without one) labels Sep 26, 2023
@agilgur5
Copy link
Collaborator

agilgur5 commented Sep 26, 2023

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
Copy link
Author

mokone91 commented Sep 26, 2023

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 agilgur5 added the kind: support Asking for support with something or a specific use case label Sep 26, 2023
@agilgur5
Copy link
Collaborator

agilgur5 commented Sep 26, 2023

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 #275 (comment) 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 #112 (comment) 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.

@agilgur5 agilgur5 added solution: duplicate This issue or pull request already exists solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin labels Sep 26, 2023
@mokone91
Copy link
Author

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 agilgur5 removed the problem: removed issue template OP removed the issue template without good cause label Sep 26, 2023
@agilgur5
Copy link
Collaborator

agilgur5 commented Sep 29, 2023

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.

     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:

-        emitDeclarationOnly: true,
+        tsconfigOverride: {
+          emitDeclarationOnly: true,
+        },

also you have two excludes in your config chain, these will overwrite each other. Your second one should specify all of the options

  "exclude": ["./dist"],
  "exclude": [
    "public",
    "node_modules",
  ]

@agilgur5 agilgur5 added the topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc) label Sep 29, 2023
@agilgur5 agilgur5 changed the title Extra types path in monorepo Extra types path in monorepo -- rootDir Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: support Asking for support with something or a specific use case problem: no repro No reproduction was provided (and have not tried to repro without one) solution: duplicate This issue or pull request already exists solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc)
Projects
None yet
Development

No branches or pull requests

2 participants