Select the Rust tests affected by a Git change and emit focused cargo nextest commands.
cargo-litmus indexes Rust source relationships, combines them with Cargo package and workspace dependency graphs from cargo-ferris-wheel, and narrows runs to affected modules, test targets, or packages when safe. Selection is conservative: when Litmus cannot prove a narrow filter is complete, it widens to the owning package, workspace, or repository instead of risking a false negative.
Install
cargo-litmus requires cargo-ferris-wheel 1.1.4 or newer on PATH. Litmus invokes the cargo-ferris-wheel binary directly so workspace discovery works from monorepo roots without a root Cargo.toml and in environments where cargo is wrapped by a build cache such as mbx.
cargo-nextest is optional unless you execute the emitted commands or pass --validate-nextest. The minimum supported Rust version is 1.88.0.
# Prebuilt binaries via cargo-binstall. cargo binstall cargo-litmus cargo-ferris-wheel cargo-nextest # Or build the required binaries from source. cargo install cargo-litmus cargo-ferris-wheel
Quick start
Run from a Git repository (or subdirectory) containing one or more Cargo workspaces:
# Build or update target/cargo-litmus.rkyv. cargo litmus index --root . # Select tests for changes between the merge-base of origin/main and HEAD. cargo litmus affected --root . --merge-base origin/main # Select tests for staged, unstaged, deleted, renamed, and untracked worktree changes. cargo litmus affected --root . --worktree # Explain why a path or package selects tests and where selection widens. cargo litmus explain crates/api/src/lib.rs --root . cargo litmus explain --package api-server --root .
Default summary output prints selection and cache telemetry. Pass --format json to print the full machine-readable report to stdout, or --json-output <path> to write the JSON report to disk while keeping summary telemetry on stdout.
Selecting changes
affected accepts explicit file lists or Git revisions:
# Explicit repository-relative paths. cargo litmus affected --files crates/api/src/lib.rs crates/types/src/lib.rs # Exact revision range, including changed line ranges. cargo litmus affected --base origin/main --head HEAD # Staged index changes only. cargo litmus affected --staged # Newline-delimited paths, a JSON array, or a JSON object on stdin. git diff --name-only origin/main...HEAD | cargo litmus affected --files-stdin
--merge-base, --worktree, --staged, and --base/--head are mutually exclusive. Combine --files or --files-stdin with --base and --head to restrict revision-range and changed-line analysis to those paths.
Index caches
The current index defaults to $CARGO_TARGET_DIR/cargo-litmus.rkyv, or target/cargo-litmus.rkyv when CARGO_TARGET_DIR is unset. Override it with --cache (index) or --current-cache (affected, explain).
For revision-aware CI, build an index at the base revision and pass it via --base-cache (or LITMUS_BASE_CACHE):
base_sha="$(git merge-base origin/main HEAD)" base_tmp="$(mktemp -d)" base_worktree="$base_tmp/repo" trap 'git worktree remove --force "$base_worktree"; rmdir "$base_tmp"' EXIT git worktree add --detach "$base_worktree" "$base_sha" cargo litmus index --root "$base_worktree" --cache target/cargo-litmus-base.rkyv cargo litmus affected \ --root . \ --base "$base_sha" \ --head HEAD \ --base-cache target/cargo-litmus-base.rkyv \ --format json
Cache metadata records the repository root and source/Cargo fingerprints. Missing, unreadable, or incompatible caches fall back to a conservative rebuild.
Warm analysis reuses the loaded index without reconstructing its item graph when the supplied changes and repository fingerprints require no update. Adding or removing Rust sources refreshes Cargo metadata to discover automatic targets.
Index format version 12 rebuilds older caches to record include paths that are only partly known (see below); existing base caches should be regenerated for structural comparisons.
How inputs select tests
Litmus classifies each changed path in precedence order:
- Configured inputs:
.cargo-litmus.tomlrules select named workspaces, packages and reverse dependents, or explicitly ignore paths. - Cargo inputs:
.cargo/config.toml(.cargo/config) andrust-toolchain.toml(rust-toolchain) resolve up the directory tree and widen to every workspace.Cargo.tomlandbuild.rsselect their owning package and reverse dependents, or widen to the owning workspace when outside any package. A workspace rootCargo.tomlwhose only change is to[workspace.dependencies](or to the keys of a package at the workspace root, which select that package as above) instead selects the members whose closure in the working-treeCargo.lockcontains a package that a changed entry names (by itspackagekey, or else its own), and their reverse dependents in every workspace, since a path dependency is built with its own workspace's dependencies. An added or removed entry selects nothing: a member starts or stops using it only by changing its own manifest. The manifest is compared with its base copy the same way and under the same fallbacks as the lockfile below, and also widens its workspace when anything else in it changes or the workspace has no readable lockfile; unless the change is confined to[profile],[patch],[replace], or the workspace'sresolver,default-members, ormetadata, the fallback also selects the members' dependents in other workspaces. As with the lockfile, a package that reads the workspace manifest at runtime or from a build script is not detected and needs a.cargo-litmus.tomlrule for it. A workspace'sCargo.lockis compared with its copy at the base revision (--base,--merge-base, orHEADfor--worktreeand--staged): litmus finds the locked packages that were added, removed, or changed (version, source, checksum, dependency list, or[replace]target) and selects the members of that workspace whose locked dependency closure contains one, without askingcargo-ferris-wheel. Every package that compiles the lockfile's bytes (see the compilation map below), or has an include litmus cannot resolve, also keeps its closure. A package that reads the lockfile at runtime or from a build script is not detected and needs a.cargo-litmus.tomlrule for the lockfile, which also turns this comparison off for it. The lockfile falls back to widening its whole workspace, including when a package sits at the workspace root, when no base revision is given, either copy is missing or unparseable, it does not differ from the base copy,--rootdoes not contain its whole workspace, the format version changes or predates version 3, a dependency reference is ambiguous, tables other than[[package]]change, the workspace lists a member path containing.., the changes reach a package beneath the workspace directory that belongs to a nested workspace, the changes reach no member of the workspace, a source that no known package compiles embeds it, the working-tree copy differs from the checked-outHEAD(from the index with--staged;--worktreeskips this check), orcargo metadatarewrites it during indexing. The comparison uses the working tree because that is what the selected tests build. When--headnames a lockfile the checkout does not hold, as when CI checks out a merge commit while naming the pull request head, that copy is compared with the base as well and both selections are kept. When the checkout is a merge commit whose second parent is--head, the checkout's copy is compared with the merge's first parent rather than the base, so lockfile changes already on the target branch are not attributed to the merged branch. Lockfiles do not record features, so feature changes come only from the manifest rules; as with a manifest change, a member that only shares a dependency with an affected member is not selected, even when feature unification changes how that dependency is built. - Indexed and embedded sources: A source or included file maps to every package that compiles it, plus the package whose directory holds it, and their reverse dependents. Litmus finds them with a compilation map: it walks forward from every Cargo target root, following
moddeclarations (inline modules, blocks, and non-mod.rsfiles included),#[path],include!,include_str!, andinclude_bytes!the way rustc resolves them, so a file shared through#[path], a custom targetpath, or a module tree is credited to each package that reaches it. Conditional#[cfg_attr(..., path = "...")]modules follow every alternative and the default location. Literal includes are found inside other macros' arguments and attributes, andconcat!(env!("CARGO_MANIFEST_DIR"), "/...")resolves against the compiling package. Every indexed source is also walked for the package that owns its directory, so a module mounted where the walk cannot see (acfg_if!arm, a proc macro) still maps what it reaches to that package. Reachable sources outside--rootor not tracked are read from disk. An include whose path is only partly known, such asconcat!(env!("CARGO_MANIFEST_DIR"), "/idl/", $path)in amacro_rules!, is bounded by its literal prefix: when that directory lies inside the compiling package, and holds no other package, any change there already selects the package. Inside amacro_rules!that is not#[macro_export], a path relative to the including file is assumed to stay inside the package, since only the package can invoke the macro; a macro argument containing..can defeat that. Any other include litmus cannot bound (a path fromenv!other thanCARGO_MANIFEST_DIRorOUT_DIR, an exported macro's include, a module file declared insidemacro_rules!, or a source that cannot be parsed) marks its package uncertain; every changed file that is not Rust source then also selects that package. Narrowing to a test target, module, or package is skipped for a file that another package compiles or that is included. - Generated paths (
target/**,**/target/**,**/out/**): Have no static source relationship and widen conservatively. - Built-in inert paths (outside package roots, when
default-inert-paths = true): Select nothing and are omitted fromcargo-ferris-wheelqueries, which would otherwise widen on unmapped files. The exception is a package with an include litmus cannot resolve (see above): it may read the file, so it is selected; a.cargo-litmus.tomlignore rule still selects nothing. Built-in inert classification stops at package roots: an unconfigured file beneath a package root still selects that package's closure because package code or tests can read files underCARGO_MANIFEST_DIRat runtime. - Unmapped files: Select the owning package closure when beneath a package root, widen to the workspace when inside a workspace outside its packages, or widen to all workspaces when outside every workspace.
Package-local narrowing requires every changed path to qualify. A manifest or
binary-entrypoint change cannot hide a simultaneous library change: dependent
tests remain selected. Lockfiles are known Cargo inputs, but never justify
package-local narrowing, and a change that includes a lockfile or a workspace
dependency change is not narrowed at all.
Production code stays production regardless of layout: src/tests/, a custom
tests.rs library, or a tests/ directory containing a production target.
Built-in inert classes:
| Class | Examples |
|---|---|
| Documentation, media, and fonts | *.md, *.mdx, *.rst, *.adoc, *.png, *.svg, *.pdf, *.woff2 |
| JavaScript and TypeScript sources | *.ts, *.tsx, *.js, *.jsx, *.mjs, *.cjs, *.mts, *.cts |
| Legal, release, and community | LICENSE*, COPYING*, NOTICE*, CHANGELOG*, CONTRIBUTING*, SECURITY*, CODEOWNERS, .cargo-litmus.toml |
| CI, agent, and editor metadata | .github/**, .agents/**, .claude/**, .cursor/**, .devcontainer/**, .vscode/**, .idea/** |
| Container recipes | Dockerfile*, *.dockerfile, .dockerignore, docker-compose*.yml, docker-compose*.yaml |
Custom builds, proc macros, or tests may consume otherwise inert files. Map those paths with [[rules]] or [[build-script-inputs]], or set default-inert-paths = false to make all unconfigured files conservative.
Other data and configuration files (*.toml, *.yaml, *.json, *.sql, *.snap), scripts, and templates remain conservative. JSON output includes each path's classification in input_explanations. uncertain_packages lists the packages with includes litmus cannot resolve, and why; every changed file that is not Rust source also selects them.
Configuration (.cargo-litmus.toml)
Place .cargo-litmus.toml at the analyzed root to map non-Rust inputs, declare build-script inputs, or ignore repository-specific trees:
default-inert-paths = true [[rules]] paths = ["migrations/**"] workspaces = ["server"] selection = "workspace" [[rules]] paths = ["schemas/**"] packages = ["api-types", "api-server"] selection = "packages" [[rules]] paths = ["ts/**", "vendor/tooling/**"] selection = "ignore" [[build-script-inputs]] paths = ["codegen/**"] packages = ["generated-client"]
- Workspace names: For
selection = "workspace", a workspace whose rootCargo.tomldeclares[package]uses that package's Cargo name regardless of member count. Virtual workspaces use their root directory name (or"root"at the repository root). - Globs:
pathsuseglobsetsyntax (*,**,?,[abc],[!abc],{a,b}) matched against repository-relative paths. - Precedence: A path matched by multiple rules selects targets if any matching rule selects workspaces or packages; it is ignored only when every matching rule sets
selection = "ignore", regardless of rule order. - Validation: Unknown fields, invalid globs, empty path or target lists, contradictory
ignoretargets, and unknown workspace or package names return an error.
Optional nextest validation
Pass --validate-nextest to affected or path-based explain to check emitted module filters with cargo nextest list and widen filters that fail or match no tests:
cargo litmus affected --merge-base origin/main --validate-nextest --format json
cargo nextest list compiles test targets and executes build scripts and proc macros; enable --validate-nextest only on trusted code. Without it, Litmus selects commands statically without running Cargo builds or build scripts.
Development
cargo check --locked --all-targets cargo nextest run --locked --profile ci cargo clippy --all-targets -- -D warnings cargo +nightly-2025-07-08 fmt --check cargo deny check cargo audit
Scenario tests
tests/integration/scenarios.rs tests end-to-end selection accuracy against synthesized multi-workspace Git monorepos and a deterministic cargo-ferris-wheel shim (no installed cargo-ferris-wheel required). Each scenario declares two-sided package expectations: missing a required package fails as a false negative, and selecting beyond allowed packages fails as unintended widening. Add a scenario whenever a change class can narrow or widen selection.
License
Licensed under the MIT License.
