lvd-lint: Enforcing Layer Rules Automatically

A reviewer opens a pull request that touches nine files. Somewhere in the third one is a new import that points the wrong way, from a utility up into a database module. It looks harmless, it compiles, and the tests pass. Nobody notices until the cycle it creates is expensive to remove. Rules that depend on human attention eventually lose.

Three checks, one graph

The book's answer is lvd-lint, a command-line tool that reads your source and answers one question: does the code obey the layering it claims to have? It does so in three ways.

  • Cycle detection. The tool builds the full import graph and looks for strongly connected components using Tarjan's algorithm. Any component with more than one file in it is a cycle, and a cycle is a violation.
  • Layer violation detection. Each file is assigned to a layer from its path, and every import is checked against the dependency rules: a file may only import from layers below it. Within a layer, imports are allowed, except that the Tools layer still forbids tool-to-tool imports.
  • Volatility alignment scoring. Instead of a bare pass or fail, the tool reports a percentage: the share of dependencies that follow the volatility ordering. A score under your configured threshold raises a warning or an error.

That second bullet hides a nice detail. Axiom A5 is not a separate tool or plugin. It falls out of the same check, so the rule from the A5 article is enforced by the same run that catches a utility importing a repository.

Why the path is enough

The assignment of files to layers comes from folders. The tool does not need annotations, decorators or a manifest per file, because the folder-as-layer principle already says where each file lives. That is what makes enforcement cheap to adopt.

Under the hood the book describes four stages: discover files (TypeScript and JavaScript extensions, minus any ignore patterns), parse each one into a syntax tree with the TypeScript compiler API, merge the imports into a directed graph whose nodes carry a layer index, and run the three analyses over that graph. Each analysis is linear in the size of the graph, which is why the book calls it practical for very large codebases.

Running it

The commands below are the ones the book shows. The first is the everyday check, the second makes violations fail the build, the third writes a machine-readable report, and the last re-analyses as files change.

# the everyday check
lvd-lint src/
# make violations fail the build
lvd-lint src/ --strict --fail-on-error
# write a report for other tools
lvd-lint src/ --format json --output lvd-report.json
# re-analyse as files change
lvd-lint src/ --watch

Console output lists each violation with its file and line, the two layers involved, the offending import and a suggested fix, then closes with a summary that includes cycles, violation counts, the alignment score and layer cohesion. You can also point the tool at a single file to analyse it along with its direct imports, and pass --ignore with a glob to skip things like mocks.

Configuration in one file

Everything else lives in a .lvdrc.json at the project root. Its layers array gives each layer a name, a numeric index, one or more glob patterns and a volatilityRange. That range is more useful than it first appears: when change frequency can be measured from git history, a component whose real behaviour falls outside the band of its layer can be flagged as misplaced.

A rules object then maps rule identifiers, such as layer-dependency and volatility-alignment, to "error", "warn" or "off". An error blocks CI, a warning only advises, and off disables the rule. This gives a team a sensible way to adopt the tool on a messy codebase: start strict on cycles, lenient on alignment, and tighten over time.

The second edition extends the configuration for the ninth layer. The UI layer gets its own entry and three further rules, covering the boundary with lower layers, isolation between UI features and the internal order of the UI folders. A layer can declare the contract files it publishes, and the boundary rule lets the UI import from a lower layer only when the target file is one of those published contracts.

The takeaway

The point of lvd-lint is not cleverness. It is that the dependency rule becomes something a machine can say no to, in the editor, on the command line and in the pipeline. Pair it with lvd-metrics to watch the trend, and with a CI pipeline to make the answer binding. The companion code lives at the LVD repository on GitHub.

What the book covers

  • The dependency-extraction algorithm in detail
  • The motivation behind the tool and its full feature list
  • Annotated console and JSON report examples
  • The complete command-line reference and configuration schema