@@ -1122,13 +1122,18 @@ impl Step for Sysroot {
1122
1122
fn run ( self , builder : & Builder < ' _ > ) -> Interned < PathBuf > {
1123
1123
let compiler = self . compiler ;
1124
1124
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
+ }
1131
1134
} ;
1135
+ let sysroot = sysroot_dir ( compiler. stage ) ;
1136
+
1132
1137
let _ = fs:: remove_dir_all ( & sysroot) ;
1133
1138
t ! ( fs:: create_dir_all( & sysroot) ) ;
1134
1139
@@ -1139,9 +1144,15 @@ impl Step for Sysroot {
1139
1144
"Cross-compiling is not yet supported with `download-rustc`" ,
1140
1145
) ;
1141
1146
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
+ }
1145
1156
1146
1157
// Copy the compiler into the correct sysroot.
1147
1158
let ci_rustc_dir =
0 commit comments