Skip to content
LogoLogo

circular-dependencies

Category: correctness. Default: error. Fixable: no. Since: 0.1.0.

What it does

Reports dependencies that are part of a circular chain of imports. The message shows the shortest cycle through each dependency.

Why is this bad?

A cycle makes modules hard to load, to test and to understand alone. A cycle can also cause undefined values at runtime.

Options

  • includeTypeOnly: also count type-only imports. The default is false. A type-only import does not exist at runtime, so a cycle through import type is not a runtime cycle.
  • ignore: globs of modules to remove from the graph before the search for cycles. Use it for a barrel file that many paths include. To limit the rule to some modules instead, use overrides.
  • maxCyclesPerComponent: the number of dependencies to search in each strongly connected component. The default is 100. Use 0 to search all dependencies.

A strongly connected component is a group of modules that import each other. It has one cycle for each dependency, and each search reads the whole group. The rule searches only the first dependencies of a group, in graph order. It then reports one extra violation on the first module of the group. That violation gives the number of dependencies that the rule did not search.

Cycles between folders

  • folders: a list of entries, each with a path. {name} in the path captures a path segment. A glob that starts with ! removes folders from the match. The rule groups the modules below each matching folder. A dependency between modules in two folders of one entry is a dependency between the folders. A cycle between two or more folders of one entry is a violation. The rule honours includeTypeOnly, ignore and maxCyclesPerComponent. It also reports the cycles between modules, as before. To silence a folder cycle, put an oxcruise-ignore-next-line circular-dependencies comment above one dependency of the cycle.
{ "includeTypeOnly": true, "folders": [{ "path": "src/features/{f}" }] }

Configure the rule

{
  "rules": {
    "circular-dependencies": "warn"
  }
}

To set options, use an object: the level, then the options of the rule.