Skip to content

Commit 557174b

Browse files
authored
Merge pull request #6045 from commercialhaskell/forgivingResolveDir
Add forgivingResolveDir
2 parents f22cecc + b01fd12 commit 557174b

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

src/Path/Extra.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Path.Extra
1414
, pathToLazyByteString
1515
, pathToText
1616
, tryGetModificationTime
17+
, forgivingResolveDir
1718
, forgivingResolveFile
1819
, forgivingResolveFile'
1920
) where
@@ -138,6 +139,24 @@ tryGetModificationTime :: MonadIO m => Path Abs File -> m (Either () UTCTime)
138139
tryGetModificationTime =
139140
liftIO . tryJust (guard . isDoesNotExistError) . getModificationTime
140141

142+
-- | 'Path.IO.resolveDir' (@path-io@ package) throws 'InvalidAbsDir' (@path@
143+
-- package) if the directory does not exist; this function yields 'Nothing'.
144+
forgivingResolveDir ::
145+
MonadIO m
146+
=> Path Abs Dir
147+
-- ^ Base directory
148+
-> FilePath
149+
-- ^ Path to resolve
150+
-> m (Maybe (Path Abs Dir))
151+
forgivingResolveDir b p = liftIO $
152+
D.canonicalizePath (toFilePath b FP.</> p) >>= \cp ->
153+
catch
154+
(Just <$> parseAbsDir cp)
155+
( \e -> case e of
156+
InvalidAbsDir _ -> pure Nothing
157+
_ -> throwIO e
158+
)
159+
141160
-- | 'Path.IO.resolveFile' (@path-io@ package) throws 'InvalidAbsFile' (@path@
142161
-- package) if the file does not exist; this function yields 'Nothing'.
143162
forgivingResolveFile ::

src/Stack/Build/Execute.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ copyExecutables exes = do
651651
case loc of
652652
Snap -> snapBin
653653
Local -> localBin
654-
mfp <- liftIO $ forgivingResolveFile bindir (T.unpack name ++ ext)
654+
mfp <- forgivingResolveFile bindir (T.unpack name ++ ext)
655655
>>= rejectMissingFile
656656
case mfp of
657657
Nothing -> do
@@ -2482,12 +2482,13 @@ data KeepOutputOpen
24822482
deriving Eq
24832483

24842484
-- | Strip Template Haskell "Loading package" lines and making paths absolute.
2485-
mungeBuildOutput :: forall m. MonadIO m
2486-
=> ExcludeTHLoading -- ^ exclude TH loading?
2487-
-> ConvertPathsToAbsolute -- ^ convert paths to absolute?
2488-
-> Path Abs Dir -- ^ package's root directory
2489-
-> ActualCompiler -- ^ compiler we're building with
2490-
-> ConduitM Text Text m ()
2485+
mungeBuildOutput ::
2486+
forall m. (MonadIO m, MonadUnliftIO m)
2487+
=> ExcludeTHLoading -- ^ exclude TH loading?
2488+
-> ConvertPathsToAbsolute -- ^ convert paths to absolute?
2489+
-> Path Abs Dir -- ^ package's root directory
2490+
-> ActualCompiler -- ^ compiler we're building with
2491+
-> ConduitM Text Text m ()
24912492
mungeBuildOutput excludeTHLoading makeAbsolute pkgDir compilerVer = void $
24922493
CT.lines
24932494
.| CL.map stripCR
@@ -2529,7 +2530,7 @@ mungeBuildOutput excludeTHLoading makeAbsolute pkgDir compilerVer = void $
25292530
let (x, y) = T.break (== ':') bs
25302531
mabs <-
25312532
if isValidSuffix y
2532-
then liftIO $
2533+
then
25332534
fmap (fmap ((T.takeWhile isSpace x <>) . T.pack . toFilePath)) $
25342535
forgivingResolveFile pkgDir (T.unpack $ T.dropWhile isSpace x) `catch`
25352536
\(_ :: PathException) -> pure Nothing

