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

Doesn't work with project reference when same path is configured to difference packages. #153

Open
jjangga0214 opened this issue Jan 5, 2021 · 5 comments

Comments

@jjangga0214
Copy link

jjangga0214 commented Jan 5, 2021

Let's say there is a directory tree like this.

packages
  └── foo
  │   ├── src
  │   │   ├── hello.ts
  │   │   └── index.ts
  │   └── tsconfig.json
  └── bar
      ├── src
      │   ├── hi.ts
      │   └── index.ts
      └── tsconfig.json

And each file is like these.

// packages/foo/src/index.ts
import { hello } from '~/hello' // intended to import './hello'

export const helloworld = () => {
  hello('world')
}
// packages/foo/tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "~/*": ["src/*"],
    }
  }
}
// packages/bar/src/index.ts
import { helloworld } from '@monorepo/foo'
import { hi } from '~/hi' // Same as importing './hi'

hi('there')
helloworld()
// packages/bar/tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "~/*": ["src/*"],
      "@monorepo/*": [ "../*/src/" ]
    }
  },
  "references": [
    { "path": "../foo/tsconfig.json", "prepend": false }
  ]
}

There are two different packages foo and bar in monorepo. They use the same path ~ internally.
Each package is developed by different teams independently. Thus, the consumer(bar) doesn't know whether the dependency(foo) configures ~ as its path (And it shouldn't care as well!).

So a developer of bar runs npx ts-node -r tsconfig-paths/register src/index.ts in packages/bar.
But it fails with the error message like this.

⨯ Unable to compile TypeScript:
../foo/src/index.ts:4:23 - error TS2307: Cannot find module '~/hello' or its corresponding type declarations.

4 import { hello } from '~/hello'
                        ~~~~~~~~~

This happens because two packages use the same path internally.
I expected tsconfig-paths fully respects typescript project reference feature, but it seems not.

How do you think this can be resolved?

Thanks.

@crystalgreen
Copy link

I have a similar issue. I use node with ts-node and tsconfig-paths.
If my path mapping is defined in an inner tsconfig that is referenced via TS project references by the root tsconfig, I get error TS2307 (cannot find module). When I move (and adapt) the mapping to the root tsconfig, it works.

@ryall
Copy link

ryall commented Mar 20, 2021

I'm also fighting with this issue and was just about to make a new ticket about it until I saw yours.

I had a quick look at the code here and it seems only the first tsconfig.json is loaded from the local package at the registration point. From there, it expects all imports to match those paths, regardless of whether the path exists in another referenced package or not.

Would be really nice to have this feature added as it's pretty important for monorepos in my opinion.

@xcfox
Copy link

xcfox commented Jul 23, 2021

I'm having the same problem, I'm currently using tsc-alias as an alternative.

@Pavel910
Copy link

Pavel910 commented Apr 6, 2022

Same requirement here. Have a bunch of workspaces, and each has its own tsconfig.json and its own paths.

It would be great if we could pass some kind of a tsconfigResolver to the register method (I'm using it programmatically), so that we can resolve to a specific config based on the location of the file being processed.

Something like this maybe:

require("tsconfig-paths").register({
  tsconfigResolver(file) {
    // Return the tsconfig from anywhere based on the `file`
    return resolveTsconfigFrom(file);
  }
})

I would be willing to work on a PR, but would need a confirmation/directions from the maintainer if he'll be interested in such functionality. Thanks!

@thefakequake
Copy link

Any updates on fixing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants