Dune 3 cannot delete files named `NUL` on Windows
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
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.
Could you submit a cram test that demonstrates the problem?
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.
I'm unable to tell if dune produced the NUL file to begin with however. I wasn't able to reproduce the original report.
I guess we should special case
NULon 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², andLPT³. Also avoid these names followed immediately by an extension; for example,NUL.txtandNUL.tar.gzare both equivalent toNUL.
...but that may be unrelated to the problem at hand.
Also, somehow the Cygwin shell has no issue whatsoever creating or deleting files named NUL.