Skip to content

Commit 14bfecb

Browse files
Refactor Windows symlink removal logic in lib.rs
- Updated the symlink removal process on Windows to first attempt removing directory symlinks with `fs::remove_dir`, and fallback to `fs::remove_file` for file symlinks. This change improves error handling and clarity in the code.
1 parent 2c4af24 commit 14bfecb

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/bootstrap/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,16 @@ impl Build {
542542
if host.is_symlink() {
543543
// Left over from a previous build; overwrite it.
544544
// This matters if `build.build` has changed between invocations.
545-
// On Windows, `fs::remove_file` can remove both file and directory symlinks/junctions,
546-
// even when the target is invalid (which causes error 267 with `fs::remove_dir`).
547-
t!(fs::remove_file(&host));
545+
// Try remove_dir first (for directory symlinks), then remove_file (for file symlinks).
546+
// On Windows, there are two types of symlinks: directory and file symlinks.
547+
// remove_dir only works on directory symlinks, remove_file only works on file symlinks.
548+
if let Err(e) = fs::remove_dir(&host) {
549+
if e.kind() == io::ErrorKind::NotADirectory {
550+
t!(fs::remove_file(&host));
551+
} else {
552+
panic!("failed to remove symlink: {}", e);
553+
}
554+
}
548555
}
549556
t!(
550557
symlink_dir(&build.config, &build_triple, &host),

0 commit comments

Comments
 (0)