Skip to content

Commit

Permalink
Put temp files in temp directory
Browse files Browse the repository at this point in the history
This helps with Windows not being capable of creating temp files in
long directories, like the ones that result from Backpack.

See how GetTempFileNameW specifies:

> The string cannot be longer than MAX_PATH–14 characters or GetTempFileName
will fail.

And actually there is a TODO in `Win32Utils.c` in GHC:

https://gitlab.haskell.org/ghc/ghc/-/blob/3939a8bf93e27d8151aa1d92bf3ce10bbbc96a72/libraries/ghc-internal/cbits/Win32Utils.c#L259

Closes haskell#10191.
  • Loading branch information
jasagredo committed Sep 16, 2024
1 parent e1b59b4 commit 7557f11
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
8 changes: 5 additions & 3 deletions Cabal-syntax/src/Distribution/Utils/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ import qualified Data.Set as Set

import qualified Control.Exception as Exception
import System.Directory
( removeFile
( getTemporaryDirectory
, removeFile
, renameFile
)
import System.FilePath
Expand Down Expand Up @@ -171,9 +172,10 @@ withFileContents name action =
-- This case will give an IO exception but the atomic property is not affected.
writeFileAtomic :: FilePath -> LBS.ByteString -> IO ()
writeFileAtomic targetPath content = do
let (targetDir, targetFile) = splitFileName targetPath
let (_, targetFile) = splitFileName targetPath
tmpDir <- getTemporaryDirectory
Exception.bracketOnError
(openBinaryTempFileWithDefaultPermissions targetDir $ targetFile <.> "tmp")
(openBinaryTempFileWithDefaultPermissions tmpDir $ targetFile <.> "tmp")
(\(tmpPath, handle) -> hClose handle >> removeFile tmpPath)
( \(tmpPath, handle) -> do
LBS.hPut handle content
Expand Down
16 changes: 9 additions & 7 deletions Cabal/src/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ import System.Directory
, getDirectoryContents
, getModificationTime
, getPermissions
, getTemporaryDirectory
, removeDirectoryRecursive
, removeFile
)
Expand Down Expand Up @@ -1768,23 +1769,24 @@ withTempFileEx
-- ^ File name template. See 'openTempFile'.
-> (SymbolicPath Pkg File -> Handle -> IO a)
-> IO a
withTempFileEx opts mbWorkDir tmpDir template action =
withTempFileEx opts _mbWorkDir _tmpDir template action = do
tmp <- getTemporaryDirectory
withFrozenCallStack $
Exception.bracket
(openTempFile (i tmpDir) template)
(openTempFile tmp template)
( \(name, handle) -> do
hClose handle
unless (optKeepTempFiles opts) $
handleDoesNotExist () $
removeFile $
name
)
(withLexicalCallStack (\(fn, h) -> action (mkRelToPkg fn) h))
(withLexicalCallStack (\(fn, h) -> action (mkRelToPkg tmp fn) h))
where
i = interpretSymbolicPath mbWorkDir -- See Note [Symbolic paths] in Distribution.Utils.Path
mkRelToPkg :: FilePath -> SymbolicPath Pkg File
mkRelToPkg fp =
tmpDir </> makeRelativePathEx (takeFileName fp)
-- i = interpretSymbolicPath mbWorkDir -- See Note [Symbolic paths] in Distribution.Utils.Path
mkRelToPkg :: FilePath -> FilePath -> SymbolicPath Pkg File
mkRelToPkg tmp fp =
makeSymbolicPath tmp </> makeRelativePathEx (takeFileName fp)

-- 'openTempFile' returns a path of the form @i tmpDir </> fn@, but we
-- want 'withTempFileEx' to return @tmpDir </> fn@. So we split off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Test.Cabal.Prelude

main = cabalTest $ do
skipUnlessGhcVersion ">= 8.1"
expectBrokenIfWindowsCI 10191 $ withProjectFile "cabal.internal.project" $ do
withProjectFile "cabal.internal.project" $ do
cabal "v2-build" ["exe"]
withPlan $ do
r <- runPlanExe' "I" "exe" []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import Test.Cabal.Prelude

main = cabalTest $ do
skipUnlessGhcVersion ">= 8.1"
ghcVer <- isGhcVersion ">= 9.10"
skipIf "Windows + 9.10.1 (#10191)" (isWindows && ghcVer)
withProjectFile "cabal.external.project" $ do
cabal "v2-build" ["exe"]
withPlan $ do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Test.Cabal.Prelude

main = cabalTest $ do
skipUnlessGhcVersion ">= 8.1"
expectBrokenIfWindowsCI 10191 $ withProjectFile "cabal.internal.project" $ do
withProjectFile "cabal.internal.project" $ do
cabal "v2-build" ["exe"]
withPlan $ do
r <- runPlanExe' "I" "exe" []
Expand Down
2 changes: 1 addition & 1 deletion cabal-testsuite/PackageTests/Backpack/T6385/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Test.Cabal.Prelude
main =
cabalTest $ expectBrokenIfWindows 10191 $ withShorterPathForNewBuildStore $ do
cabalTest $ withShorterPathForNewBuildStore $ do
skipUnlessGhcVersion ">= 8.1"
withRepo "repo" $ do
cabal "v2-build" ["T6385"]
10 changes: 5 additions & 5 deletions cabal-testsuite/PackageTests/HaddockKeepsTmps/cabal.test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import Distribution.Verbosity
import Distribution.Simple.Glob
import Distribution.Simple.Glob.Internal
import Distribution.Simple.Utils
import System.Directory (getTemporaryDirectory)

-- Test that "cabal haddock" preserves temporary files
-- We use haddock-keep-temp-file: True in the cabal.project.
main = cabalTest $ recordMode DoNotRecord $ withProjectFile "cabal.project" $ do
main = do
cwd <- getTemporaryDirectory
cabalTest $ recordMode DoNotRecord $ withProjectFile "cabal.project" $ do
cabal "haddock" []

cwd <- fmap testCurrentDir getTestEnv

-- Windows has multiple response files, and only the last one (alphabetically) is the important one.
(safeLast . sort . globMatches <$> liftIO (runDirFileGlob silent Nothing cwd (GlobDirRecursive [WildCard, Literal "txt"]))) >>= \case
(safeLast . sort . globMatches <$> liftIO (runDirFileGlob silent Nothing cwd (GlobDirRecursive [Literal "had", WildCard, Literal "txt"]))) >>= \case
Nothing -> error "Expecting a response file to exist"
Just m -> do
-- Assert the matched response file is not empty, and indeed a haddock rsp
Expand Down

0 comments on commit 7557f11

Please sign in to comment.