sverweij / dependency-cruiser

Validate and visualize dependencies. Your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
https://npmjs.com/dependency-cruiser
MIT License
5.27k stars 250 forks source link

Listing dependancy tree via dependency-cruiser gives me the reference from barel files #938

Closed karthickshanmugam0689 closed 4 months ago

karthickshanmugam0689 commented 4 months ago

Summary

Hi dependency-cruiser team,

We're using dependency-cruiser to analyze the dependency tree within our monorepo. It works well for identifying dependencies at the package level. However, we encounter an issue when using barrel files for re-exports. We are not able to get the complete dependency including the module from other package inside monorepo which is imported into the particular file

Context

Consider the following project structure

* app-1 #( package name as per package.json = @org-web/app-1)
  * src
    * App.ts
    * app1Utils.ts
* libraries
  * common-util #( package name as per package.json = @org-web/common-util)
    * src
       * index.ts
       * utils
          * helper.ts
          * someOtherHelper.ts

In libraries/common-util/src/index.ts we have

export { helper } from './utils/helper';
export { someOtherHelper } from './utils/someOtherHelper';

Inside my app-1/src/App.ts, we have

import { helper } from '@org-web/common-util';
import { app1Utils } from './app1Utils.ts';

// statements list for execution
helper(/** List of arguments */);

Running npx depcruise src on app-1, currently shows

               |-- "@org-web/common-uti" ==> "../../libraries/common-util/src/index.ts"
               |
"src/App.ts" --|
               |
               |-- "./app1Utils.ts"

Running npx depcruise src --max-depth 2 on app-1, currently shows

               |-- "@org-web/common-uti" ==> "../../libraries/common-util/src/index.ts"
               |                             ["./utils/helper","./utils/someOtherHelper.ts"]
"src/App.ts" --|
               |
               |-- "./app1Utils.ts"

The desired outcome is for depedency-cruiser to identify the actual dependency: libraries/common-util/src/utils/helper.ts. But it always resolves to index.ts file of that package or brings in all files reexported in index.ts of that package with --max-depth option. Is there a possibility with madge to even resolve this barrel files and give us the specific file reference even if it is reexported?

Environment

sverweij commented 4 months ago

Hi @karthickshanmugam0689 it seems depedency-cruiser working correctly here. The modules import only the barrel module and it identifies that - in other words the 'index.ts' is the real dependency. In itself it depends on the modules it re-exports.

Technically it is possible (and interesting) to connect re-exports and 'skip' the barrel, but to do so is quite non-trivial (see https://github.com/sverweij/dependency-cruiser/issues/896#issuecomment-1881827884 for some examples) and goes beyond dependency-cruiser's scope. Dependency-cruiser focuses on dependencies between modules as the one thing to do well and not on finer grained analysis.

Also see issues that touched upon the same subject/ are duplicates:

I'll expand this FAQ entry to include the specific scenario of barrel's re-exports.

Is there a possibility with madge to even resolve this barrel files and give us the specific file reference even if it is reexported?

You'd have to ask the maintainer(s) of madge that, but it seems you already did :-)

karthickshanmugam0689 commented 4 months ago

Ah very true @sverweij . Initially we went to the option of barrel files but then later we felt that it would not be a right choice to go towards barrel files 😞 And yes this question is also asked in madge and yes again, a small copy paste as per the description 😛