Skip to content

Commit c83a41e

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

3 files changed

Lines changed: 50 additions & 20 deletions

File tree

rust/codeql-extractor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ options:
3636
This value is an optional path to use as `CARGO_TARGET_DIR` for the internal
3737
cargo commands the extractor uses. Pointing it to a persistent directory may
3838
reduce execution time of consecutive extractor runs. By default, a new scratch
39-
directory is used for each run.
39+
directory is used for each extraction, while qltests use the test's `target`
40+
directory so artifacts can be reused across runs.
4041
type: string
4142
cargo_target:
4243
title: Target architecture

rust/extractor/src/config.rs

Lines changed: 23 additions & 7 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")?;
@@ -108,11 +123,17 @@ impl Config {
108123
figment.extract().context("loading configuration")
109124
}
110125

111-
fn get_extra_env(&self) -> FxHashMap<String, Option<String>> {
126+
pub(crate) fn get_extra_env(&self) -> FxHashMap<String, Option<String>> {
112127
let mut extra_env = FxHashMap::default();
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: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,29 @@ fn set_sources(config: &mut Config) -> anyhow::Result<()> {
9090
Ok(())
9191
}
9292

93+
fn cargo_check(config: &Config) -> anyhow::Result<()> {
94+
let mut command = Command::new("cargo");
95+
command.env("CARGO_TARGET_DIR", config.cargo_target_dir());
96+
// Pass the extra environment variables to the initial `cargo check`.
97+
for (key, value) in config.get_extra_env() {
98+
match value {
99+
Some(value) => command.env(key, value),
100+
None => command.env_remove(key),
101+
};
102+
}
103+
let status = command
104+
.arg("check")
105+
.arg("-q")
106+
.status()
107+
.context("spawning cargo check")?;
108+
if status.success() {
109+
info!("cargo check successful");
110+
Ok(())
111+
} else {
112+
anyhow::bail!("requested cargo check failed");
113+
}
114+
}
115+
93116
pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
94117
dump_lib()?;
95118
set_sources(config)?;
@@ -98,17 +121,7 @@ pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
98121
dump_nightly_toolchain()?;
99122
}
100123
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-
};
124+
cargo_check(config)?;
125+
}
113126
Ok(())
114127
}

0 commit comments

Comments
 (0)