src/Stack/Build/Target.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ import qualified Data.Map as Map
6868
import qualified Data.Set as Set
6969
import qualified Data.Text as T
7070
import Path ( isProperPrefixOf )
71-
import Path.Extra ( rejectMissingDir )
72-
import Path.IO ( forgivingAbsence, getCurrentDir, resolveDir )
71+
import Path.Extra ( forgivingResolveDir, rejectMissingDir )
72+
import Path.IO ( getCurrentDir )
7373
import RIO.Process ( HasProcessContext )
7474
import Stack.SourceMap ( additionalDepPackage )
7575
import Stack.Prelude
@@ -150,8 +150,7 @@ parseRawTargetDirs root locals ri =
150150
case parseRawTarget t of
151151
Just rt -> pure $ Right [(ri, rt)]
152152
Nothing -> do
153-
mdir <- liftIO $ forgivingAbsence (resolveDir root (T.unpack t))
154-
>>= rejectMissingDir
153+
mdir <- forgivingResolveDir root (T.unpack t) >>= rejectMissingDir
155154
case mdir of
156155
Nothing -> pure $ Left $
157156
fillSep

src/Stack/ComponentFile.hs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ import Path
3939
, stripProperPrefix
4040
)
4141
import Path.Extra
42-
( forgivingResolveFile, parseCollapsedAbsFile
43-
, rejectMissingDir, rejectMissingFile
42+
( forgivingResolveDir, forgivingResolveFile
43+
, parseCollapsedAbsFile, rejectMissingDir, rejectMissingFile
4444
)
4545
import Path.IO
46-
( doesDirExist, doesFileExist, forgivingAbsence
47-
, getCurrentDir, listDir, resolveDir
48-
)
46+
( doesDirExist, doesFileExist, getCurrentDir, listDir )
4947
import Stack.Constants
5048
( haskellDefaultPreprocessorExts, haskellFileExts
5149
, relDirAutogen, relDirBuild, relDirGlobalAutogen
@@ -290,9 +288,7 @@ parseHI hiPath = do
290288
let moduleNames = fmap (fromString . T.unpack . decodeUtf8Lenient . fst) .
291289
Iface.unList . Iface.dmods . Iface.deps
292290
resolveFileDependency file = do
293-
resolved <-
294-
liftIO (forgivingResolveFile dir file) >>=
295-
rejectMissingFile
291+
resolved <- forgivingResolveFile dir file >>= rejectMissingFile
296292
when (isNothing resolved) $
297293
prettyWarnL
298294
[ flow "Dependent file listed in:"
@@ -500,7 +496,7 @@ resolveDirOrWarn :: FilePath.FilePath
500496
-> RIO GetPackageFileContext (Maybe (Path Abs Dir))
501497
resolveDirOrWarn = resolveOrWarn "Directory" f
502498
where
503-
f p x = liftIO (forgivingAbsence (resolveDir p x)) >>= rejectMissingDir
499+
f p x = forgivingResolveDir p x >>= rejectMissingDir
504500

505501
-- | Make the global autogen dir if Cabal version is new enough.
506502
packageAutogenDir :: Version -> Path Abs Dir -> Maybe (Path Abs Dir)

src/Stack/Ghci.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ preprocessTargets buildOptsCLI sma rawTargets = do
235235
then do
236236
fileTargets <- forM fileTargetsRaw $ \fp0 -> do
237237
let fp = T.unpack fp0
238-
mpath <- liftIO $ forgivingResolveFile' fp
238+
mpath <- forgivingResolveFile' fp
239239
case mpath of
240240
Nothing -> throwM (MissingFileTarget fp)
241241
Just path -> pure path

src/Stack/PackageFile.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ resolveFileOrWarn :: FilePath.FilePath
3535
-> RIO GetPackageFileContext (Maybe (Path Abs File))
3636
resolveFileOrWarn = resolveOrWarn "File" f
3737
where
38-
f p x = liftIO (forgivingResolveFile p x) >>= rejectMissingFile
38+
f p x = forgivingResolveFile p x >>= rejectMissingFile
3939

4040
-- | Get all files referenced by the package.
4141
packageDescModulesAndFiles ::

0 commit comments

Comments
 (0)