Skip to content

feat(ci): configure cargo deny for workspace layer boundary enforcement - #1932

Merged
josecelano merged 3 commits into
torrust:developfrom
josecelano:1925-configure-cargo-deny-for-layer-boundary-enforcement
Jun 23, 2026
Merged

josecelano merged 3 commits into
torrust:developfrom
josecelano:1925-configure-cargo-deny-for-layer-boundary-enforcement

Conversation

@josecelano

Copy link
Copy Markdown
Member

Description

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

Closes #1925

Changes

  • deny.toml (new): workspace root configuration with [bans] section using the wrappers mechanism to restrict which packages can depend on server-layer, protocol, and core crates
  • .github/workflows/testing.yaml: added new layer-bans CI job that runs cargo deny check bans
  • .github/workflows/copilot-setup-steps.yml: added Install cargo-deny step (setup workflow for Copilot agent VM infrastructure)
  • contrib/dev-tools/git/hooks/pre-commit.sh: added cargo deny check bans as a pre-commit step (0.57s runtime)
  • docs/packages.md: added "Layer Boundary Enforcement" section documenting forbidden edges, wrapper mechanism, known exceptions, and maintenance instructions
  • packages/AGENTS.md: added step 7 in "Adding or Modifying a Package" referencing deny enforcement
  • packages/udp-server/Cargo.toml: fixed wildcard path dependency (added missing version)
  • packages/persistence-benchmark/Cargo.toml: fixed wildcard path dependency (added missing version)
  • docs/issues/open/1925-...md: updated implementation tracking and verification results

Verification

  • cargo deny check bans — ✅ (exit 0, zero errors)
  • Forbidden edge test (core -> server) — ✅ detected as error[banned]
  • cargo build --workspace — ✅
  • linter all — ✅
  • cargo test --doc --workspace — ✅
  • Pre-commit hook — ✅ (all steps pass)

@josecelano
josecelano requested a review from a team as a code owner June 22, 2026 18:04
Copilot AI review requested due to automatic review settings June 22, 2026 18:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds automated enforcement of the workspace’s layered architecture by introducing cargo deny bans (via wrappers) and wiring the check into local and CI workflows, plus updating documentation and fixing internal dependency metadata needed to satisfy wildcard bans.

Changes:

  • Added deny.toml with [bans] rules to prevent forbidden cross-layer dependency edges.
  • Integrated cargo deny check bans into CI (testing.yaml) and the local pre-commit hook.
  • Documented the enforcement mechanism and updated internal path dependencies to include explicit versions (to satisfy wildcard bans).

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
deny.toml New cargo-deny bans configuration to enforce layer boundaries via wrappers.
.github/workflows/testing.yaml Adds layer-bans job to run cargo deny check bans in CI.
.github/workflows/copilot-setup-steps.yml Installs cargo-deny in the Copilot setup workflow so agent VMs can run the new checks.
contrib/dev-tools/git/hooks/pre-commit.sh Adds a pre-commit step to run cargo deny check bans.
docs/packages.md Documents layer boundary enforcement, forbidden edges, and maintenance guidance.
packages/AGENTS.md Adds contributor guidance referencing the deny-based enforcement.
packages/udp-server/Cargo.toml Adds missing version for an internal path dependency (avoid wildcard bans failure).
packages/persistence-benchmark/Cargo.toml Adds missing version for an internal path dependency (avoid wildcard bans failure).
docs/issues/open/1925-1669-si-31-configure-cargo-deny-for-layer-boundary-enforcement.md Updates implementation tracking and verification status for the issue spec.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deny.toml Resolved
Comment thread deny.toml Resolved
@josecelano josecelano self-assigned this Jun 22, 2026
@codecov

codecov Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.46%. Comparing base (cefc37f) to head (97b37fb).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1932      +/-   ##
===========================================
- Coverage    79.49%   79.46%   -0.03%     
===========================================
  Files          324      324              
  Lines        23116    23116              
  Branches     23116    23116              
===========================================
- Hits         18375    18370       -5     
- Misses        4465     4476      +11     
+ Partials       276      270       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Install and configure cargo-deny with bans + wrappers to enforce the
workspace layered architecture, preventing accidental dependency edges
between layers.

