sverweij / dependency-cruiser

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

Question: How to use "scope: folder" for detecting circular imports between folders in a folder #762

Open Aaronkala opened 1 year ago

Aaronkala commented 1 year ago

Summary

We have features/libraries in a modules/ folder. We'd like to allow a acyclic dependency graph between the modules while guarding against circular dependencies.

If a file in modules/a/ depends on a file in modules/b/, nothing in modules/b/ can depend on modules/a/.

I noticed that there's a scope option. But I'm unsure how would I configure the rule and I'm looking for some examples.

Context

The following rule configuration leaks outside the /modules/ folder causing them to be included in the "circular" detection. Example error caused by this: /module/a -> /common/ -> /api/ -> /views/ -> /module/a.

{
  name: 'no-circular-dependency-between-modules',
  comment:
    'If a module A depends on module B, then ' +
    'module B should not depend on module A',
  severity: 'error',
  from: {
    path: '/modules/([^/]+)'
  },
  to: {
    circular: true,
  },
  scope: "folder",
},

What I'd love to get is some way to confine the scope to only circulars between the /module/ folders. The via examples look like they might provide a fix here, but I'm unsure if scope: "folder" supports via yet.

The full solution we are trying to implement is:

  1. Forbid acyclic imports between modules
  2. Forbid all imports between modules, unless explicitly allowed (a can import b)

Environment

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

zzwdpq commented 2 months ago

大佬请教下,如何检测文件夹之间循环依赖的,有源码可以参考么 @sverweij ,求回复