Skip to content

Commit df57849

Browse files
author
Vladislav Sabanov
committed
Add explicit fail for cabal clean. Rename CleanAction to CleanActionNotScript
1 parent 9b26df8 commit df57849

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE PatternSynonyms #-}
33
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE LambdaCase #-}
45

56
module Distribution.Client.CmdClean (cleanCommand, cleanAction) where
67

@@ -13,6 +14,8 @@ import Distribution.Client.Config
1314
import Distribution.Client.DistDirLayout
1415
( DistDirLayout (..)
1516
, defaultDistDirLayout
17+
, ProjectRoot (ProjectRootImplicit)
18+
, defaultProjectFile
1619
)
1720
import Distribution.Client.Errors
1821
import Distribution.Client.ProjectConfig
@@ -161,14 +164,17 @@ cleanAction (ProjectFlags{..}, CleanFlags{..}) extraArgs _ = do
161164
notScripts <- filterM (fmap not . doesFileExist) extraArgs
162165
unless (null notScripts) $
163166
dieWithException verbosity $
164-
CleanAction notScripts
167+
CleanActionNotScript notScripts
165168

166169
projectRoot <- either throwIO return =<< findProjectRoot verbosity mprojectDir mprojectFile
167170

168171
let distLayout = defaultDistDirLayout projectRoot mdistDirectory Nothing
169172

170173
-- Do not clean a project if just running a script in it's directory
171174
when (null extraArgs || isJust mdistDirectory) $ do
175+
isValid <- isValidProjectRoot projectRoot
176+
unless isValid $ dieWithException verbosity CleanActionNotPackage
177+
172178
if saveConfig
173179
then do
174180
let buildRoot = distBuildRootDirectory distLayout
@@ -214,6 +220,16 @@ cleanAction (ProjectFlags{..}, CleanFlags{..}) extraArgs _ = do
214220
info verbosity ("Deleting cache (" ++ cache ++ ") for script (" ++ script ++ ")")
215221
removeDirectoryRecursive cache
216222

223+
isValidProjectRoot :: ProjectRoot -> IO Bool
224+
isValidProjectRoot = \case
225+
(ProjectRootImplicit dir) -> do
226+
let projectFile = dir </> defaultProjectFile
227+
projectExists <- doesFileExist projectFile
228+
contents <- listDirectory dir
229+
let cabalFiles = filter (".cabal" `isSuffixOf`) contents
230+
pure (projectExists || not (null cabalFiles))
231+
_ -> pure True
232+
217233
removeEnvFiles :: FilePath -> IO ()
218234
removeEnvFiles dir =
219235
(traverse_ (removeFile . (dir </>)) . filter ((".ghc.environment" ==) . take 16))

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ data CabalInstallException
9999
| UnknownExecutable String UnitId
100100
| MultipleMatchingExecutables String [String]
101101
| CmdRunReportTargetProblems String
102-
| CleanAction [String]
102+
| CleanActionNotScript [String]
103103
| ReportCannotPruneDependencies String
104104
| ReplCommandDoesn'tSupport
105105
| ReplTakesNoArguments [String]
@@ -192,6 +192,7 @@ data CabalInstallException
192192
| LegacyAndParsecParseResultsDiffer FilePath String String
193193
| CabalFileParseFailure CabalFileParseError
194194
| ProjectConfigParseFailure ProjectConfigParseError
195+
| CleanActionNotPackage
195196
deriving (Show)
196197

197198
exceptionCodeCabalInstall :: CabalInstallException -> Int
@@ -255,7 +256,7 @@ exceptionCodeCabalInstall e = case e of
255256
UnknownExecutable{} -> 7068
256257
MultipleMatchingExecutables{} -> 7069
257258
CmdRunReportTargetProblems{} -> 7070
258-
CleanAction{} -> 7071
259+
CleanActionNotScript{} -> 7071
259260
ReportCannotPruneDependencies{} -> 7072
260261
ReplCommandDoesn'tSupport{} -> 7073
261262
ReplTakesNoArguments{} -> 7074
@@ -348,6 +349,7 @@ exceptionCodeCabalInstall e = case e of
348349
LegacyAndParsecParseResultsDiffer{} -> 7165
349350
CabalFileParseFailure{} -> 7166
350351
ProjectConfigParseFailure{} -> 7167
352+
CleanActionNotPackage{} -> 7168
351353

352354
exceptionMessageCabalInstall :: CabalInstallException -> String
353355
exceptionMessageCabalInstall e = case e of
@@ -458,7 +460,7 @@ exceptionMessageCabalInstall e = case e of
458460
++ ":\n"
459461
++ unlines elabUnitId
460462
CmdRunReportTargetProblems renderProb -> renderProb
461-
CleanAction notScripts ->
463+
CleanActionNotScript notScripts ->
462464
"'clean' extra arguments should be script files: "
463465
++ unwords notScripts
464466
ReportCannotPruneDependencies renderCannotPruneDependencies -> renderCannotPruneDependencies
@@ -885,6 +887,8 @@ exceptionMessageCabalInstall e = case e of
885887
renderCabalFileParseError cbfError
886888
ProjectConfigParseFailure pcfError ->
887889
renderProjectConfigParseError pcfError
890+
CleanActionNotPackage ->
891+
"Not a cabal project or package directory; skipping project cleanup."
888892

889893
instance Exception (VerboseException CabalInstallException) where
890894
displayException :: VerboseException CabalInstallException -> [Char]

0 commit comments

Comments
 (0)