- Create deny.toml with deny entries for server, core, and protocol crates
- Add layer-bans CI job to testing.yaml workflow
- Add Install cargo-deny to copilot-setup-steps workflow
- Add cargo deny check bans to pre-commit hook
- Document forbidden edges and wrapper rules in docs/packages.md
- Fix two wildcard path dependencies (udp-server, persistence-benchmark)
- Make multiple-versions rationale self-contained in deny.toml
- Tighten protocol crate comment to match actual rule
- Add --locked flag to copilot setup workflow install step
- Mark M4 verification as PASS in issue spec
@josecelano
josecelano force-pushed the 1925-configure-cargo-deny-for-layer-boundary-enforcement branch from 5518180 to e1b01da Compare June 22, 2026 20:41
@josecelano
josecelano requested a review from Copilot June 23, 2026 08:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Comment thread .github/workflows/testing.yaml Resolved
- Pin cargo-deny to v0.19.9 in testing.yaml and copilot-setup-steps.yml
- Update proposed config example in issue spec to match actual deny.toml
- Clean up duplicate notes block in issue spec
@josecelano

Copy link
Copy Markdown
Member Author

All 7 Copilot review comments have been addressed in commits a478b63c and 97b37fb:

  1. multiple-versions rationale — Made self-contained with example crate names
  2. protocol comment wording — Tightened to "directly by torrust-tracker-core"
  3. --locked flag — Added to copilot-setup-steps.yml install step
  4. M4 pending→PASS — Updated in issue spec
  5. Stale example config — Updated udp-protocol wrappers to match actual deny.toml
  6. Version pin in testing.yaml — Pinned to [email protected]
  7. Version pin in copilot-setup-steps.yml — Pinned to [email protected]

(These thread IDs don't expose GraphQL node IDs via the REST API, so I can't use the resolve_thread API — but the fixes are pushed to the branch.)

@josecelano

Copy link
Copy Markdown
Member Author

ACK 97b37fb

@josecelano
josecelano merged commit 94427f3 into torrust:develop Jun 23, 2026
22 checks passed

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a post-merge review of the change from #1932 as it stands at e170b9620 on the default branch.

What still runs: deny.toml, whose [bans] list later commits extended with the REST API protocol, the crates rule B.1 keeps out of torrust-tracker-test-helpers and self wrappers for the testing features; the layer-bans job at .github/workflows/testing.yaml:141-162; the cargo deny check bans step at contrib/dev-tools/git/hooks/pre-commit.sh:60; the pinned cargo-deny install in the agent setup workflow; and the "Layer Boundary Enforcement" section of docs/packages.md:129-220 with step 8 of packages/AGENTS.md:120-124. At this commit every banned crate's direct dependents in the checked graph are on its wrapper list and no wrapper is unmatched, so the check passes. The points below concern what it does not cover and what the documentation says it covers.

[Minor] The wrapper lists bound only direct dependents, but deny.toml:28-30 says "All other uses (direct or transitive) are denied" and docs/packages.md:181-183 says "any transitive dependency from a non-server package, is rejected". cargo-deny accepts a banned crate whenever each of its direct dependents is a wrapper, so a path through an unbanned wrapper passes. Two cases at this commit: adding torrust-tracker-rest-api-runtime-adapter to packages/test-helpers/Cargo.toml passes (there is no cycle, because test-helpers is only a dev-dependency of the crates the adapter pulls in) and gives test-helpers torrust-tracker-core, torrust-tracker-events, torrust-tracker-swarm-coordination-registry, torrust-tracker-client-lib and torrust-tracker-udp-server, the crates deny.toml:105-108 says it must never reach; adding torrust-tracker-rest-api-application to packages/http-core/Cargo.toml also passes, an upward core-to-application edge that packages/AGENTS.md:10-11 rules out, because neither the application crate nor the runtime adapter has an entry. Smallest fix: reword both passages to say that a wrapper list restricts direct dependents and that a path through a wrapper is allowed. To close the two gaps, add entries for torrust-tracker-rest-api-runtime-adapter (wrappers torrust-tracker, torrust-tracker-axum-rest-api-server) and torrust-tracker-rest-api-application (wrappers torrust-tracker-axum-rest-api-server, torrust-tracker-rest-api-runtime-adapter).

