Skip to content

SI-31: Configure cargo deny for workspace layer boundary enforcement #1925

Closed

Description

@josecelano

Subissue of EPIC #1669 — Overhaul: Packages

Goal

Install and configure cargo deny to programmatically enforce the workspace's layered architecture rules, preventing accidental dependency edges between layers from being introduced.

Background

The workspace has a documented layered architecture (see packages/AGENTS.md and the EPIC #1669 layer guardrails). Dependencies may only flow downward — outer layers (servers) may depend on inner layers (core, protocol, domain), but inner layers must not depend on outer layers.

The EPIC defines these forbidden edges:

  • core -> server
  • tracker-core -> core
  • tracker-core -> protocol
  • tracker-core -> server
  • protocol -> core
  • protocol -> tracker-core
  • protocol -> server

Currently, these rules are documented but not enforced by CI. A developer could accidentally add a core -> server dependency edge (e.g., a core package depending on udp-server) and it would compile and pass CI without any check.

cargo deny is the standard Rust tool for linting dependency graphs. Its bans check supports per-crate wrappers — a mechanism to allow a crate as a dependency only for a specific set of direct dependents while denying it for everyone else. This is exactly the primitive needed for layer enforcement.

Why cargo deny instead of other approaches

Approach Limitation
Manual code review Human error; not automated
Custom CI script Reinventing cargo deny
Cargo features / cfg gates Wrong abstraction for this concern
Rust compiler (#![deny(..)]) Cannot enforce cross-crate dependency rules

Known existing layer violation

One violation exists today: rest-api-core (a core-layer package) depends on udp-server (a server-layer package). This is tracked in #1924. Until that violation is fixed, the wrappers list for udp-server will include rest-api-core as a legitimate direct dependent.

Execution order: This issue should be implemented after the rest-api-core decoupling (#1924), or the final PR must include a second commit that removes rest-api-core from the udp-server wrappers. If implemented first, deny will pass but the stale exception would remain until explicitly cleaned up.

Layer map and forbidden edges

Layer classification

Layer Packages
Server (axum-*, *-server) torrust-tracker-axum-http-server, torrust-tracker-axum-rest-api-server, torrust-tracker-axum-health-check-api-server, torrust-tracker-axum-server, torrust-tracker-udp-server
Core (*-core) torrust-tracker-core, torrust-tracker-http-core, torrust-tracker-udp-core, torrust-tracker-rest-api-core
Protocol (*-protocol) torrust-tracker-http-protocol, torrust-tracker-udp-protocol
Domain / Shared torrust-tracker-configuration, torrust-tracker-primitives, torrust-tracker-events, torrust-tracker-swarm-coordination-registry, torrust-tracker-client-lib
Tools / Benchmarks torrust-tracker-test-helpers, torrust-tracker-torrent-repository-benchmarking, e2e-tools, persistence-benchmark
CLI tools torrust-tracker-client (console binary)

Forbidden dependency edges

Edge Description Current violations
core -> server Core must not depend on delivery-layer packages rest-api-core -> udp-server
tracker-core -> core Tracker core must not depend on its protocol-specific wrappers None
tracker-core -> protocol Tracker core must not depend on protocol parsing crates None
tracker-core -> server Tracker core must not depend on server crates None
protocol -> core Protocol crates must not depend on core logic None
protocol -> tracker-core Protocol crates must not depend on tracker core None
protocol -> server Protocol crates must not depend on server crates None
domain -> server Domain/shared packages must not depend on server crates None

Legitimate direct dependents (wrappers)

These are the packages that are currently allowed to depend on server-layer crates:

Server crate Legitimate direct dependents
torrust-tracker-axum-http-server Root binary (torrust-tracker)
torrust-tracker-axum-rest-api-server Root binary (torrust-tracker)
torrust-tracker-axum-health-check-api-server Root binary (torrust-tracker)
torrust-tracker-axum-server axum-http-server, axum-rest-api-server, axum-health-check-api-server, root (torrust-tracker)
torrust-tracker-udp-server axum-rest-api-server, axum-health-check-api-server (dev), rest-api-core (pending fix #1924), root (torrust-tracker)

Scope

In Scope

  • Install cargo deny in CI (GitHub Action) or as part of the existing lint pipeline.
  • Create deny.toml configuration file at the workspace root.
  • Configure the [bans] section with explicit deny entries for each server crate, using wrappers to list the packages that are legitimately allowed to depend on them.
  • Add cargo deny check bans to the CI testing workflow (GitHub Actions).
  • Add cargo deny check bans to the pre-commit hook if it runs fast enough, otherwise to the pre-push hook.
  • Configure the [licenses] and [advisories] checks if desired (secondary benefit).

Out of Scope

  • Fixing existing layer violations (those are separate subissues in EPIC Overhaul: Packages #1669).
  • Configuring cargo deny for non-workspace external dependencies (license checking, advisory scanning) — those are separate concerns.
  • Configuring cargo deny sources checks.

Implementation Plan

ID Status Task Notes / Expected Output
T1 TODO Install cargo deny (or confirm it's available) cargo install --locked cargo-deny or via system package manager
T2 TODO Create deny.toml at the workspace root with bans configuration Configuration matching the proposed deny rules
T3 TODO Run cargo deny check bans and verify it passes All dependency edges match the allowed wrappers
T4 TODO Add cargo deny check bans to CI testing workflow (GitHub Actions) CI catches violations before merge
T5 TODO Add cargo deny check bans to pre-commit (fast) or pre-push (slow) Gated by performance
T6 TODO Verify that adding a test core -> server dep triggers a deny error Proof the enforcement works
T7 TODO Document the deny.toml configuration in packages/AGENTS.md Future developers understand the rules

Acceptance Criteria

  • deny.toml exists at the workspace root with bans configuration.
  • cargo deny check bans passes (exit code 0) on the current workspace state.
  • Adding a forbidden dependency edge (e.g., core -> server) causes cargo deny check bans to fail.
  • CI (GitHub Actions testing workflow) runs cargo deny check bans and rejects changes with new banned edges.
  • The pre-commit or pre-push hook runs cargo deny check bans.
  • packages/AGENTS.md references the deny.toml enforcement in its Adding/Modifying a Package section.

Out of Scope (repeated from above for clarity)

  • Fixing existing layer violations (separate subissues in EPIC Overhaul: Packages #1669).
  • License checking, advisory scanning, and sources checks.

Activity

  1. self-assigned this
    on Jun 20, 2026
  2. added 2 commits that reference this issue on Jun 20, 2026
  3. added 5 commits that reference this issue on Jun 23, 2026
    94427f3
    ad647c7
    9932ccf
    663035a
    07bcd08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions