@@ -1121,13 +1121,18 @@ impl Step for Sysroot {
11211121 fn run ( self , builder : & Builder < ' _ > ) -> Interned < PathBuf > {
11221122 let compiler = self . compiler ;
11231123 let host_dir = builder. out . join ( & compiler. host . triple ) ;
1124- let sysroot = if compiler. stage == 0 {
1125- host_dir. join ( "stage0-sysroot" )
1126- } else if builder. download_rustc ( ) {
1127- host_dir. join ( "ci-rustc-sysroot" )
1128- } else {
1129- host_dir. join ( format ! ( "stage{}" , compiler. stage) )
1124+
1125+ let sysroot_dir = |stage| {
1126+ if stage == 0 {
1127+ host_dir. join ( "stage0-sysroot" )
1128+ } else if builder. download_rustc ( ) && compiler. stage != builder. top_stage {
1129+ host_dir. join ( "ci-rustc-sysroot" )
1130+ } else {
1131+ host_dir. join ( format ! ( "stage{}" , stage) )
1132+ }
11301133 } ;
1134+ let sysroot = sysroot_dir ( compiler. stage ) ;
1135+
11311136 let _ = fs:: remove_dir_all ( & sysroot) ;
11321137 t ! ( fs:: create_dir_all( & sysroot) ) ;
11331138
@@ -1138,9 +1143,15 @@ impl Step for Sysroot {
11381143 "Cross-compiling is not yet supported with `download-rustc`" ,
11391144 ) ;
11401145
1141- // #102002, cleanup stage1 and stage0-sysroot folders when using download-rustc so people don't use old versions of the toolchain by accident.
1142- let _ = fs:: remove_dir_all ( host_dir. join ( "stage1" ) ) ;
1143- let _ = fs:: remove_dir_all ( host_dir. join ( "stage0-sysroot" ) ) ;
1146+ // #102002, cleanup old toolchain folders when using download-rustc so people don't use them by accident.
1147+ for stage in 0 ..=2 {
1148+ if stage != compiler. stage {
1149+ let dir = sysroot_dir ( stage) ;
1150+ if !dir. ends_with ( "ci-rustc-sysroot" ) {
1151+ let _ = fs:: remove_dir_all ( dir) ;
1152+ }
1153+ }
1154+ }
11441155
11451156 // Copy the compiler into the correct sysroot.
11461157 let ci_rustc_dir =
0 commit comments