@@ -6,7 +6,7 @@ use std::sync::{Arc, Mutex};
66
77use crate :: core:: PackageId ;
88use crate :: core:: compiler:: compilation:: { self , UnitOutput } ;
9- use crate :: core:: compiler:: { self , Unit , artifact} ;
9+ use crate :: core:: compiler:: { self , Unit , UserIntent , artifact} ;
1010use crate :: util:: cache_lock:: CacheLockMode ;
1111use crate :: util:: errors:: CargoResult ;
1212use annotate_snippets:: { Level , Message } ;
@@ -352,11 +352,37 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
352352 #[ tracing:: instrument( skip_all) ]
353353 pub fn prepare_units ( & mut self ) -> CargoResult < ( ) > {
354354 let dest = self . bcx . profiles . get_dir_name ( ) ;
355- let host_layout = Layout :: new ( self . bcx . ws , None , & dest) ?;
355+ // We try to only lock the artifact-dir if we need to.
356+ // For example, `cargo check` does not write any files to the artifact-dir so we don't need
357+ // to lock it.
358+ let must_take_artifact_dir_lock = match self . bcx . build_config . intent {
359+ UserIntent :: Check { .. } => {
360+ // Generally cargo check does not need to take the artifact-dir lock but there are
361+ // two notable exceptions:
362+ // 1. If check has `--test` or `--tests` we need to pass `CARGO_BIN_EXE_` which
363+ // points into the artifact-dir. In theory we don't need to take the lock here
364+ // but we do to satify the Layout struct.
365+ // 2. If check has `--timings` we still need to lock artifact-dir since we will
366+ // output the report files.
367+ let is_test = self . bcx . roots . iter ( ) . any ( |u| u. mode . is_any_test ( ) ) ;
368+ is_test || !self . bcx . build_config . timing_outputs . is_empty ( )
369+ }
370+ UserIntent :: Build
371+ | UserIntent :: Test
372+ | UserIntent :: Doc { .. }
373+ | UserIntent :: Doctest
374+ | UserIntent :: Bench => true ,
375+ } ;
376+ let host_layout = Layout :: new ( self . bcx . ws , None , & dest, must_take_artifact_dir_lock) ?;
356377 let mut targets = HashMap :: new ( ) ;
357378 for kind in self . bcx . all_kinds . iter ( ) {
358379 if let CompileKind :: Target ( target) = * kind {
359- let layout = Layout :: new ( self . bcx . ws , Some ( target) , & dest) ?;
380+ let layout = Layout :: new (
381+ self . bcx . ws ,
382+ Some ( target) ,
383+ & dest,
384+ must_take_artifact_dir_lock,
385+ ) ?;
360386 targets. insert ( target, layout) ;
361387 }
362388 }
0 commit comments