Skip to content

Commit f58b895

Browse files
committed
Fix #5866 Replace duff forgivingAbsence $ resolveFile ...
1 parent 783f195 commit f58b895

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

src/Path/Extra.hs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@ module Path.Extra
1414
, pathToLazyByteString
1515
, pathToText
1616
, tryGetModificationTime
17+
, forgivingResolveFile
18+
, forgivingResolveFile'
1719
) where
1820

1921
import Data.Time ( UTCTime )
2022
import Path
21-
( Abs, Dir, File, Rel, parseAbsDir, parseAbsFile
22-
, toFilePath
23+
( Abs, Dir, File, PathException (..), Rel, parseAbsDir
24+
, parseAbsFile, toFilePath
2325
)
2426
import Path.Internal ( Path (Path) )
25-
import Path.IO ( doesDirExist, doesFileExist, getModificationTime )
27+
import Path.IO
28+
( doesDirExist, doesFileExist, getCurrentDir
29+
, getModificationTime
30+
)
2631
import RIO
2732
import System.IO.Error ( isDoesNotExistError )
2833
import qualified Data.ByteString.Char8 as BS
2934
import qualified Data.ByteString.Lazy.Char8 as BSL
3035
import qualified Data.Text as T
3136
import qualified Data.Text.Encoding as T
37+
import qualified System.Directory as D
3238
import qualified System.FilePath as FP
3339

3440
-- | Convert to FilePath but don't add a trailing slash.
@@ -123,3 +129,30 @@ pathToText = T.pack . toFilePath
123129

124130
tryGetModificationTime :: MonadIO m => Path Abs File -> m (Either () UTCTime)
125131
tryGetModificationTime = liftIO . tryJust (guard . isDoesNotExistError) . getModificationTime
132+
133+
-- | 'Path.IO.resolveFile' (@path-io@ package) throws 'InvalidAbsFile' (@path@
134+
-- package) if the file does not exist; this function yields 'Nothing'.
135+
forgivingResolveFile ::
136+
MonadIO m
137+
=> Path Abs Dir
138+
-- ^ Base directory
139+
-> FilePath
140+
-- ^ Path to resolve
141+
-> m (Maybe (Path Abs File))
142+
forgivingResolveFile b p = liftIO $
143+
D.canonicalizePath (toFilePath b FP.</> p) >>= \cp ->
144+
catch
145+
(Just <$> parseAbsFile cp)
146+
( \e -> case e of
147+
InvalidAbsFile _ -> pure Nothing
148+
_ -> throwIO e
149+
)
150+
151+
-- | 'Path.IO.resolveFile'' (@path-io@ package) throws 'InvalidAbsFile' (@path@
152+
-- package) if the file does not exist; this function yields 'Nothing'.
153+
forgivingResolveFile' ::
154+
MonadIO m
155+
=> FilePath
156+
-- ^ Path to resolve
157+
-> m (Maybe (Path Abs File))
158+
forgivingResolveFile' p = getCurrentDir >>= flip forgivingResolveFile p

