Anti-Patterns: Fail the Build Only on Certainties
Why one upward import costs more than it looks, and why a gate should fail the build only on the few violations a machine can judge with certainty.
A violation caught while you type costs seconds. The same violation caught in code review costs a round trip, and caught in production it costs far more. The idea behind LVD's automation chapter is simple: run the same checks at every stage, so each stage is a safety net for the one before it.
The book arranges its checks by how quickly they answer. At commit time, a hook gives feedback within the developer's flow. At pull-request time, a pipeline gives the whole team a verdict and a record. And at edit time, an editor extension could point at the problem before the file is even saved. Each stage uses the tools from the previous articles on lvd-lint and lvd-metrics.
A hook, set up in the book with husky, stops violations before they reach the shared repository. Its trick is restraint: it lists only the staged TypeScript files, runs the linter on those, and exits quietly if there are none. It uses the quiet and fail-on-error options so the only output is a short message when something is wrong, and a non-zero exit blocks the commit. Because only the files you touched are analysed, the book reports that it finishes in under a second for typical commits.
A hook is advisory in practice, since anyone can skip it. It is there for speed, not enforcement. Enforcement belongs in the next stage.
The book gives a GitHub Actions workflow that runs the full toolchain on every pull request, and a GitLab template that does the same. In outline it chains commands you have already met:
# 1. lint and keep the report
lvd-lint src/ --format json --output lvd-lint-report.json
# 2. regenerate the picture
npx lvd-diagram --type layers --output architecture.svg
# 3. enforce the thresholds
npx lvd-metrics gate --config .lvd-gates.yml
It first lints and saves a JSON report, then generates the architecture diagram (the heatmap is generated too), then runs the quality gate, and finally posts the numbers as a comment on the pull request.
One design choice deserves attention. The lint step is marked to continue even on failure, and a separate step at the very end fails the job if linting failed. The effect is that the developer gets the diagram, the metrics and the comment even when there are violations, instead of a red build with nothing to look at. Failing last is kinder than failing first.
For systems with a UI layer, the book adds a step before linting: regenerate the API client and confirm the checked-in copy matches.
git diff --exit-code src/ui/services/generated
A stale generated client is a quiet way for the UI and the API to drift apart, and this check makes the drift loud.
A table of metrics is useful, but a table of differences is better. The more mature variant in the book analyses both the base branch and the pull request, then posts a comment with a before and after for each metric, a note of which violations the change resolved, and an overall verdict. A reviewer sees at once whether a change improves or degrades the architecture, without running anything locally. Reviewing a change to an order service becomes a question of "did cohesion go up or down?" rather than "does this feel tidy?"
The last stage in the chapter is presented as concepts rather than a finished product. A VS Code extension, or an equivalent for another editor, would embed lvd-lint as a library and re-analyse incrementally on save. A diagnostic provider would show violations in the Problems panel next to the TypeScript and ESLint errors, with a message that names the rule and suggests the fix. A code action provider would offer quick fixes such as moving an import to the Resources layer or extracting shared logic to Utils.
The book also sketches hover tooltips that show the layer of an imported file, whether the dependency is compliant and how many layers it crosses. The aim is that an architecture rule becomes as immediate as a syntax error, and developers absorb the layer boundaries just by working.
Put the cheap check where it is fastest, put the binding check where it cannot be skipped, and make the pull request show the trend, not just a verdict. The source for the tools is in the companion repository, and the diagram step is covered in lvd-diagram.