Repository navigation
rt: remove_tree on Windows removes a junction or symlink itself; a link in a tree no longer empties its target - #1863
Conversation
…nk in a tree no longer empties its target win_remove_tree_w (rt/windows_x86_64.w, windows_aarch64.w) classified each path with win_is_dir, that is GetFileAttributesW, which reports a directory junction or directory symlink as a directory (a junction reads 0x410: FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT). So the walk listed the link's target, deleted everything in it, then removed the link, and returned 0. Removing a folder that held a junction emptied the folder the junction pointed at, outside the tree; remove_tree on a junction itself emptied its target. Nothing in the Windows runtime read FILE_ATTRIBUTE_REPARSE_POINT. The POSIX walks lstat, and unlink a symlink without entering it. The walk now classifies each child from the WIN32_FIND_DATAW that FindFirstFileW and FindNextFileW already return: dwFileAttributes, and dwReserved0, which holds a reparse point's tag. A link is a reparse point whose tag is a name surrogate (winnt.h IsReparseTagNameSurrogate, bit 29: a junction's IO_REPARSE_TAG_MOUNT_POINT 0xA0000003, a symlink's 0xA000000C), the line Rust's FileType::is_symlink draws on Windows. A directory link is removed with RemoveDirectoryW and a file link with DeleteFileW, each of which removes the link and not its target, and neither is entered. Other reparse points, such as cloud file placeholders, are walked as before. The path remove_tree is given is classified the same way: GetFileAttributesW reports a link itself however the path ends, and a reparse point's tag comes from its own FindFirstFileW entry, asked for without trailing separators, since that call fails on one. No foreign call is added, so no runtime-domain row changes. The three functions touched lose the pointer casts they did not need. test/behavior/behav_fs_remove_tree_windows_junctions.w (only-on: windows) makes junctions with cmd /c mklink /J, which needs no privilege, under TEMP: a junction needs NTFS, and a checkout may be on another file system. It covers a junction inside the removed tree, remove_tree on a junction (with and without a trailing separator), and a junction two levels down whose target holds a junction of its own. On 355c20b it fails at line 56: remove_tree returned 0 and the target's keep.txt is gone. copy_tree and list_files_text still follow junctions; this change does not touch them. Fixes withlang-dev#1798
|
Verified on win10 (2026-09-29): on 591d9c5 |
ehartford
left a comment
There was a problem hiding this comment.
Reviewed on a worktree at the PR head (Mac) and a fresh clone on native Windows (win10).
Data loss demonstrated and fixed. Fixture: target\keep.txt, mklink /J victim\link target, then remove_tree("victim").
- Before (seed bd6e00d = current main): rc 0, victim gone, target\keep.txt gone, target listing empty. PR test: panic
called unwrap on Err: Os(2, …/keep/keep.txt). - After (PR merged onto main, stage1): rc 0, victim gone, keep.txt survives. PR test prints ok.
Detection is right. win_is_link requires FILE_ATTRIBUTE_REPARSE_POINT and the name-surrogate bit, so junctions (0xA0000003), symlinks (0xA000000C) and WSL links (0xA000001D) are links while dedup/cloud placeholders are still walked — the line Rust draws. Children are classified from the FindFirstFile entry the walk already holds (dwFileAttributes @0, dwReserved0 @36, cFileName @44 — offsets verified), never via a second GetFileAttributesW. Dir link → RemoveDirectoryW, file link → DeleteFileW, neither entered; a top-level mount point is unmounted, not emptied; both attribute-query failures return win_neg_error() (no silent fallback). No new foreign call: llvm-nm --undefined-only of the runtime object is identical before/after. windows_aarch64.w carries the same code and cross-compiles.
POSIX parity: darwin/linux walk with lstat and never follow — equivalent for in-tree and bare-top cases (verified on the Mac).
Gate on the Mac: green (fixed list, 239 s), driven by the PR's pinned seed.
Non-blocking: test line 46 let _clean = remove_tree(base) — bare call is the house style. File-symlink branch unexercised (privilege), as you note.
Process: the PR is 42 commits behind main and the body's 'neither Windows runtime file has changed' is stale (817a071 etc. touched both); the merge is clean and the remove_tree region unchanged. Its pinned Windows seed (2e8defa) can no longer drive the tree on win10, so the Windows evidence above is on PR+main. Rebase before the stack's battery.
Filing separately: (1) POSIX remove_tree("link/") with a trailing slash empties the target on macOS (verified on main) — Windows is now stricter; (2) copy_tree/list_files_text on Windows still follow junctions; (3) Windows panic locations mangle backslashes in paths.
…op path: remove_tree("link/") removed the link's target (#1951)
lstat("link/") resolves the link, so rt_remove_tree saw the target as a
directory, emptied it, and rmdir("link/") removed the target itself; the
link was left dangling and keep.txt was gone (rt/darwin_aarch64.w:539,
and the same lines in rt/linux_x86_64.w and rt/linux_aarch64.w). The entry
now copies the path without its trailing separators and walks that; a
child path never ends in one (rt_path_join). Windows already strips before
its reparse-point query (#1863).
test/behavior/behav_1951_remove_tree_symlink_trailing_slash.w: a symlink
to a directory removed with one and two trailing slashes, a tree holding a
symlink removed with a trailing slash, and a plain directory with one; the
target's file survives each. Red on main's compiler (the target directory
was removed), green on the rebuilt stage1.
…ymlink as the link: neither walks into its target (#1952) win_copy_tree_w and win_list_files_walk classified each entry with GetFileAttributesW (rt/windows_x86_64.w win_is_dir), which calls a junction a directory, so list_files_text listed the junction's target and copy_tree copied it, from outside the tree. #1863 fixed remove_tree the same way; the three walkers now share it: win_top_entry classifies the top path (the reparse tag from its own FindFirstFileW entry, trailing separators left off), and each child is classified by its listing entry with win_is_link (reparse point + name-surrogate bit). list_files_text lists a link as a leaf, the link itself, as the lstat walk lists a symlink on POSIX. copy_tree copies a file link as the file it names (POSIX open() follows one) and refuses a directory link with -21 (EISDIR), as the POSIX copy fails on a directory symlink (verified on the Mac: copy_tree over a tree holding a dir symlink returns -21). Same change in rt/windows_aarch64.w; the two files' walker code is identical. test/behavior/behav_1952_windows_walkers_junctions.w (Windows), mirroring behav_fs_remove_tree_windows_junctions.w. On win10 with the pinned seed (nightly-20260930-local-2-bd6e00d1dee6): fails at line 58, the listing holds the junction's target. With this branch's stage1 (:dev on win10): passes, as do behav_fs_remove_tree_windows_junctions, behav_fs_list_files_file_path and behav_fs_windows_unicode_names.
…op path: remove_tree("link/") removed the link's target (#1951)
lstat("link/") resolves the link, so rt_remove_tree saw the target as a
directory, emptied it, and rmdir("link/") removed the target itself; the
link was left dangling and keep.txt was gone (rt/darwin_aarch64.w:539,
and the same lines in rt/linux_x86_64.w and rt/linux_aarch64.w). The entry
now copies the path without its trailing separators and walks that; a
child path never ends in one (rt_path_join). Windows already strips before
its reparse-point query (#1863).
test/behavior/behav_1951_remove_tree_symlink_trailing_slash.w: a symlink
to a directory removed with one and two trailing slashes, a tree holding a
symlink removed with a trailing slash, and a plain directory with one; the
target's file survives each. Red on main's compiler (the target directory
was removed), green on the rebuilt stage1.
…ymlink as the link: neither walks into its target (#1952) win_copy_tree_w and win_list_files_walk classified each entry with GetFileAttributesW (rt/windows_x86_64.w win_is_dir), which calls a junction a directory, so list_files_text listed the junction's target and copy_tree copied it, from outside the tree. #1863 fixed remove_tree the same way; the three walkers now share it: win_top_entry classifies the top path (the reparse tag from its own FindFirstFileW entry, trailing separators left off), and each child is classified by its listing entry with win_is_link (reparse point + name-surrogate bit). list_files_text lists a link as a leaf, the link itself, as the lstat walk lists a symlink on POSIX. copy_tree copies a file link as the file it names (POSIX open() follows one) and refuses a directory link with -21 (EISDIR), as the POSIX copy fails on a directory symlink (verified on the Mac: copy_tree over a tree holding a dir symlink returns -21). Same change in rt/windows_aarch64.w; the two files' walker code is identical. test/behavior/behav_1952_windows_walkers_junctions.w (Windows), mirroring behav_fs_remove_tree_windows_junctions.w. On win10 with the pinned seed (nightly-20260930-local-2-bd6e00d1dee6): fails at line 58, the listing holds the junction's target. With this branch's stage1 (:dev on win10): passes, as do behav_fs_remove_tree_windows_junctions, behav_fs_list_files_file_path and behav_fs_windows_unicode_names.
… args refused in Sema (#1954); raw source locations (#1953); in-root wo store (#1885); hex of narrow signed ints (#1907); HOME-less build (#1884); Windows copy_tree/list_files_text don't follow junctions (#1952) (#1978) * POSIX remove_tree strips trailing separators before classifying the top path: remove_tree("link/") removed the link's target (#1951) lstat("link/") resolves the link, so rt_remove_tree saw the target as a directory, emptied it, and rmdir("link/") removed the target itself; the link was left dangling and keep.txt was gone (rt/darwin_aarch64.w:539, and the same lines in rt/linux_x86_64.w and rt/linux_aarch64.w). The entry now copies the path without its trailing separators and walks that; a child path never ends in one (rt_path_join). Windows already strips before its reparse-point query (#1863). test/behavior/behav_1951_remove_tree_symlink_trailing_slash.w: a symlink to a directory removed with one and two trailing slashes, a tree holding a symlink removed with a trailing slash, and a plain directory with one; the target's file survives each. Red on main's compiler (the target directory was removed), green on the rebuilt stage1. * Sema refuses Vec.range with anything but one range argument: v.range(0, 1) crashed the compiler at build (#1954) The issue's repro is `v.range(0, 1)`: two integers where the method takes one range. Sema typed the call (check was silent), and the VEC_RANGE lowering read the range's element type off the first argument (src/CodegenDispatch.w:10470 wl_struct_get_type_at on an i64; lldb frame #1 under LLVMStructGetTypeAtIndex, EXC_BAD_ACCESS at 0x0). `len()` and `is_empty()` on a VecRange from `v.range(start..end)` build and run on main; the crash was in the misspelled constructor, not in len. check_method_call_parts now checks `range` beside split_at: exactly one argument, of range type, else a diagnostic naming the spelling. test/compile_errors/err_1954_vec_range_two_args.w and err_1954_vec_range_int_arg.w pin the two diagnostics (check passed both on main); test/behavior/behav_1954_vecrange_len_is_empty.w pins len() and is_empty() over a VecRange. * A compiler-synthesized string constant is raw: a panic location no longer reads the path's backslashes as escapes (#1953) MirBuilder.source_location_operand (src/MirLower.w:3092) interned `path:line:col` as a plain CK_STR, and every consumer decodes a plain CK_STR as a source literal (CodegenDispatch str_const_text -> decode_string_escapes; cc_string_literal_payload; comptime), so `W:\review-1863\test\behavior\x.w` printed as `W:` CR `eview-1863` TAB `estbehaviorx.w`. lower_synthesized_str_lit interns compiler-made text with the raw-literal prefix the Parser gives a raw literal, which all three decoders already honor; the location, `__FILE__` and the function name go through it. test/behavior/behav_1953_panic_location_backslash_path.w (Windows): runs the compiler on a program under TEMP\...\t\r\b\boom.w and expects its `__FILE__` and its panic location byte for byte. * An absolute install destination beneath the project root is contained, not an escape (#1885) build_graph_path_project_contained refused every absolute path (src/BuildGraphSupport.w:151), so WITH_WO_DIR spelled as an absolute in-root path (`C:/dev/wt/out/with-wo`) failed the wo install targets with "output escapes project root". It now takes the root: an absolute path that is the root or lies beneath it (separators normalized; case folded on Windows) is checked as the relative path it names, anything else is still refused. build_graph_validate_target_containment takes the root from the dispatcher. The issue's second half, the old store path persisting after unsetting WITH_WO_DIR, is #1925's graph-cache env fingerprint, which landed after the issue's main (355c20b): a removed variable hashes as empty and re-plans. The fixture pins both: an absolute in-root store installs (red on main's compiler with the escape message), the variable removed re-plans to the default store, and an absolute store outside the project is still refused. * A narrow integer under a hex, binary or octal spec prints its own bits: i8 -2 under :x is fe (#1907) The issue's u8 cases were fixed by #1922 (Sema's signedness reaches the formatter, so an unsigned value zero-extends). A narrow signed value was still sign-extended to 64 bits before with_fmt_int_spec (rt/rt_core.w) formatted it, so `i8` -2 printed `fffffffffffffffe` under :x, :b, :o, :X and :#x. f-strings reach the formatter through FMT_BUF_WRITE_FMT (with_fmt_buf_write_i64_spec), a format call through FMT_SPEC; both now pass Sema's bit width of the value in flags bits 24-31 (CodegenDispatch mir_int_spec_flags; CCodegen int_spec_flags_text for the C backend), and with_fmt_int_spec masks a bit-pattern base to that width. Decimal is unchanged: a negative decimal still prints '-' and its magnitude. test/behavior/behav_1907_hex_spec_own_width.w: the issue's u8 lines, u16 and u32, and i8/i16/i32/i64 -2 under x, 02x, #x, b, o and X. Red on the first stage1 of this branch (fffffffffffffffe for i8), green after. --emit-c passes the same width bits. * One home resolver for the build's stores, and a loud stop when there is no home (#1884) The issue's case, Windows without HOME, became the USERPROFILE fallback in build/wo.w wo_home with #1937, after the issue's main. Two parts were left. With no home at all (HOME unset, and USERPROFILE too on Windows) the store still became `/.local/with-wo` and the build failed late, after stage1 and stage2, as an install escaping the project root: on main, `HOME= with build --graph` exits 0 and plans every wo artifact under /.local/with-wo. And the green store (build.w green_store_install_path, build/retention.w ret_green_store_path), `:install-user` (src/main.w) and the driver's test-verdict and build-runner stores read HOME alone. build/compiler.w comp_home_dir is now the one rule for the build layer: $HOME, else %USERPROFILE% on Windows, else Diagnostics.error naming the variables and the overrides (WITH_WO_DIR, WITH_GREEN_DIR) at graph time; comp_home_lookup gives "" for a caller that only asks whether an explicit store lies beneath it, and comp_home_dir_env is the action-time reading. The driver's stores and :install-user use build_graph_home_dir (the same rule), and :install-user refuses with no home. test/behavior/behav_1884_build_layer_no_home.w evaluates this repo's graph with no home and expects the refusal (rc 0 with a /.local/with-wo plan on main). test/behavior/behav_1884_windows_install_home_unset.w (Windows) pins the fallback: a `$HOME/` install with HOME emptied lands under %USERPROFILE%. * Windows copy_tree and list_files_text treat a junction or directory symlink as the link: neither walks into its target (#1952) win_copy_tree_w and win_list_files_walk classified each entry with GetFileAttributesW (rt/windows_x86_64.w win_is_dir), which calls a junction a directory, so list_files_text listed the junction's target and copy_tree copied it, from outside the tree. #1863 fixed remove_tree the same way; the three walkers now share it: win_top_entry classifies the top path (the reparse tag from its own FindFirstFileW entry, trailing separators left off), and each child is classified by its listing entry with win_is_link (reparse point + name-surrogate bit). list_files_text lists a link as a leaf, the link itself, as the lstat walk lists a symlink on POSIX. copy_tree copies a file link as the file it names (POSIX open() follows one) and refuses a directory link with -21 (EISDIR), as the POSIX copy fails on a directory symlink (verified on the Mac: copy_tree over a tree holding a dir symlink returns -21). Same change in rt/windows_aarch64.w; the two files' walker code is identical. test/behavior/behav_1952_windows_walkers_junctions.w (Windows), mirroring behav_fs_remove_tree_windows_junctions.w. On win10 with the pinned seed (nightly-20260930-local-2-bd6e00d1dee6): fails at line 58, the listing holds the junction's target. With this branch's stage1 (:dev on win10): passes, as do behav_fs_remove_tree_windows_junctions, behav_fs_list_files_file_path and behav_fs_windows_unicode_names. * p7_abs takes a drive-letter path as absolute: a Windows case directory was joined onto the root twice (#1885) pre_d_build_runner's p7_abs treated only a leading '/' as absolute, so on Windows p7_abs(p7_join(case_dir, ...)) spelled W:\root/W:\root/out/... and behav_1885_in_root_store_and_env_replan.w failed on win10 with that path in the escape message. A leading '/' or '\', or a drive letter and a separator, is absolute. With it the #1885 fixture passes on win10 with the branch stage1, the issue's case: an absolute drive-letter store inside the project root. * path_absolute_test: build_graph_path_project_contained takes the project root (#1885) 456f18f0 made containment relative to the root: an absolute path beneath it is contained, so the predicate is now (root, path). The three cases keep their intent under root `/root` (C:/elsewhere/x.w and /elsewhere/x.w escape, src/x.w is contained), and the case #1885 made legal is added: /root/out/x.w is contained. Test: the previous file no longer compiles on stage1 ("expects 2 argument(s), found 1", the battery red); this one prints ok. --------- Co-authored-by: Eric Hartford <[email protected]>
Fixes #1798.
Seed-driven battery on this commit (
with run tools/battery.w, native Windows x86_64, the pinned seednightly-20260926-local-1-2e8defaa18efwith thenightly-20260907SDK; see the SDK note below)::battery-checksis green exceptnative-spec-tests,contract-view-testsanduat-tests, plustestandtest-with-audits, which fail only because they depend on those;:fixpoint, the drop and move audits,behavior-tests,native-compile-error-testsanduser-programs-safe.The three reds reproduce on the parent
355c20b1with the same seed, SDK and host. None comes from this change:355c20b1and on this commitnative-spec-testsspec_ss14_11_await_combinator_cancel_joins.wcarriesknown-issue: #916, but it passes on windows-x86_64: "expected this test to stay red, but it passed" (it's already skipped on linux-aarch64 for the same reason)contract-view-tests.expectedfiles differ only by CRLF: the host's Git for Windows hascore.autocrlf=true, and.gitattributespins LF only fortest/behaviorfixtures (#801). The outputs are byte-identical on both commitsuat-testsverbsscenario's steprun: ./out/bin/tmpexits -2 (no.exe), andskip_platformprintswindowswhere the expected listing has<host>. The reports are identical on both commits apart from one profile timing lineIt stays a draft until you're satisfied with that.
The branch is on
355c20b1. It merges cleanly onto current main4a940563, where neither Windows runtimefile has changed.
The bug
win_remove_tree_w(rt/windows_x86_64.w,rt/windows_aarch64.w) classified each path withwin_is_dir, which isGetFileAttributesW. That call reports a directory junction or a directory symlink as a directory (a junction reads0x410:FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT). So the walk listed the link's target, deletedeverything in it, removed the link, and returned 0. Removing a folder that held a junction emptied the folder the
junction pointed at, outside the tree.
remove_treeon a junction itself emptied its target. The POSIX walkslstat,and unlink a symlink without entering it.
The fix
WIN32_FIND_DATAWthatFindFirstFileW/FindNextFileWalready return:dwFileAttributes, anddwReserved0, which holds a reparse point's tag.IsReparseTagNameSurrogate, bit 29 of the tag): ajunction (
IO_REPARSE_TAG_MOUNT_POINT,0xA0000003) or a symlink (0xA000000C). This is the line Rust'sFileType::is_symlinkdraws on Windows.RemoveDirectoryW, a file link withDeleteFileW. Each removes the link, notits target. Neither is entered.
remove_treeis given is classified the same way.GetFileAttributesWreports a link itself, howeverthe path ends. A reparse point's tag comes from its own
FindFirstFileWentry, asked for without trailingseparators, because that call fails on one.
Test
test/behavior/behav_fs_remove_tree_windows_junctions.w(only-on: windows, nounsafe):cmd /c mklink /J, which needs no privilege (a symlink needs Developer Mode orelevation, Windows: action/capability/net/process/fs behavior tests fail #800).
TEMP, because a junction needs NTFS and a checkout may be on another file system. The folder namecarries the pid, as
test/internals/conan_http_temp_test.wdoes.remove_treeon a junction, with and without a trailing separator;On
355c20b1it fails at line 56:remove_treereturned 0, and the target'skeep.txtis gone. With the fix itpasses.
The issue's repro, and each case run on its own, show the same thing:
355c20b1target\keep.txtremove_tree(junction)remove_tree(junction + "\")Existing tests that use
remove_treeorcopy_treepass:behav_fs_windows_unicode_names,behav_fs_list_files_file_path,behav_build_cache_install_paths,behav_build_install_atomic_verify,behav_std_build_api, andtest/internals/conan_http_temp_test.behav_fs_remove_treeskips on Windows by its owndirective (#800).
src/main build :gateon this commit: all targets green (native Windows x86_64, the fixed list; this worktree hadno battery measurements). The aarch64 backend cross-compiles to COFF-ARM64, and neither backend's object imports
anything new. The gate linked against the
nightly-20260907LLVM SDK: the pinned seed release's own SDK was built withVS 2026 Build Tools, and linking stage1 against it with VS 2022 17.14 (MSVC 14.44) fails on 14 MSVC STL helpers
(
__std_min_element_4u,__std_unique_8,__std_rotate, ...).Not in this change
copy_treeandlist_files_textstill follow junctions. A copy holds the target's files as a real directory,and a listing lists them. A junction cycle stops at the path-length limit rather than looping: the listing stops at
26 lines, and
copy_treefails with -206 (ERROR_FILENAME_EXCED_RANGE) after 23 nested directories.elevation to create, so the test doesn't make one.
followed (CVE-2022-21658 class). Rust closed this with handle-relative opens (
NtOpenFilewithOBJ_DONT_REPARSE).