[New-bugs-announce] [issue31226] shutil.rmtree fails when target has an internal directory junction (Windows)

Vidar Fauske report at bugs.python.org
Thu Aug 17 09:58:18 EDT 2017


New submission from Vidar Fauske:

On Windows (Windows 10 in my case), given the following directory structure:
- rootfolder
 - a
 - b
  - junc (directory junction to ../a)

a call to `shutil.rmtree('root')` will fail with an exception `FileNotFoundError: [WinError 3]`, in a call to `os.listdir()` in `_rmtree_unsafe`. See attached minimal working example.

Note that sorting order is important: A link in 'a' pointing to 'b' does not fail. This is because `os.listdir()` raises an exception for 'b/junc' when its target ('a') has already been deleted.

Also, note that this is only for junctions, not directory links (`mklink /J` vs `mklink /D`), because:
 - Directory links flag false in the `stat.S_ISDIR(os.lstat('b/junc').st_mode)` test while junctions do not.
 - `os.islink()` returns false for both junctions, while directory links do not.

Indicated Python versions are those which I have personally tested on, and observed this behavior.

Current use case: Deleting a folder tree generated by an external tool, which creates junction links as part of its normal operation ('lerna' tool for the 'npm' javascript package manager).

----------
components: IO, Library (Lib), Windows
files: mwe.py
messages: 300424
nosy: paul.moore, steve.dower, tim.golden, vidartf, zach.ware
priority: normal
severity: normal
status: open
title: shutil.rmtree fails when target has an internal directory junction (Windows)
versions: Python 3.3, Python 3.6
Added file: http://bugs.python.org/file47090/mwe.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31226>
_______________________________________


More information about the New-bugs-announce mailing list