Enforcing Dependency Boundaries: A Deductive Framework
My dear colleague, you've posed a classic architecture puzzle—one where unchecked dependencies breed chaos, cycles spawn fragility, and ownership dissolves into anarchy. Fear not; I shall deduce a rigorous enforcement strategy using module graphs, ownership, and CI rules. This forms an ironclad triad: visualize (graphs), assign (ownership), and gatekeep (CI).
I'll outline the methodology step-by-step, with tools, rules, and implementation blueprints. Assume a monorepo (e.g., Bazel, Nx, or Turborepo) for scalability, but adapt as needed.
1. Module Graphs: Map the Battlefield
Visualize dependencies as a directed graph to expose violations before they fester.
2. Ownership: Assign Custodianship
Code without owners is a crime scene waiting to happen. Enforce via explicit boundaries tied to teams/individuals.
-
Tools:
| Tool | Use Case | Integration |
|---|
| CODEOWNERS (GitHub) | .github/CODEOWNERS: app/services/ @team-backend | Auto-reviews/PR blocks. |
| Bazel OWNERS | //app/services:OWNERS file | Build-time checks. |
| Danger.js or GitHub Actions | Dynamic enforcement | Scans changes against graph. |
-
Enforcement Rules:
- Boundary Owners: Each module/graph edge has an approver (e.g.,
ui changes touching api need @api-team).
- Dependency Whitelisting: Owners curate allowed deps in
package.json or bazel targets.
# Example MODULE.bazel
go_library(
name = "services",
srcs = [...],
deps = [":domain"], # Only whitelisted
visibility = ["//app/ui:__pkg__"], # Strict visibility
)
- Mutation Guards: PRs crossing boundaries auto-notify/require dual approval.
3. CI Rules: The Unyielding Gatekeeper
Integrate into pipelines (GitHub Actions, GitLab CI, Jenkins) as hard fails. No merge without compliance.
-
Pipeline Stages:
jobs:
1. lint & test # Baseline
2. dep-graph-check # Acyclic + boundaries
3. ownership-verify # CODEOWNERS compliance
4. build-affected # Only changed modules (Nx/Turbo)
5. e2e-if-boundary-crossed # Risky changes get extra smoke
-
Key CI Rules (YAML Snippet for GitHub Actions):
name: Enforce Boundaries
on: [pull_request]
jobs:
graph-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run dep-graph # Fails on cycles/forbidden deps
- name: Boundary Audit
run: |
changed=$(git diff --name-only HEAD~1 | grep 'app/ui')
if [[ $changed ]]; then
nx affected:graph --base=origin/main # Nx example
# Custom: grep for forbidden imports
grep -r 'from.*domain' app/ui/ && exit 1
fi
ownership:
uses: ./.github/workflows/owners.yml # Delegates to CODEOWNERS
-
Advanced Gates:
- Affected-Only Builds (Nx/Turbo):
nx affected --target=build—ignores unaffected modules.
- Synthetic Cycles: CI injects temp deps to test resilience.
- Metrics: Track violations in PR comments (e.g., "3 boundary crosses detected").
The Holistic Verdict
This triad renders your codebase a fortress: observable, accountable, unbreakable. Should any module conspire otherwise, summon me with specifics—logs, graphs, or errant PRs—and I'll dissect the culprit. Elementary! 🕵️♂️