Repository navigation
Description
Spec: std.fs.remove_tree is "Remove a file or directory tree recursively" (lib/std/fs.w:43; listed under
fs in docs/spec/stdlib/libstd-spec.md). A directory junction or symlink inside the tree is an entry of that tree;
the directory it points to is not. The POSIX backends already behave this way. rt_remove_tree
(rt/linux_x86_64.w:501) takes lstat, and it unlinks a symlink without descending into it.
On Windows, win_remove_tree_impl (rt/windows_x86_64.w:648, rt/windows_aarch64.w:551) starts with
win_is_dir(path). That is true for a directory junction or a directory symlink, so the walk lists the link's
target and deletes everything in it, then removes the link. Nothing in the Windows runtime checks
FILE_ATTRIBUTE_REPARSE_POINT. The repro below uses a junction; a directory symlink takes the same path through
the code, but it wasn't run.
Repro (NTFS; making a junction needs no admin rights):
New-Item -ItemType Directory target, victim\sub | Out-Null
Set-Content target\keep.txt 'must survive'
New-Item -ItemType Junction -Path victim\link -Target (Resolve-Path target) | Out-Nulluse std.fs
fn main:
let rc = remove_tree("victim")
print(f"remove_tree rc {rc}")
Expected: remove_tree rc 0. victim is gone, and target\keep.txt still exists: only the link was removed.
Actual: remove_tree rc 0. victim is gone, and target\keep.txt has been deleted; target is left empty.
Reproduced on windows-x86_64, native, with a compiler built from 334739d2. win_remove_tree_impl is unchanged on
current main (5b6830b1).
Why it matters: junctions are common on Windows, used for relocated data folders, tool caches and SDK links. One
remove_tree of an output folder that happens to hold a junction silently empties whatever the junction points at.
With's build layer itself calls remove_tree on output folders and repo copies (build.w, build/compiler.w,
build/emit_c.w, build/corpus.w).
Workaround: none from With code. std.fs can't tell a junction from a directory, so a program can't remove the
links itself before calling remove_tree.
Possible fix: in the walk, read dwFileAttributes from the WIN32_FIND_DATAW that FindFirstFileW and
FindNextFileW already return. For an entry with FILE_ATTRIBUTE_REPARSE_POINT, remove the link itself
(RemoveDirectoryW for a directory link, DeleteFileW for a file link) and don't descend. Check the top-level path
the same way.
Related: #800 skips test/behavior/behav_fs_remove_tree.w on Windows because making a symlink there needs
privilege. That test's link is a file symlink pointing inside the tree, so it wouldn't catch this case anyway.
Making a junction needs no privilege, so a Windows test can cover this case.
Found while fixing a separate Windows runtime text bug, which will be filed on its own.
Activity
rt: remove_tree on Windows removes a junction or symlink itself; a li…