Cross-platform null handle OR ignoring a process' output

Viewed 124

This seems pretty straightforward a problem: if on a Unix system I want to discard output of a process (from the process library), the following comment (original Haddock link) seems relevant:

... If you wish to ignore the child process's output you should either create a pipe and drain it manually or pass a Handle that writes to /dev/null.

And it works fine. The problem I have is that openFile "/dev/null" WriteMode does not work properly on Windows - it creates an actual file called /dev/null.

Is there some cross-platform way of getting a Handle that just ignores its input? Other SO questions have me thinking one of openFile "nul" WriteMode or openFile "null" WriteMode should work, but neither seem to properly ignore output (see this line 462 of this log for an example of the former not working).

1 Answers

From File paths under Windows in the GHC manual:

Since GHC 8.6.1, the Haskell I/O manager automatically promotes paths in the legacy format to Win32 file namespace. By default the I/O manager will do two things to your paths:

  • replace \ with \\
  • expand relative paths to absolute paths

If you want to opt out of all preprocessing just expliticly use namespaces in your paths. Due to this change, if you need to open raw devices (e.g. COM ports) you need to use the device namespace explicitly. (e.g. \\.\COM1). GHC and Haskell programs in general no longer support opening devices in the legacy format.

Thus, in GHC 8.6.1 and later, instead of using NUL, you now need to use \\.\NUL, like silently had to change.

Related