Skip to content

fs::Dir::remove_file and remove_dir remove the target of a symlink on Windows #164249

Closed

Description

I tried this code:

#![feature(dirfd)]
use std::fs::{self, Dir};
use std::os::windows::fs::{symlink_dir, symlink_file};

fn exists(path: &str) -> bool {
    fs::symlink_metadata(path).is_ok()
}

fn main() -> std::io::Result<()> {
    fs::create_dir("d")?;
    fs::write("d/target.txt", b"data")?;
    fs::create_dir("d/targetdir")?;
    symlink_file("target.txt", "d/filelink")?;
    symlink_dir("targetdir", "d/dirlink")?;
    let dir = Dir::open("d")?;

    println!("Dir::remove_file(filelink) = {:?}", dir.remove_file("filelink"));
    println!("  filelink exists: {}", exists("d/filelink"));
    println!("  target.txt exists: {}", exists("d/target.txt"));

    println!("Dir::remove_dir(dirlink) = {:?}", dir.remove_dir("dirlink"));
    println!("  dirlink exists: {}", exists("d/dirlink"));
    println!("  targetdir exists: {}", exists("d/targetdir"));

    // For comparison: the same on a fresh setup with the path-based functions.
    fs::create_dir("e")?;
    fs::write("e/target.txt", b"data")?;
    symlink_file("target.txt", "e/filelink")?;
    println!("fs::remove_file(filelink) = {:?}", fs::remove_file("e/filelink"));
    println!("  filelink exists: {}", exists("e/filelink"));
    println!("  target.txt exists: {}", exists("e/target.txt"));
    Ok(())
}

I expected to see this happen: the Dir methods remove the link and leave its target alone. That is what they do on Unix, and what fs::remove_file does on Windows (the third case below).

Instead, this happened: the link stays and its target is removed.

Meta

rustc --version --verbose:

rustc 1.101.0-nightly (e9bbadf68 2026-10-12)
host: x86_64-pc-windows-msvc
release: 1.101.0-nightly
Backtrace

<backtrace>

Activity

  1. added
    needs-triage
    on Oct 13, 2026
  2. rustbot commented on Oct 13, 2026

    @rustbot
    Collaborator

    Error: Only Rust team members can ping teams.

    Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

  3. added
    T-libs
    and removed
    needs-triage
    on Oct 22, 2026
  4. added 5 commits that reference this issue on Nov 10, 2026
  5. added a commit that references this issue on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugT-libs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions