$ show dtolnay/trybuild
Trybuild is a Rust library that provides a test harness for compiler diagnostics, commonly known as UI tests. It allows developers to write test cases that compile Rust source files and assert that the compiler's error messages match expected output stored in `.stderr` files. The library is widely used for testing procedural macros and other compile-time error reporting. The API is minimal: `TestCases::new()`, `.pass(path)`, and `.compile_fail(path)`. Under the hood, trybuild creates a temporary Cargo project for each test suite, compiles each test case individually using `cargo check` or `cargo build`, captures the compiler's JSON output, normalizes file paths and other machine-specific details, and compares the result against saved snapshots. It supports both pass and compile-fail tests, glob patterns, workspace inheritance, path dependencies, and various normalization strategies to keep snapshots stable across environments. The codebase is exceptionally well-structured, with clean separation of concerns, a sophisticated normalization engine that maintains backward compatibility through multiple normalization variations, and robust file locking for concurrent test execution. It is actively maintained by David Tolnay and is a cornerstone of the Rust procedural macro ecosystem.
The LLM identified 4 files worth highlighting for reuse:
The normalization engine that transforms raw rustc diagnostic output into a stable, comparable form. It handles path normalization (source dir, workspace, target dir, cargo registry, rustlib, path dependencies), removes volatile line numbers for external crates, strips non-deterministic messages (e.g., 'aborting due to', 'For more information'), and supports multiple normalization variations to avoid breaking existing snapshots when new normalization steps are added. This module is self-contained, well-tested, and could be extracted as a standalone crate for any tool that needs to compare rustc output across environments.
A clever utility that determines which Cargo features are enabled for the current test binary by parsing the fingerprint JSON files generated by Cargo's build system. It handles both the legacy and new build directory layouts. This is a reusable pattern for any tool that needs to introspect the feature set of a Rust crate at test time, without requiring the user to pass features explicitly.
A robust file-based lock implementation that coordinates access to shared resources across multiple test threads and processes. It combines an intra-process mutex with a filesystem lockfile that includes staleness detection (busting locks older than 1.5 seconds) and a background thread that periodically updates the lockfile's mtime to prevent false staleness. This is a self-contained, production-quality locking primitive that could be reused in any Rust project requiring cross-process synchronization.
A thin wrapper around the `dissimilar` crate that computes a character-level diff between expected and actual compiler output. It includes safeguards against large or non-ASCII inputs and only renders the diff if the common content is at least 80% of the larger input. The module gracefully degrades when the `diff` feature is disabled or on Windows, providing a zero-cost abstraction. This pattern of optional diffing with fallback is reusable for any tool that compares textual output.