Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ compiler issue.

<br>

## 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.

<br>

## Troubleshooting

The Rust compiler's diagnostic output can vary as a function of whether the
Expand Down
16 changes: 10 additions & 6 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -55,11 +55,15 @@ fn cargo_with_rustflags(project: &Project, extra_rustflags: &[&'static str]) ->
cmd
}

fn cargo_target_dir(project: &Project) -> impl Iterator<Item = (&'static str, PathBuf)> {
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<Directory> {
Expand Down
6 changes: 5 additions & 1 deletion src/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading