Skip to content

Commit f22cecc

Browse files
authored
Merge pull request #6028 from commercialhaskell/fix5866
Fix #5866 Replace duff `forgivingAbsence $ resolveFile ...`
2 parents fa58055 + 3f309e5 commit f22cecc

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.
@@ -131,3 +137,30 @@ pathToText = T.pack . toFilePath
131137
tryGetModificationTime :: MonadIO m => Path Abs File -> m (Either () UTCTime)
132138
tryGetModificationTime =
133139
liftIO . tryJust (guard . isDoesNotExistError) . getModificationTime
140+
141+
-- | 'Path.IO.resolveFile' (@path-io@ package) throws 'InvalidAbsFile' (@path@
142+
-- package) if the file does not exist; this function yields 'Nothing'.
143+
forgivingResolveFile ::
144+
MonadIO m
145+
=> Path Abs Dir
146+
-- ^ Base directory
147+
-> FilePath
148+
-- ^ Path to resolve
149+
-> m (Maybe (Path Abs File))
150+
forgivingResolveFile b p = liftIO $
151+
D.canonicalizePath (toFilePath b FP.</> p) >>= \cp ->
152+
catch
153+
(Just <$> parseAbsFile cp)
154+
( \e -> case e of
155+
InvalidAbsFile _ -> pure Nothing
156+
_ -> throwIO e
157+
)
158+
159+
-- | 'Path.IO.resolveFile'' (@path-io@ package) throws 'InvalidAbsFile' (@path@
160+
-- package) if the file does not exist; this function yields 'Nothing'.
161+
forgivingResolveFile' ::
162+
MonadIO m
163+
=> FilePath
164+
-- ^ Path to resolve
165+
-> m (Maybe (Path Abs File))
166+
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
@@ -70,11 +70,14 @@ import Path
7070
, stripProperPrefix
7171
)
7272
import Path.CheckInstall ( warnInstallSearchPathIssues )
73-
import Path.Extra ( toFilePathNoTrailingSep, rejectMissingFile )
73+
import Path.Extra
74+
( forgivingResolveFile, rejectMissingFile
75+
, toFilePathNoTrailingSep
76+
)
7477
import Path.IO
7578
( copyFile, doesDirExist, doesFileExist, ensureDir
76-
, forgivingAbsence, ignoringAbsence, removeDirRecur
77-
, removeFile, renameDir, renameFile, resolveFile
79+
, ignoringAbsence, removeDirRecur, removeFile, renameDir
80+
, renameFile
7881
)
7982
import RIO.Process
8083
( HasProcessContext, byteStringInput, doesExecutableExist
@@ -648,7 +651,7 @@ copyExecutables exes = do
648651
case loc of
649652
Snap -> snapBin
650653
Local -> localBin
651-
mfp <- liftIO $ forgivingAbsence (resolveFile bindir $ T.unpack name ++ ext)
654+
mfp <- liftIO $ forgivingResolveFile bindir (T.unpack name ++ ext)
652655
>>= rejectMissingFile
653656
case mfp of
654657
Nothing -> do
@@ -2528,7 +2531,7 @@ mungeBuildOutput excludeTHLoading makeAbsolute pkgDir compilerVer = void $
25282531
if isValidSuffix y
25292532
then liftIO $
25302533
fmap (fmap ((T.takeWhile isSpace x <>) . T.pack . toFilePath)) $
2531-
forgivingAbsence (resolveFile pkgDir (T.unpack $ T.dropWhile isSpace x)) `catch`
2534+
forgivingResolveFile pkgDir (T.unpack $ T.dropWhile isSpace x) `catch`
25322535
\(_ :: PathException) -> pure Nothing
25332536
else pure Nothing
25342537
case mabs of

src/Stack/ComponentFile.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ import Path
3939
, stripProperPrefix
4040
)
4141
import Path.Extra
42-
( parseCollapsedAbsFile, rejectMissingDir, rejectMissingFile
42+
( forgivingResolveFile, parseCollapsedAbsFile
43+
, rejectMissingDir, rejectMissingFile
4344
)
4445
import Path.IO
4546
( doesDirExist, doesFileExist, forgivingAbsence
46-
, getCurrentDir, listDir, resolveDir, resolveFile
47+
, getCurrentDir, listDir, resolveDir
4748
)
4849
import Stack.Constants
4950
( haskellDefaultPreprocessorExts, haskellFileExts
@@ -290,7 +291,7 @@ parseHI hiPath = do
290291
Iface.unList . Iface.dmods . Iface.deps
291292
resolveFileDependency file = do
292293
resolved <-
293-
liftIO (forgivingAbsence (resolveFile dir file)) >>=
294+
liftIO (forgivingResolveFile dir file) >>=
294295
rejectMissingFile
295296
when (isNothing resolved) $
296297
prettyWarnL

src/Stack/Ghci.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import qualified Data.Text.Lazy as TL
2626
import qualified Data.Text.Lazy.Encoding as TLE
2727
import qualified Distribution.PackageDescription as C
2828
import Path
29-
import Path.Extra ( toFilePathNoTrailingSep )
29+
import Path.Extra ( forgivingResolveFile', toFilePathNoTrailingSep )
3030
import Path.IO hiding ( withSystemTempDir )
3131
import RIO.Process
3232
( HasProcessContext, exec, proc, readProcess_
@@ -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 $ forgivingAbsence (resolveFile' fp)
238+
mpath <- liftIO $ forgivingResolveFile' fp
239239
case mpath of
240240
Nothing -> throwM (MissingFileTarget fp)
241241
Just path -> pure path

src/Stack/PackageFile.hs

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

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

0 commit comments

Comments
 (0)