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

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

Open Aaronkala opened this issue 1 year ago • 2 comments

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

  • Version used: latest
  • Node version: 18

Aaronkala avatar Mar 22 '23 10:03 Aaronkala