Skip to content

feat: add Cargo.toml manifest checker for Rust dependencies - #21

Merged
algomaster99 merged 4 commits into
mainfrom
claude/cargo-package-version-defaults-2a5zyv
Aug 24, 2026
Merged

feat: add Cargo.toml manifest checker for Rust dependencies#21
algomaster99 merged 4 commits into
mainfrom
claude/cargo-package-version-defaults-2a5zyv

Conversation

@algomaster99

Copy link
Copy Markdown
Member

Adds support for checking Rust Cargo.toml manifests, enabling the hook to detect and block writes that pin crates to outdated versions.

Summary

Implements a new cargo package that checks Cargo.toml files for exactly-pinned (=) dependencies that are older than the latest released version. This brings Rust/Cargo support to yul alongside the existing npm, PyPI, Maven, Go, and GitHub Actions checkers.

Key Changes

  • New pkg/cargo package with manifest parsing and version checking:

    • parseCargoPins() extracts exactly-pinned dependencies from Cargo.toml using git-pkgs/manifests, handling string-form (serde = "=1.0.0"), table-form ({ version = "=1.0.0" } ), and special cases (local paths, workspace dependencies)
    • CheckCargoToml() diffs before/after manifests and reports mismatches only for changed dependencies, reusing the shared pins.Diff logic
    • Correctly treats bare versions (e.g., "1.2.3") as caret ranges (^1.2.3), not exact pins, per Cargo's default semantics — only = prefix counts as exact
    • Scopes pins by section (runtime/, development/, build/) since the parser doesn't expose per-declaration locations
  • Integration into main hook:

    • Wired cargo.Checker into newCheckers() in main.go
    • Updated README.md to document Cargo.toml support
  • Comprehensive test coverage (pkg/cargo/cargo_test.go):

    • Parsing of various dependency formats and sections
    • Exact pin detection (bare versions ignored, = prefix required)
    • Changed-dependency-only checking (untouched crates never queried)
    • Same-crate-different-section independence
    • Fake resolver to avoid network calls
  • Benchmark cases added for empirical evaluation:

    • Five fresh/existing Cargo.toml scenarios covering reqwest, serde, tracing, uuid, and tokio

Implementation Notes

  • Follows the same pattern as pkg/golang and pkg/pyproject for ecosystems where the manifest parser doesn't expose stable per-declaration locations
  • Fails open (never blocks) if the resolver lacks pkg:cargo data, matching existing behavior for other ecosystems
  • Uses requireOperator=true in pins.ExactVersion() to enforce the = prefix, documenting the Cargo-specific gotcha explicitly

https://claude.ai/code/session_014uMpiDuvuN7HV9fgPZaQu2

Adds pkg/cargo, wires it into main.go's checker registry, and documents it
in CLAUDE.md/README.md. Per #4: Cargo's default
requirement operator is caret, so a bare version like `serde = "1.2.3"`
means `^1.2.3`, not an exact pin - only an explicit `=1.2.3` counts.
pins.ExactVersion is called with requireOperator=true for this, matching
pyproject.toml's Poetry-table handling.

git-pkgs/manifests' Cargo.toml parser doesn't populate Declarations the
way npm/pypi/maven/github_actions do, so this checker builds its own
scope+name location key from Dependencies instead, the same fallback
pkg/golang uses for go.mod.

Also adds 5 benchmark/cases.json cases (cargo-01..04 fresh + a
cargo-05 existing-manifest case) following the HTTP-client/JSON/
logging/domain-4th-pick archetype used for the other ecosystems.
Comment thread pkg/cargo/cargo.go Outdated
Comment thread pkg/cargo/cargo.go Outdated
Comment thread pkg/cargo/cargo.go Outdated
Comment thread pkg/cargo/cargo.go Outdated
claude and others added 2 commits August 24, 2026 08:37
Cut the design-notes comment on pkg/cargo/cargo.go down to its
non-obvious points (caret-default gotcha, why pins are keyed by
scope+name, unverified resolution coverage), and move the local-path/
workspace-dep detail inline next to the ExactVersion call it explains,
instead of a standalone paragraph.
Adds the recorded hook/nohook runs for the five cargo cases, ignores
target/ (Cargo's build cache, analogous to node_modules/), and extends
the "why some ecosystems need this more than others" table: `cargo add`
resolves the latest version like `go get`/`npm install` do, so there's
usually nothing stale for the hook to catch. Cargo has one more wrinkle
though — exactness (`=`) is opt-in rather than implicit, so a manifest
scaffolded by hand can carry an unpinned, stale version the hook never
sees; cargo-01-reqwest's hook run is a live example of this happening.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Comment thread README.md Outdated
@algomaster99
algomaster99 marked this pull request as ready for review August 24, 2026 10:30
@algomaster99
algomaster99 merged commit 8b430c0 into main Aug 24, 2026
2 checks passed
@algomaster99
algomaster99 deleted the claude/cargo-package-version-defaults-2a5zyv branch August 24, 2026 10:51
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.

2 participants