@@ -626,9 +626,13 @@ impl Config {
626626 let llvm_assertions = llvm_assertions. unwrap_or ( false ) ;
627627 let mut target_config = HashMap :: new ( ) ;
628628 let mut channel = "dev" . to_string ( ) ;
629- let out = flags_build_dir
630- . or ( build_build_dir. map ( PathBuf :: from) )
631- . unwrap_or_else ( || PathBuf :: from ( "build" ) ) ;
629+ let out = if cfg ! ( test) {
630+ test_build_dir ( )
631+ } else {
632+ flags_build_dir
633+ . or_else ( || build_build_dir. map ( PathBuf :: from) )
634+ . unwrap_or_else ( || PathBuf :: from ( "build" ) )
635+ } ;
632636
633637 // NOTE: Bootstrap spawns various commands with different working directories.
634638 // To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
@@ -679,11 +683,6 @@ impl Config {
679683 } ;
680684
681685 let initial_rustc = build_rustc. unwrap_or_else ( || {
682- let out = if cfg ! ( test) {
683- std:: env:: current_dir ( ) . unwrap ( ) . ancestors ( ) . nth ( 2 ) . unwrap ( ) . join ( "build" )
684- } else {
685- out. clone ( )
686- } ;
687686 download_beta_toolchain ( & dwn_ctx, & out) ;
688687 let target = if cfg ! ( test) { get_host_target ( ) } else { host_target } ;
689688
@@ -2395,3 +2394,15 @@ pub(crate) fn read_file_by_commit<'a>(
23952394 git. arg ( "show" ) . arg ( format ! ( "{commit}:{}" , file. to_str( ) . unwrap( ) ) ) ;
23962395 git. run_capture_stdout ( dwn_ctx. exec_ctx ) . stdout ( )
23972396}
2397+
2398+ fn test_build_dir ( ) -> PathBuf {
2399+ env:: var_os ( "CARGO_TARGET_DIR" )
2400+ . map ( |value| Path :: new ( & value) . parent ( ) . unwrap ( ) . to_path_buf ( ) )
2401+ . unwrap_or_else ( || {
2402+ let base = option_env ! ( "CARGO_MANIFEST_DIR" )
2403+ . map ( PathBuf :: from)
2404+ . unwrap_or_else ( || std:: env:: current_dir ( ) . expect ( "failed to get current dir" ) ) ;
2405+
2406+ base. ancestors ( ) . nth ( 2 ) . unwrap_or_else ( || Path :: new ( "." ) ) . join ( "build" )
2407+ } )
2408+ }
0 commit comments