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