Skip to content

Commit dc29b10

Browse files
committed
Rust: Reuse Cargo target dir when running QL tests
1 parent b756a08 commit dc29b10

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

rust/extractor/src/config.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ pub struct Config {
7676
}
7777

7878
impl Config {
79+
/// Returns the directory where Cargo should place its build cache.
80+
pub(crate) fn cargo_target_dir(&self) -> PathBuf {
81+
self.cargo_target_dir.clone().unwrap_or_else(|| {
82+
// When the `target` directory is not explicitly set, we default to
83+
// the relative `target` directory (cargo's default) when running
84+
// qltests. This directory is preserved, so subsequent builds
85+
// benefit from the cache.
86+
if self.qltest {
87+
PathBuf::from("target")
88+
} else {
89+
self.scratch_dir.join("target")
90+
}
91+
})
92+
}
93+
7994
pub fn extract() -> anyhow::Result<Config> {
8095
let args = argfile::expand_args(argfile::parse_fromfile, argfile::PREFIX)
8196
.context("expanding parameter files")?;
@@ -113,6 +128,12 @@ impl Config {
113128
// RUSTUP_AUTO_INSTALL is set to 0 by rust-analyzer (https://github.com/rust-lang/rust-analyzer/issues/20719),
114129
// but we do want to allow rustup to auto-install toolchains if needed, so we set it to 1 here.
115130
extra_env.insert("RUSTUP_AUTO_INSTALL".to_owned(), Some("1".to_owned()));
131+
if self.qltest_cargo_check {
132+
// When running qltests we add this flag to match the `cargo check`
133+
// invocation in the `cargo_check` function. This is neccessary as
134+
// Cargo does not re-use the cache when `RUSTFLAGS` differ.
135+
extra_env.insert("RUSTFLAGS".to_owned(), Some("-Awarnings".to_owned()));
136+
}
116137
extra_env.extend(self.cargo_extra_env.clone());
117138
extra_env
118139
}
@@ -182,12 +203,7 @@ impl Config {
182203
.iter()
183204
.map(|p| join_path_buf(dir, p))
184205
.collect(),
185-
target_dir_config: Utf8PathBuf::from_path_buf(
186-
self.cargo_target_dir
187-
.clone()
188-
.unwrap_or_else(|| self.scratch_dir.join("target")),
189-
)
190-
.map_or(
206+
target_dir_config: Utf8PathBuf::from_path_buf(self.cargo_target_dir()).map_or(
191207
TargetDirectoryConfig::None,
192208
TargetDirectoryConfig::Directory,
193209
),

rust/extractor/src/qltest.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ fn set_sources(config: &mut Config) -> anyhow::Result<()> {
9090
Ok(())
9191
}
9292

93+
fn cargo_check(config: &Config) -> anyhow::Result<()> {
94+
let status = Command::new("cargo")
95+
.env("RUSTFLAGS", "-Awarnings")
96+
.env("CARGO_TARGET_DIR", config.cargo_target_dir())
97+
.arg("check")
98+
.arg("-q")
99+
.status()
100+
.context("spawning cargo check")?;
101+
if status.success() {
102+
info!("cargo check successful");
103+
Ok(())
104+
} else {
105+
anyhow::bail!("requested cargo check failed");
106+
}
107+
}
108+
93109
pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
94110
dump_lib()?;
95111
set_sources(config)?;
@@ -98,17 +114,7 @@ pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
98114
dump_nightly_toolchain()?;
99115
}
100116
if config.qltest_cargo_check {
101-
let status = Command::new("cargo")
102-
.env("RUSTFLAGS", "-Awarnings")
103-
.arg("check")
104-
.arg("-q")
105-
.status()
106-
.context("spawning cargo check")?;
107-
if status.success() {
108-
info!("cargo check successful");
109-
} else {
110-
anyhow::bail!("requested cargo check failed");
111-
}
112-
};
117+
cargo_check(config)?;
118+
}
113119
Ok(())
114120
}

0 commit comments

Comments
 (0)