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