Skip to content

on Windows, copyFile makes empty source and destination files when the source file is absent #177

@hsenag

Description

@hsenag

on Windows,

copyFile nonexistent shouldnotexist

succeeds and leaves 0-byte files for both nonexistent and shouldnotexist.

I would expect it to throw an exception and not create either source or destination file, which is what happens on Linux and MacOS.

The behaviour seems to have started in directory-1.3.8.0 and I bisected it to 78b3e59. I'm fairly sure it's directory because I did things
like holding Win32 and trying with a range of GHCs. With default versions we only noticed it starting with GHC 9.6, but I get the same
behaviour with GHC 9.4.8+Win32 2.13.3.0+directory 1.3.8.0 (for example).

I used this test program to track down where the problem started:

module Main where

import Control.Exception
import System.Directory
import System.IO.Error (isDoesNotExistError)

main :: IO ()
main = do
  removeFile "nonexistent.source" `catch` (\e -> if isDoesNotExistError e then return () else throwIO e)
  removeFile "shouldnotexist.target" `catch` (\e -> if isDoesNotExistError e then return () else throwIO e)
  (copyFile "nonexistent.source" "shouldnotexist.target" >> putStrLn "BAD: copyFile did not throw") `catch` (\e -> if isDoesNotExistError e then putStrLn "OK: copyFile threw" else throwIO e)
  doesFileExist "nonexistent.source" >>= \b -> if b then putStrLn "BAD: nonexistent.source exists" else putStrLn "OK: nonexistent.source does not exist"
  doesFileExist "shouldnotexist.target" >>= \b -> if b then putStrLn "BAD: shouldnotexist.target exists" else putStrLn "OK: shouldnotexist.target does not exist"
  removeFile "nonexistent.source" `catch` (\e -> if isDoesNotExistError e then return () else throwIO e)
  removeFile "shouldnotexist.target" `catch` (\e -> if isDoesNotExistError e then return () else throwIO e)
  return ()

ref: https://bugs.darcs.net/issue2721

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions