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

TS importModuleSpecifier could work better for lerna monorepos with libs only #36624

Closed
lgraziani2712 opened this issue Jan 20, 2020 · 6 comments · Fixed by #40637
Closed

TS importModuleSpecifier could work better for lerna monorepos with libs only #36624

lgraziani2712 opened this issue Jan 20, 2020 · 6 comments · Fixed by #40637
Assignees
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@lgraziani2712
Copy link

lgraziani2712 commented Jan 20, 2020

TS Template added by @mjbvz

TypeScript Version: 3.8.0

Search Terms

  • importModuleSpecifier
  • auto import
  • mono repo / monorepo

Related issue: #36181 (I thought was something related to TS itself, but maybe is not).

Right now, the typescript.preferences.importModuleSpecifier auto option only uses relative paths for files nearer to the actual file than the basePath configuration file. But this isn't the best approach for a monorepo full with libs.

I only want to have aliases on imports from other lib folders, but I want to keep every import within a lib as relative imports. The problem is, VSCode suggestions sometimes adds the alias to imports within files of the same lib. When I build and publish everything as is, the service using that lib will crash because doesn't understand the alias.

Thank you in advance!

@mjbvz
Copy link
Contributor

mjbvz commented Jan 21, 2020

Can you provide a small example project that shows what doesn't work today and explain the expected behavior?

@vscodebot vscodebot bot closed this as completed Jan 28, 2020
@vscodebot

This comment has been minimized.

@lgraziani2712
Copy link
Author

Sorry for the delay. Here is the repo: https://github.com/lgraziani2712/vscode-88977

Here is a comment in the built file: lgraziani2712/vscode-88977@07e82a9#r37005671

@mjbvz mjbvz reopened this Jan 28, 2020
@mjbvz mjbvz transferred this issue from microsoft/vscode Feb 5, 2020
@mjbvz mjbvz removed their assignment Feb 5, 2020
@mjbvz
Copy link
Contributor

mjbvz commented Feb 5, 2020

Moving upstream for feedback from TS team

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Feb 26, 2020
@Ore4444
Copy link

Ore4444 commented Apr 20, 2020

I'm having the same issue.
compilerOptions.baseUrl in my project tsconfig is set to root of monorepo modules.
In every module tsconfig, I have: compilerOptions.outDir = "lib" and compilerOptions.rootDir = "src"
I don't have a baseUrl in module tsconfigs which is what causes the mismatch of relative imports (i have exactly the same symptoms as #36181).
If I add compilerOptions.baseUrl = "src" to a module tsconfig, it does solve the issue of doing move refactoring, but for some reason tsc fails with lots of TS2307. Now none of my compilerOptions.paths are resolved for that module.
The difference between baseUrl and no baseUrl can easily be verified by moving a folder or renaming a file with webstorm.
Seems like when compilerOptions.baseUrl is set in a composite project, compilerOptions.paths of parent tsconfig stops having a effect.

I've had this issue since the beginning of my monorepo 1.5 years ago. Finally this issue gave me a direction. I'd love to give more context if that would help.

More issues I found that might be related: #36235, #35768, #20110

@andrewbranch
Copy link
Member

andrewbranch commented Aug 7, 2020

@Ore4444 what you’re describing sounds like a separate issue than the original post here. Are you able to provide an example repo demonstrating your problem?

If I add compilerOptions.baseUrl = "src" to a module tsconfig, it does solve the issue of doing move refactoring, but for some reason tsc fails with lots of TS2307. Now none of my compilerOptions.paths are resolved for that module.
The difference between baseUrl and no baseUrl can easily be verified by moving a folder or renaming a file with webstorm.
Seems like when compilerOptions.baseUrl is set in a composite project, compilerOptions.paths of parent tsconfig stops having a effect.

Actually, this sounds like expected behavior. paths are resolved relative to baseUrl, so if you override baseUrl in a child tsconfig, that changes how the parent paths resolve—specifically, in this case, all those paths will fail to resolve.

Most of what’s described in this issue, including 100% of the problem in @lgraziani2712’s example repo, is essentially a duplicate of #31869, and would be solved by implementing that proposal. It does leave open the issue of trying to use a common set of paths in a base tsconfig, though, because each composite project will be able to resolve itself with its unrooted paths entry. Example:

tsconfig.base.json: "paths": { "lib-a": ["packages/lib-a"], "lib-b": ["packages/lib-b"], "lib-c": ... }
packages/
  app/
    tsconfig.json (extends base, references all libs)
  lib-a/
    tsconfig.json (extends base, references b and c)
  lib-b/
    tsconfig.json (extends base, no references)
  lib-c/
    ...

The intention is probably:

  • the app package can reference any lib by its path mapping - ✔️
  • the lib-a package can reference lib-b and lib-c by their path mappings - ✔️
  • the lib-a package cannot reference itself by the "lib-a" path mapping - ❌

To accomplish this, even if #31869 is implemented, each project would need its own paths setting to avoid the pitfall of being able to reference itself. I hoped this would be solvable already by letting lib-a/tsconfig.json specify "paths": { "lib-a": [] }, but paths in a child tsconfig completely overwrites paths in an extended config instead of merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants