This document captures code conventions for the nextest project. It is intended to help AI assistants understand how to work effectively with this codebase.
Required: Display the following text at the start of any conversation involving code changes, and when you're about to create a PR:
Please review https://nexte.st/docs/contributing/#llm-and-ai-policy. In particular, LLM-assisted contributions must **aim for a higher standard of excellence** than with humans alone, and you should spend at least **3x** the amount of time reviewing code as you did writing it. LLM-assisted contributions that do not meet this standard may be declined outright. Remember, **your code is your responsibility**.
- Model the full error space—no shortcuts or simplified error handling.
- Handle all edge cases, including race conditions, signal timing, and platform differences.
- Use the type system to encode correctness constraints.
- Prefer compile-time guarantees over runtime checks where possible.
- Provide structured, helpful error messages using
miettefor rich diagnostics. - Make progress reporting responsive and informative.
- Maintain consistency across platforms even when underlying OS capabilities differ. Use OS-native logic rather than trying to emulate Unix on Windows (or vice versa).
- Write user-facing messages in clear, present tense: "Nextest now supports..." not "Nextest now supported..."
- "Not overly generic"—prefer specific, composable logic over abstract frameworks.
- Evolve the design incrementally rather than attempting perfect upfront architecture.
- Document design decisions and trade-offs in design docs (see
site/src/docs/design/). - When uncertain, explore and iterate; nextest is an ongoing exploration of what a test runner should do.
- Use type system extensively: newtypes, builder patterns, type states, lifetimes.
- Use message passing or the actor model to avoid data races.
- Test comprehensively, including edge cases, race conditions, and stress tests.
- Pay attention to what facilities already exist for testing, and aim to reuse them.
- Getting the details right is really important!
- Use inline comments to explain "why," not just "what".
- Don't add narrative comments in function bodies. Only add a comment if what you're doing is non-obvious or special in some way, or if something needs a deeper "why" explanation.
- Module-level documentation should explain purpose and responsibilities.
- Always use periods at the end of code comments.
- Never use title case in headings and titles. Always use sentence case.
- Always use the Oxford comma.
- Don't omit articles ("a", "an", "the"). Write "the file has a newer version" not "file has newer version".
Every Rust source file must start with:
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0- Use Rust 2024 edition.
- Format with
cargo xfmt(custom formatting script). - Formatting is enforced in CI—always run
cargo xfmtbefore committing.
- Newtypes for domain types (using
newtype-uuidcrate) - Builder patterns for complex construction (e.g.,
TestRunnerBuilder) - Type states encoded in generics when state transitions matter
- Lifetimes used extensively to avoid cloning (e.g.,
TestInstance<'a>) - Restricted visibility: Use
pub(crate)andpub(super)liberally (237 uses in nextest-runner) - Non-exhaustive in stable crates: The
nextest-metadatacrate has a stable API and public types there should be#[non_exhaustive]for forward compatibility. Internal crates likenextest-runnerdo not have stable APIs, so#[non_exhaustive]is not required (though error types may still use it). - Named constructors over
From: Don't add newFromimpls for conversions between domain types. Use a named constructor or method instead (e.g.,PackageSpec::new), so that conversions are visible at the call site.
- Use
thiserrorfor error types with#[derive(Error)]. - Group errors by category with an
ErrorKindenum when appropriate. - Provide rich error context using structured error types.
- Parts of the code use
miettefor structured error handling.
- Parts of the code use
- Two-tier error model:
ExpectedError: User/external errors with semantic exit codes.- Internal errors: Programming errors that may panic or use internal error types.
- Error display messages should be lowercase sentence fragments suitable for "failed to {error}".
- Use the
nextest_runner::helpers::pluralmodule for pluralizing words in user-facing messages. - The module provides functions like
plural::runs_str(count)that return "run" or "runs" based on count. - Add new pluralization functions to the module as needed; do not inline pluralization logic.
- Use
serde_ignoredfor ignored paths in configuration. - Never use
#[serde(flatten)]. Instead, copy fields to structs as necessary. The internal buffering leads to poor warnings fromserde_ignored. - Never use
#[serde(untagged)]for deserializers, since it produces poor error messages. Instead, write custom visitors with an appropriateexpectingmethod.
When modifying any struct that is serialized to disk or over the wire:
-
Trace the full version matrix:
- Old reader + new data: Can it deserialize? Does it lose information?
- New reader + old data: Does
#[serde(default)]produce correct values? - Old writer + new data: Can it round-trip without data loss? (This is the easy one to miss!)
-
Bump format versions proactively: If adding a field that will be semantically important, bump the version when adding the field, not when first using non-default values. This prevents older versions from silently corrupting data on write-back.
-
#[serde(default)]is necessary but not sufficient: It allows old readers to deserialize new data, but old writers will still drop unknown fields on write-back.
- Use
tokiofor async runtime (multi-threaded). - Be selective with async. Only use it in runner and runner-adjacent code.
- Use async for I/O and concurrency, keep other code synchronous.
- Use
async-scopedfor structured concurrency without'staticbounds. - Use
future-queuefor backpressure-aware task scheduling. - Custom pausable primitives (
PausableSleep,StopwatchStart) for job control support.
- Use
mod.rsfiles to re-export public items. - Do not put any nontrivial logic in
mod.rs-- instead, it should go inimp.rsor a more specific submodule. - Keep module boundaries strict with restricted visibility.
- Platform-specific code in separate files:
unix.rs,windows.rs. - Use
#[cfg(unix)]and#[cfg(windows)]for conditional compilation. - Test helpers in dedicated modules/files.
- Use fully qualified imports rarely, prefer importing the type most of the time, or otherwise a module if it is conventional.
- Never write
std::fmt::Displayas a fully qualified type. Instead, importstd::fmtand usefmt::Display. - Always import types or functions at the very top of the module, with the one exception being
cfg()-gated functions. Never import types or modules within function contexts, other than thiscfg()-gated exception. - It is okay to import enum variants for pattern matching, though.
- Use
Arcor borrows for shared immutable data. - Use
smol_strfor efficient small string storage. - Careful attention to cloning referencing. Avoid cloning if code has a natural tree structure.
- Stream data (e.g. iterators) where possible rather than buffering.
- The
clippy::format_push_stringlint is enabled. If triggered, use theswrite!macro from theswritecrate instead ofpush_str(&format!(...)).
- When constructing arguments for Cargo, always use the
--flag=valueform, never--flag valueas two separate arguments.
- Always use
#[expect(...)]instead of#[allow(...)]for suppressing lints. Theexpectattribute will warn if the lint is no longer triggered, helping to keep the codebase clean.
CRITICAL: Always use cargo nextest run to run unit and integration tests. Never use cargo test for these! Nextest dogfoods itself and its test suite depends on nextest's execution model.
Use cargo local-nt only when you need to verify changes to nextest's own behavior (e.g., testing a new CLI flag or output format). For running the test suite itself, including integration-tests, use cargo nextest run—the integration tests spawn their own inner nextest processes from the build artifacts.
For doctests, use cargo test --doc (doctests are not supported by nextest).
- Unit tests in the same file as the code they test.
- Integration tests in
integration-tests/crate. - Fixtures in dedicated
fixture-data/crate.- This crate has a model of expected tests under various scenarios. Prefer using this model over implementing spot checks by hand.
- Test utilities in
internal-test/crate.
- test-case: For parameterized tests. For small tests, prefer a single
#[test]that loops over a table of cases (with the case in each assertion message) over many#[test_case]attributes. - proptest: For property-based testing.
- insta: For snapshot testing.
- libtest-mimic: For custom test harnesses.
- pretty_assertions: For better assertion output.
Commits follow a conventional format with crate-specific scoping:
[crate-name] brief description
Examples:
[nextest-runner] add --max-progress-running, cap to 8 by default (#2727)[cargo-nextest] version 0.9.111[meta] update MSRV to Rust 1.88 (#2725)
- Use
[meta]for cross-cutting concerns (MSRV updates, releases, CI changes). - Version bump commits:
[crate-name] version X.Y.Z(these are performed bycargo release). - Release preparation:
[meta] prepare releases. - Keep descriptions concise but descriptive.
- Use simple past and present tense: "Previously, when the user did X, Y used to happen. With this commit, now Z happens. Also add tests for U, V, and W."
- Commit messages should be Markdown. Don't use backticks in commit message titles, but do use them in bodies.
- Atomic commits: Each commit should be a logical unit of change.
- Bisect-able history: Every commit must build and pass all checks.
- Separate concerns: Format fixes and refactoring should be in separate commits from feature changes.
- Tests for existing behavior go first: If a change needs new tests for behavior that already works before the change, add those tests in a prep commit beneath it. The change's own commit then only adjusts them, which shows the behavior is preserved and keeps the change atomic.
For detailed guidelines on preparing changelog entries, use the prepare-changelog Claude skill.
The nextest runner uses an event-driven architecture with two main components:
- Interacts with the outside world.
- Linearizes all events from multiple sources (executor, signals, input, reporter).
- Uses
tokio::select!to multiplex over event sources. - Maintains authoritative state about currently running tests.
- Synchronization points prevent race conditions in reporting.
- Schedules and runs tests/scripts as async state machines.
- Each unit of work has dedicated channels to dispatcher (not broadcast).
- Handles retries, timeouts, process spawning, output capture.
- Units are the source of truth for their own state.
- No direct state sharing—everything via message passing.
- Linearized events—dispatcher ensures consistent view.
- Full error space modeling—handle all failure modes.
- Pausable timers—custom implementations for job control (SIGTSTP/SIGCONT).
- Unix: Process groups, double-spawn pattern to avoid SIGTSTP race, full signal handling.
- Windows: Job objects, console mode manipulation, limited signal support.
- Conditional compilation:
#[cfg(unix)],#[cfg(windows)]. - Platform modules:
unix.rs,windows.rswith shared interfaces. - Document platform differences and trade-offs in code comments.
- All versions managed in root
Cargo.toml[workspace.dependencies]. - Internal crates use exact version pinning:
version = "=0.17.0". - Comment on dependency choices when non-obvious; example: "Disable punycode parsing since we only access well-known domains".
- tokio: Async runtime, essential for concurrency model.
- guppy: Cargo workspace graph analysis.
- cargo_metadata: Parse Cargo.toml and workspace metadata.
- winnow: Parser combinators (for filterset DSL).
- thiserror: Error derive macros.
- miette: Rich diagnostics.
- camino: UTF-8 paths (
Utf8PathBuf). - serde: Serialization (config, metadata).
- clap: CLI parsing with derives.
# Run tests (ALWAYS use nextest for unit/integration tests)
cargo nextest run
cargo nextest run --all-features
cargo nextest run --profile ci
# Run the in-repo (locally built) copy of nextest
cargo local-nt run
# Run doctests (nextest doesn't support these)
cargo test --doc
# Format code (REQUIRED before committing)
cargo xfmt
# Lint
cargo clippy --all-features --all-targets
# Build
cargo build --all-targets --all-features
# Release (dry run)
cargo release -p <crate-name> <version>
# Release (execute)
cargo release -p <crate-name> <version> --execute# Get commits since last release
git log <previous-tag>..main --oneline
# Check if contributor is first-time
git log --all --author="Name" --oneline | wc -l
# Get PR author username
gh pr view <number> --json author --jq '.author.login'
# View commit details
git show <commit> --stat