src/Stack/Build/Execute.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ import Path
7676
, stripProperPrefix
7777
)
7878
import Path.CheckInstall ( warnInstallSearchPathIssues )
79-
import Path.Extra ( toFilePathNoTrailingSep, rejectMissingFile )
79+
import Path.Extra
80+
( forgivingResolveFile, rejectMissingFile
81+
, toFilePathNoTrailingSep
82+
)
8083
import Path.IO
8184
( copyFile, doesDirExist, doesFileExist, ensureDir
82-
, forgivingAbsence, ignoringAbsence, removeDirRecur
83-
, removeFile, renameDir, renameFile, resolveFile
85+
, ignoringAbsence, removeDirRecur, removeFile, renameDir
86+
, renameFile
8487
)
8588
import RIO.Process
8689
( HasProcessContext, byteStringInput, doesExecutableExist
@@ -660,7 +663,7 @@ copyExecutables exes = do
660663
case loc of
661664
Snap -> snapBin
662665
Local -> localBin
663-
mfp <- liftIO $ forgivingAbsence (resolveFile bindir $ T.unpack name ++ ext)
666+
mfp <- liftIO $ forgivingResolveFile bindir (T.unpack name ++ ext)
664667
>>= rejectMissingFile
665668
case mfp of
666669
Nothing -> do
@@ -2541,7 +2544,7 @@ mungeBuildOutput excludeTHLoading makeAbsolute pkgDir compilerVer = void $
25412544
if isValidSuffix y
25422545
then liftIO $
25432546
fmap (fmap ((T.takeWhile isSpace x <>) . T.pack . toFilePath)) $
2544-
forgivingAbsence (resolveFile pkgDir (T.unpack $ T.dropWhile isSpace x)) `catch`
2547+
forgivingResolveFile pkgDir (T.unpack $ T.dropWhile isSpace x) `catch`
25452548
\(_ :: PathException) -> pure Nothing
25462549
else pure Nothing
25472550
case mabs of

src/Stack/ComponentFile.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ import Path
4343
, stripProperPrefix
4444
)
4545
import Path.Extra
46-
( parseCollapsedAbsFile, rejectMissingDir, rejectMissingFile
46+
( forgivingResolveFile, parseCollapsedAbsFile
47+
, rejectMissingDir, rejectMissingFile
4748
)
4849
import Path.IO
4950
( doesDirExist, doesFileExist, forgivingAbsence
50-
, getCurrentDir, listDir, resolveDir, resolveFile
51+
, getCurrentDir, listDir, resolveDir
5152
)
5253
import Stack.Constants
5354
( haskellDefaultPreprocessorExts, haskellFileExts
@@ -294,7 +295,7 @@ parseHI hiPath = do
294295
Iface.unList . Iface.dmods . Iface.deps
295296
resolveFileDependency file = do
296297
resolved <-
297-
liftIO (forgivingAbsence (resolveFile dir file)) >>=
298+
liftIO (forgivingResolveFile dir file) >>=
298299
rejectMissingFile
299300
when (isNothing resolved) $
300301
prettyWarnL

src/Stack/Ghci.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import qualified Data.Text.Lazy as TL
3030
import qualified Data.Text.Lazy.Encoding as TLE
3131
import qualified Distribution.PackageDescription as C
3232
import Path
33-
import Path.Extra ( toFilePathNoTrailingSep )
33+
import Path.Extra ( forgivingResolveFile', toFilePathNoTrailingSep )
3434
import Path.IO hiding ( withSystemTempDir )
3535
import RIO.Process
3636
( HasProcessContext, exec, proc, readProcess_
@@ -239,7 +239,7 @@ preprocessTargets buildOptsCLI sma rawTargets = do
239239
then do
240240
fileTargets <- forM fileTargetsRaw $ \fp0 -> do
241241
let fp = T.unpack fp0
242-
mpath <- liftIO $ forgivingAbsence (resolveFile' fp)
242+
mpath <- liftIO $ forgivingResolveFile' fp
243243
case mpath of
244244
Nothing -> throwM (MissingFileTarget fp)
245245
Just path -> pure path

src/Stack/PackageFile.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import Distribution.PackageDescription hiding ( FlagName )
1616
import Distribution.Simple.Glob ( matchDirFileGlob )
1717
import qualified Distribution.Types.UnqualComponentName as Cabal
1818
import Path ( parent )
19-
import Path.Extra ( rejectMissingFile )
20-
import Path.IO ( forgivingAbsence, resolveFile )
19+
import Path.Extra ( forgivingResolveFile, rejectMissingFile )
2120
import Stack.ComponentFile
2221
( benchmarkFiles, executableFiles, libraryFiles
2322
, resolveOrWarn, testFiles
@@ -37,7 +36,7 @@ resolveFileOrWarn :: FilePath.FilePath
3736
-> RIO GetPackageFileContext (Maybe (Path Abs File))
3837
resolveFileOrWarn = resolveOrWarn "File" f
3938
where
40-
f p x = liftIO (forgivingAbsence (resolveFile p x)) >>= rejectMissingFile
39+
f p x = liftIO (forgivingResolveFile p x) >>= rejectMissingFile
4140

4241
-- | Get all files referenced by the package.
4342
packageDescModulesAndFiles ::

0 commit comments

Comments
 (0)