Organizing code and CI rules
I'm thinking about code organization patterns, like internal packages in Go and Java or C#'s InternalsVisibleTo. I also need to address governance through ADRs for exceptions, time-bounded waivers, and API extraction using facades and anti-corruption layers.
I want to deliver a document with CI configurations, providing a three-part answer that covers module practices, ownership models, and enforcement rules. I’ll include a layering enforcement template as well as how to handle generated code and test dependencies, using tools like depcruise.Enforcing API boundaries
I'm thinking about how to prevent breaking changes in APIs using semantic versioning and tools like api-diff. The user seems to want general guidance in a compact format, so I'll provide a bullet list outlining a recommended approach:
- Step 0: Define module boundaries and create a module metadata file.
- Step 1: Encode these boundaries in the build tool, restricting visibility and dependency scopes.
- Step 2: Generate graphs to visualize and compute changes.
- Step 3: Assign owners for CODEOWNERS and API reviews to manage cross-module changes.
- Step 4: Implement CI checks for graph cycles, size budgets, new dependencies, public API changes, and enforce rules on the main branch.
- Step 5: Establish an exceptions workflow and maybe include a sample GitHub Actions snippet.Proposing boundary configurations
I'm considering proposing a "dep boundary config file" that could be implemented using Python. It's essential to ensure that we test module boundaries effectively. I’m thinking about using tools like ArchUnit for Java and import-linter for Python to perform these boundary tests. This approach would help maintain organization within the code and prevent any unwanted dependencies from creeping in. Overall, I want to create a solid framework for managing these configurations.
1) Module graphs (make the architecture explicit)
Define what a “module” is
Pick one, document it, and align tooling to it:
- Build modules (recommended):
:target (Bazel), Gradle subproject, Maven module, TS project reference, etc.
- Directory/package modules:
src/<module>/... with import rules (works when build tooling can’t express boundaries).
For each module, define:
- Public API surface (what others may depend on)
- Internal code (must not be imported externally)
- Allowed dependencies (edges out)
- Test-only allowances (edges allowed only in tests)
A simple “dependency contract” file per module helps, e.g. module.yaml:
name: payments
owner: team-fintech
allowed_deps:
- libs:logging
- libs:http
forbidden_deps:
- services:orders
visibility:
public:
- payments/api/**
internal:
- payments/internal/**
Generate and track the dependency graph
Automate graph generation from the source of truth:
- If you have a build graph (Bazel/Gradle/etc.), prefer that.
- Otherwise, parse imports (language-specific tools).
Outputs to produce in CI:
- Full graph (DOT/JSON) for visualization
- Violations report (forbidden edges, cycles, “internal API” usage)
- Graph diff on PRs (new edges introduced)
Common checks:
- No cycles (strongly connected components must be size 1)
- Layering rules (e.g., UI → app → domain → infra; never reverse)
- No transitive leakage (only depend on declared APIs; prevent “reach-through”)
Tooling examples (pick per stack):
- Java/Kotlin: ArchUnit + Gradle module boundaries
- TS/JS: dependency-cruiser / eslint-plugin-boundaries
- Python: import-linter
- Bazel:
visibility + bazel query cycle checks
2) Ownership (make violations socially enforceable)
Map modules to owners
Use:
CODEOWNERS for path-based ownership
- A module registry (YAML/JSON) for “module → owning team” mapping (helps automation)
Example CODEOWNERS:
/libs/domain/ @team-domain
/services/payments/ @team-fintech
/services/orders/ @team-orders
Require owners to approve boundary-relevant changes
Require approvals when a PR:
- Adds/changes dependencies for a module
- Touches a module’s public API surface
- Adds a new module
- Introduces a new cross-module edge (even if “allowed”, owners should see it)
Implementation options:
- GitHub branch protection: “Require review from Code Owners”
- GitLab: CODEOWNERS + approval rules
Introduce “API guardians” per module (optional but effective)
For modules with many consumers:
- Require at least one approval from an “API owners” group when
public/** changes.
- Add automated API diff checks (binary/source/API surface) for breaking change detection.
3) CI rules (make it impossible to merge boundary breaks)
Minimum CI gates to enforce
-
Forbidden dependency edges
Fail if module A depends on module B not in its allowlist (or violates layer rules).
-
No cycles
Fail on any cyclic dependency between modules (and optionally packages).
-
No “internal” imports
Fail if code outside a module imports from internal/** (or non-exported packages).
-
Dependency change detection
Fail (or require special approval) if PR introduces:
- A new module dependency edge
- A new external third-party dependency
- A new “high-level” dependency (e.g., domain depending on infra)
- Exception mechanism with expiry
Don’t rely on “just this once” merges. Use:
- Waiver file with an owner, reason, ticket, and expiration date
- CI fails once waiver expires
Example waiver entry:
- from: services/payments
to: services/orders
reason: "Temporary while extracting shared contract"
ticket: ARCH-421
expires: 2026-01-15
approved_by: team-fintech
Practical PR gating pattern
- Run boundary checks on every PR.
- If violations:
- CI fails with a clear message: which rule, which edge, what fix.
- Provide suggested refactor: “depend on
orders/api not orders/internal”, “introduce interface in domain”, etc.
- If a PR adds new deps but they’re allowed:
- Require explicit acknowledgment (label like
deps-change) and owner approval.
Example CI workflow steps (generic)
- Build/resolve dependency metadata
- Generate module graph (JSON)
- Run checks:
- cycles
- forbidden edges / layering matrix
- internal API usage
- diff against baseline (new edges)
- Upload artifacts (graph + report)
- Gate merge via required status checks
A compact “dependency boundary policy” that works well
- Only depend inward (toward stable/core modules).
- Depend on interfaces/APIs, not implementations (explicit
api/ packages).
- No cycles ever.
- No internal imports across module boundaries ever.
- New dependencies require owner approval.
- Exceptions expire and are tracked.
If you share your stack (language/build system/monorepo vs polyrepo), I can suggest the most direct tooling + a concrete config (e.g., Bazel visibility, Gradle + ArchUnit, dependency-cruiser rules, import-linter contracts, and a ready-to-drop CI job).