dune icon indicating copy to clipboard operation
dune copied to clipboard

Dune 3 cannot delete files named `NUL` on Windows

Open MisterDA opened this issue 3 years ago • 6 comments

When there's a cycle in dependencies (I guess) or for some other reason a NUL file can be produced in the _build directory. Dune fails to remove it. It can be tricky to remove them: https://stackoverflow.com/questions/17883481/delete-a-file-named-nul-on-windows.

Error: Conflict between the following libraries:
- "lwt.unix" in _build/default/src/unix
- "lwt.unix" in
  "C:\\Users\\antonin\\.opam\\ocaml-variants.4.13.1+mingw64c\\lib\\lwt\\unix"
  -> required by library "lwttester" in _build/default/test
-> required by executable main in test/ppx/dune:2
-> required by _build/default/test/ppx/main.exe
-> required by %{exe:main.exe} at test/ppx/dune:10
-> required by alias test/ppx/runtest in test/ppx/dune:7
$ dune clean
Error: rmdir(_build\default\src\unix): Directory not empty
$ ls _build/default/src/unix
NUL

MisterDA avatar Feb 24 '22 15:02 MisterDA

I'd be interested to know how this is file created in the first place. Dune knows about NUL because it's how it expands %{null} if Sys.win32, and it also opens channels to/from this device when ignoring redirects. If dune created that file by mistake and we change that, I think that it would be a valid fix.

emillon avatar Mar 24 '22 16:03 emillon

Could you submit a cram test that demonstrates the problem?

rgrinberg avatar Apr 22 '22 18:04 rgrinberg

Here is a repro:

Reproduction case for #5485

  $ cat > dune-project <<EOF
  > (lang dune 3.19)
  > EOF

  $ cat > dune <<EOF
  > (rule
  >  (deps
  >   (sandbox always))
  >  (action
  >   (write-file NUL "")))
  > EOF

  $ dune build
  Error:
  rename(_build/.sandbox/0122f98a4ae22887117b25895c059f16/default/NUL): File exists
  -> required by _build/default/NUL
  -> required by alias all
  -> required by alias default
  [1]

  $ dune clean

I guess we should special case NUL on windows and forbid it.

Note that %{null} expands to $pwd\nul (not NUL) and Dune correctly forbids this as falling "outside" of the current directory.

Alizter avatar Jun 11 '25 23:06 Alizter

I'm unable to tell if dune produced the NUL file to begin with however. I wasn't able to reproduce the original report.

Alizter avatar Jun 11 '25 23:06 Alizter

I guess we should special case NUL on windows and forbid it.

There's actually a few forbidden names. See https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions.

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, COM¹, COM², COM³, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, LPT¹, LPT², and LPT³. Also avoid these names followed immediately by an extension; for example, NUL.txt and NUL.tar.gz are both equivalent to NUL.

...but that may be unrelated to the problem at hand.

MisterDA avatar Jun 12 '25 08:06 MisterDA

Also, somehow the Cygwin shell has no issue whatsoever creating or deleting files named NUL.

nojb avatar Jun 12 '25 08:06 nojb