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

Feature request: Resolve local path aliases in monorepos #565

Open
ertrzyiks opened this issue Feb 3, 2022 · 4 comments
Open

Feature request: Resolve local path aliases in monorepos #565

ertrzyiks opened this issue Feb 3, 2022 · 4 comments
Labels
enhancement this will make dependency-cruiser sweeter

Comments

@ertrzyiks
Copy link

First of all, thanks for the amazing tool! I find dependency-cruiser extremely helpful in my work.

Context

Here is an example repo that showcases a feature that is used in the one that I would like to analyze.
https://github.com/ertrzyiks/dep-cruiser-ts-monorepo

It is a monorepo and the workspace a has a local path alias

Imports using this path alias are not resolved.

It works only if I add a global tsconfig.json to the root of the project

{
  "compilerOptions": {
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "~core/*": ["packages/a/src/this-is-the-core/*"],
    }
  },
  "include": ["./packages"]
}

Expected Behavior

Imports are resolved using the closest tsconfig.json

Current Behavior

The workspace tsconfig.json is ignored

Possible Solution

Considered alternatives

Run dependency cruiser separately on each workspace

@ertrzyiks
Copy link
Author

ertrzyiks commented Feb 3, 2022

Maybe I should use TS projects instead 🤔

[edit] I tried to add a solution tsconfig to the root folder but it doesn't help

{
  "compilerOptions": {
    "noEmit": true
  },
  "references": [
    { "path": "./packages/a" },
    { "path": "./packages/b" }
  ],
  "files": []
}

@github-actions github-actions bot added the stale label Feb 11, 2022
Repository owner deleted a comment from github-actions bot Feb 11, 2022
@sverweij sverweij added enhancement this will make dependency-cruiser sweeter and removed stale labels Feb 11, 2022
@daton89
Copy link

daton89 commented Apr 1, 2022

I'm also interested in this request, ts projects often uses paths to reference other packages. See Nx monorepo etc

@neelance
Copy link
Contributor

We are also now using compilerOptions.paths to switch to absolute import paths. We would love to see this feature implemented.

For now we are using a patch:

diff --git a/node_modules/dependency-cruiser/src/extract/resolve/index.js b/node_modules/dependency-cruiser/src/extract/resolve/index.js
index a877690..62558e7 100644
--- a/node_modules/dependency-cruiser/src/extract/resolve/index.js
+++ b/node_modules/dependency-cruiser/src/extract/resolve/index.js
@@ -26,7 +26,7 @@ function resolveModule(
 ) {
   let lReturnValue = null;
 
-  const lStrippedModuleName = resolveHelpers.stripToModuleName(pModule.module);
+  const lStrippedModuleName = resolveHelpers.stripToModuleName(pModule.module.replace(/^~\//, `${pBaseDirectory}/src/`));
   if (
     isRelativeModuleName(lStrippedModuleName) ||
     ["cjs", "es6", "tsd"].includes(pModule.moduleSystem)

@neelance
Copy link
Contributor

I now managed to solve this without resorting to a patch by using the webpackConfig option with a config file that only provides the resolve.alias option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement this will make dependency-cruiser sweeter
Projects
None yet
Development

No branches or pull requests

4 participants