Skip to content

Commit 0c5f471

Browse files
authored
Merge pull request #11938 from haskell/more-robust-cmdclean
Make 'cabal clean' more robust: retry after getting 'Unsatisfied constraints (directory is non empty)' error
2 parents 4960764 + 79e93c6 commit 0c5f471

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

Cabal-tests/lib/Test/Utils/TempTestDir.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ removeDirectoryRecursiveHack :: Verbosity -> FilePath -> IO ()
6060
removeDirectoryRecursiveHack verbosity dir | isWindows = go 1
6161
where
6262
isWindows = System.Info.os == "mingw32"
63-
limit = 3
63+
limit = 5
6464

6565
go :: Int -> IO ()
6666
go n = do

cabal-install/src/Distribution/Client/CmdClean.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import Control.Monad
8181
, mapM
8282
)
8383
import qualified Data.Set as Set
84+
import qualified GHC.IO.Exception as GHC
8485
import System.Directory
8586
( canonicalizePath
8687
, doesDirectoryExist
@@ -92,7 +93,8 @@ import System.FilePath
9293
( (</>)
9394
)
9495
import System.IO.Error
95-
( isPermissionError
96+
( ioeGetErrorType
97+
, isPermissionError
9698
)
9799
import qualified System.Process as Process
98100

@@ -193,7 +195,14 @@ cleanAction (ProjectFlags{..}, CleanFlags{..}) extraArgs _ = do
193195
"attrib -s -h -r " <> distRoot <> "\\*.* /s /d"
194196
catch
195197
(removePathForcibly distRoot)
196-
(\e -> if isPermissionError e then threadDelay 1000 >> removePathForcibly distRoot else throw e)
198+
( \e ->
199+
-- Permission error is usually when some files are (temporarily) locked.
200+
-- Unsatisfied constraints (directory is non empty) error happens
201+
-- when some files inside the directory were not removed (perhaps because they are locked).
202+
if isPermissionError e || ioeGetErrorType e == GHC.UnsatisfiedConstraints
203+
then threadDelay 1000 >> removePathForcibly distRoot
204+
else throw e
205+
)
197206

198207
removeEnvFiles $ distProjectRootDirectory distLayout
199208

changelog.d/pr-11604.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
synopsis: Replace removeDirectoryRecursive with removePathForcibly
33
packages: [Cabal, cabal-install]
4-
prs: 11604
4+
prs: 11604 11938
55
---
66

77
We replace `System.Directory.removeDirectoryRecursive` calls with a more robust `System.Directory.removePathForcibly`. Additionally, some functions (most notably the one responsible for `cabal clean`) now run `removeDirectoryRecursive` twice on all platforms if the first time was not successful (previously only on Windows) and include a small delay in between.

0 commit comments

Comments
 (0)