agentleFS
leroyguillaumeleroyguillaume/claude/rust-build-conventions/SKILL.md

rust-build-conventions

Skill7 starsChanged 3 days agoMITView on GitHub

Rust build and test infrastructure: toolchain pin, sqlx database tests, `build.rs`, cargo-chef, test coverage. TRIGGER when: scaffolding a Rust project; pinning the Rust toolchain; a `rust-toolchain.toml` (or `rust-toolchain`) in the repo; editing a `build.rs`; calling `sqlx::migrate!` or writing a `#[sqlx::test]`; "relation does not exist" after adding a migration; building a Rust `Dockerfile` or touching `cargo chef`; CI red after a clippy release; measuring test coverage, `cargo llvm-cov` or `cargo tarpaulin`. SKIP when: no such file or test is involved (`rust-conventions`). Load `rust-conventions` alongside.

  • Installs packages

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When a crate embeds migrations with sqlx::migrate!(...), add a build.rs that re-runs on migration changes.

    line 39
  2. Very specific:

    Create this build.rs as soon as the crate calls sqlx::migrate!, not after the first stale-migration surprise.

    line 52
  3. Very specific:

    When the project ships a Dockerfile, build it with cargo-chef — but only inside the Dockerfile.

    line 62
  4. Very specific:

    Never add cargo-chef to Cargo.toml (deps or dev-deps) and never invoke it in local development or CI outside the image build — it is purely …

    line 76
  5. Very specific:

    Never use testcontainers (or other throwaway-container harnesses) for database tests.

    line 111

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust build and test conventions

Always:

  • Pin the toolchain in mise.toml (mise-conventions), on an exact patch version, never in a rust-toolchain.toml: an existing one moves into mise.toml and is deleted. Without a pin, a clippy release turns CI red on code nobody touched; with one, a toolchain bump is a reviewable diff.
    [tools]
    rust = { version = "<current stable release>", components = ["rustfmt", "clippy"], profile = "minimal" }
    Resolve <current stable release> when you write the file, from mise latest rust or https://releases.rs, never from memory. This pin is not the MSRV. rust-version in Cargo.toml is what consumers must have and should stay as low as the code allows; the toolchain pin is what contributors and CI build with and should stay current. Never collapse the two.
  • For database integration tests, run against a real Postgres (the project's docker-compose service) and isolate with #[sqlx::test] — it creates a fresh database per test, applies the migrations, and injects a PgPool (or PgConnection). Point DATABASE_URL at the compose Postgres when running the suite. Do not use testcontainers.
  • When a crate embeds migrations with sqlx::migrate!(...), add a build.rs that re-runs on migration changes. The macro embeds the migrations at compile time, so adding a new .sql file does not, on its own, make Cargo rebuild the crate — the macro keeps expanding to the stale set and tests fail with confusing "relation does not exist" errors against the just-added schema. The fix is one line:
    // build.rs
    fn main() {
        println!("cargo::rerun-if-changed=build.rs");
        println!("cargo::rerun-if-changed=migrations");
    }
    Create this build.rs as soon as the crate calls sqlx::migrate!, not after the first stale-migration surprise.
  • This is specific to macros that read files themselves (sqlx::migrate! and similar proc macros). include_str! / include_bytes! need no build.rs: rustc records the included file in its dep-info, and Cargo rebuilds when it changes. Whenever a build.rs exists, emit rerun-if-changed for build.rs itself and every path it watches: a build script that emits no rerun-if-changed at all is re-run on any file change in the package. The cargo:: form needs Rust ≥ 1.77; a crate whose rust-version is lower keeps the single-colon cargo: form.
  • When the project ships a Dockerfile, build it with cargo-chef — but only inside the Dockerfile. Install it there with cargo install cargo-chef --locked --version "${CARGO_CHEF_VERSION}", the version in an ARG CARGO_CHEF_VERSION=<latest release> resolved when you write it, preceded by # renovate: datasource=crate depName=cargo-chef for the Renovate custom manager that keeps it current (dependency-update-conventions). The ARG sits at the top of the Dockerfile with the other version knobs (docker-conventions) and is redeclared, bare, in the chef stage: an ARG before FROM is not visible inside a stage. Stage it chef → planner (cargo chef prepare) → builder (cargo chef cook then cargo build --release --locked). The recipe normalises away the package version and other non-dependency Cargo.toml metadata, so the cooked dependency layer stays cached across version bumps and source-only changes. Never add cargo-chef to Cargo.toml (deps or dev-deps) and never invoke it in local development or CI outside the image build — it is purely a Docker layer-caching tool.
  • Measure coverage with cargo-llvm-cov (ci-conventions). It reads rustc's own source-based instrumentation, so it runs on stable, on every platform, and counts regions as well as lines; cargo-tarpaulin defaults to a ptrace engine on Linux and wraps that same instrumentation elsewhere. The registry lacks it, and it needs the llvm-tools component:
    [tools]
    "aqua:taiki-e/cargo-llvm-cov" = "<latest>"
    rust = { version = "<current stable release>", components = ["rustfmt", "clippy", "llvm-tools"], profile = "minimal" }
      coverage:
        desc: Run the tests and measure their line and region coverage
        cmds:
          - cargo llvm-cov --workspace --all-targets --locked {{.CLI_ARGS}}
          - cargo llvm-cov report --html
    
      coverage:summary:
        desc: Print the summary of the last coverage run
        cmds:
          - cargo llvm-cov report
    The HTML report lands in target/llvm-cov/html/. On GitLab, coverage also writes cargo llvm-cov report --cobertura --output-path target/llvm-cov/cobertura.xml, and the job's regex reads the TOTAL line's line coverage: /^TOTAL(?:\s+\S+){8}\s+(\d+(?:\.\d+)?)%/. Branch coverage (--branch) and doctests (--doctests) need nightly: never add a nightly toolchain for them.

Never:

  • Never use testcontainers (or other throwaway-container harnesses) for database tests. Use a real Postgres (the project's docker-compose) with #[sqlx::test] for per-test isolation.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.