[Minor] The check sees only the graph rooted at the root package. .github/workflows/testing.yaml:162 and contrib/dev-tools/git/hooks/pre-commit.sh:60 run cargo deny check bans without --workspace, and the root Cargo.toml is not a virtual manifest, so cargo-deny takes torrust-tracker as the sole root, as deny.toml:107-108 records. 16 of the 37 workspace members are outside that graph: the console client, e2e-tools, hung-component-tracker, persistence-benchmark, torrent-repository-benchmarking and the developer and skill tools. Today the console client depends directly on torrust-tracker-udp-protocol, torrust-tracker-client-lib and torrust-tracker-http-protocol (console/tracker-client/Cargo.toml:26-30) and persistence-benchmark on torrust-tracker-core (packages/persistence-benchmark/Cargo.toml:31). None of these is on the relevant wrapper list, and the check never sees the edges. A new package that the root does not yet depend on is equally unchecked. So packages/AGENTS.md:120-122 ("the check will fail") and the maintenance steps at docs/packages.md:203-216 promise more than actually runs. Smallest fix: state that scope in both places. The full fix is to add the console client to the wrapper lists of the two protocol crates and the client library, and persistence-benchmark to the list of torrust-tracker-core, then run cargo deny --workspace check bans in the job and the hook.

[Minor] The wrapper lists can no longer tell the REST API transport's shipped edges from its test-support edges, and the documentation still calls those edges open violations. Since ad3d91b0e, torrust-tracker-axum-rest-api-server reaches torrust-tracker-core, torrust-tracker-http-core, torrust-tracker-udp-core and torrust-tracker-udp-server only through optional dependencies of its testing feature (packages/axum-rest-api-server/Cargo.toml:23-30, :37-40, :64). Its self dev-dependency (:72) turns that feature on, so the crate has to stay on the wrapper lists at deny.toml:58, :92, :98 and :114, and those lists do not distinguish dependency kinds or features. Removing optional = true from packages/axum-rest-api-server/Cargo.toml:39 restores a shipped transport-to-tracker-core edge, and cargo deny check bans still passes. Meanwhile docs/packages.md:155 and :165-168 still list those edges as "In progress", so nothing tells a reader that the shipped edge is gone and that bringing it back is a regression. Smallest fix: update those rows to say the shipped edges are removed and the wrappers remain only for the testing environment. Guarding the shipped build itself needs a check on its dependency tree, for example in the existing shipped-features job.

[Minor] The pre-commit step needs cargo-deny, but the developer setup does not install it. contrib/dev-tools/git/hooks/pre-commit.sh:60 runs cargo deny check bans, while Step 6 of the setup-dev-environment skill (setup-dev-environment/SKILL.md:86-89) installs only cargo-machete, and docs/packages.md:203-216 gives no install command. A contributor who follows the setup gets cargo's "no such command: deny" at that step, and every commit through the hook aborts. It fails closed, so nothing slips through, but the hook cannot be used until the tool is installed. Fix: add cargo install --locked [email protected] to Step 6, the same pin as .github/workflows/testing.yaml:158.

[Nit] The layer-bans job (.github/workflows/testing.yaml:141-162) declares no permissions, and the workflow sets none either, so on push events it runs with the repository's default token scope. actions/checkout persists that token for later steps by default, where the build scripts compiled by cargo install can read it, although the job only needs to read the tree. Fix: set permissions: contents: read on the job or the workflow, as the agent setup workflow already does.

Checked and found sound: every banned crate's direct dependents in the checked graph are on its wrapper list, and no wrapper is unmatched. Each direct-edge rule in the forbidden-edge tables at docs/packages.md:145-173 that is not marked in progress is enforced for direct edges between crates in the graph. wildcards = "deny" holds with the version fields the change added to the udp-server and persistence-benchmark path dependencies. cargo-deny is installed pinned and --locked in both workflows. The job runs on push and pull_request only, uses no secrets, and fails on a ban error while warnings stay non-fatal. The hook stops at the first failing step. Two gaps present at merge were closed later: protocol -> tracker-core and the transitive tracker-core -> client-lib -> udp-protocol path had no entry until 607422a01 banned torrust-tracker-core and torrust-tracker-client-lib, and the rest-api-core exception went away with 00e657ca0.

This branch was previously deployed

1 inactive deployment
coverage — 97b37fb3 Deployed Jun 23, 2026 by josecelano via Generate Coverage Report #1465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants