dependency-cruiser icon indicating copy to clipboard operation
dependency-cruiser copied to clipboard

Feature request: Resolve local path aliases in monorepos

Open ertrzyiks opened this issue 2 years ago • 4 comments

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 avatar Feb 03 '22 16:02 ertrzyiks

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": []
}

ertrzyiks avatar Feb 03 '22 16:02 ertrzyiks

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

daton89 avatar Apr 01 '22 14:04 daton89

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 avatar Oct 21 '22 10:10 neelance

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.

neelance avatar Oct 22 '22 22:10 neelance