@@ -76,6 +76,21 @@ pub struct Config {
7676}
7777
7878impl 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 ) ,
0 commit comments