Skip to content

Commit e065d06

Browse files
committed
rio's withBinaryFileAtomic doesn't work on wsl1, restore the old code
:( uhhh
1 parent 1a96e30 commit e065d06

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Stack/Prelude.hs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Stack.Prelude
2424
, module X
2525
) where
2626

27+
import Conduit
2728
import RIO as X
2829
import RIO.File as X hiding (writeBinaryFileAtomic)
2930
import Data.Conduit as X (ConduitM, runConduit, (.|))
@@ -44,6 +45,8 @@ import RIO.Process (HasProcessContext (..), ProcessContext, setStdin,
4445

4546
import qualified Data.Text.IO as T
4647
import qualified RIO.Text as T
48+
import System.Permissions (osIsWindows)
49+
4750

4851
-- | Path version
4952
withSystemTempDir :: MonadUnliftIO m => String -> (Path Abs Dir -> m a) -> m a
@@ -211,7 +214,21 @@ defaultFirstFalse :: (a -> FirstFalse) -> Bool
211214
defaultFirstFalse _ = False
212215

213216
-- | Write a @Builder@ to a file and atomically rename.
217+
--
218+
-- In the future: replace with a function in rio
214219
writeBinaryFileAtomic :: MonadIO m => Path absrel File -> Builder -> m ()
215-
writeBinaryFileAtomic fp builder =
216-
liftIO $
217-
withBinaryFileAtomic (toFilePath fp) WriteMode (`hPutBuilder` builder)
220+
writeBinaryFileAtomic fp builder
221+
-- Atomic file writing is not supported on Windows yet, unfortunately.
222+
-- withSinkFileCautious needs to be implemented properly for Windows to make
223+
-- this work.
224+
| osIsWindows =
225+
liftIO $
226+
withBinaryFile (toFilePath fp) WriteMode $ \h ->
227+
hPutBuilder h builder
228+
| otherwise =
229+
liftIO $
230+
withSinkFileCautious (toFilePath fp) $ \sink ->
231+
runConduit $
232+
yield builder .|
233+
unsafeBuilderToByteString .|
234+
sink

0 commit comments

Comments
 (0)