diff --git a/README.md b/README.md index d0ecf7a..cfdabea 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,27 @@ compiler issue.
+## Sharing the build cache + +By default trybuild compiles each test case under a separate +`target/tests/trybuild` directory and injects `--cfg trybuild` into the +rustflags. Both keep trybuild's compilation fully isolated from whatever the +surrounding workspace is doing. + +If your crate doesn't reference `cfg(trybuild)` anywhere, you can opt into +sharing the parent workspace's incremental build cache by setting the +environment variable `TRYBUILD_NO_CFG`. With that variable set, trybuild omits +the `--cfg trybuild` rustflag and stops overriding `CARGO_TARGET_DIR`, so the +spawned cargo invocation inherits the parent's target directory and can reuse +already-compiled dependency artifacts. For projects with heavy dependency +trees this can make trybuild's cold-cache time substantially shorter. + +Don't set this variable if your crate or any of its dependencies reference +`cfg(trybuild)` — those branches would otherwise be compiled as if trybuild +were absent. + +
+ ## Troubleshooting The Rust compiler's diagnostic output can vary as a function of whether the diff --git a/src/cargo.rs b/src/cargo.rs index 1a38655..f85fdeb 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -7,7 +7,7 @@ use serde_derive::Deserialize; use std::fs::File; use std::path::PathBuf; use std::process::{Command, Output, Stdio}; -use std::{env, io, iter}; +use std::{env, io}; use target_triple::TARGET; #[derive(Deserialize)] @@ -55,11 +55,15 @@ fn cargo_with_rustflags(project: &Project, extra_rustflags: &[&'static str]) -> cmd } -fn cargo_target_dir(project: &Project) -> impl Iterator { - iter::once(( - "CARGO_TARGET_DIR", - path!(project.target_dir / "tests" / "trybuild"), - )) +fn cargo_target_dir(project: &Project) -> Vec<(&'static str, PathBuf)> { + if env::var_os("TRYBUILD_NO_CFG").is_some() { + Vec::new() + } else { + vec![( + "CARGO_TARGET_DIR", + path!(project.target_dir / "tests" / "trybuild"), + )] + } } pub(crate) fn manifest_dir() -> Result { diff --git a/src/rustflags.rs b/src/rustflags.rs index 0913b94..f8d650e 100644 --- a/src/rustflags.rs +++ b/src/rustflags.rs @@ -3,7 +3,11 @@ use std::env; const IGNORED_LINTS: &[&str] = &["dead_code"]; pub(crate) fn toml(extra_rustflags: &[&'static str]) -> toml::Value { - let mut rustflags = vec!["--cfg", "trybuild", "--verbose"]; + let mut rustflags = if env::var_os("TRYBUILD_NO_CFG").is_some() { + vec!["--verbose"] + } else { + vec!["--cfg", "trybuild", "--verbose"] + }; for &lint in IGNORED_LINTS { rustflags.push("-A");