Skip to content

Commit b7d12f5

Browse files
committed
Reword index-state mismatch warning
1 parent 10de4e5 commit b7d12f5

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ import Distribution.Types.PackageName (PackageName)
7979
import Distribution.Version
8080
( Version, VersionRange, mkVersion, intersectVersionRanges )
8181
import Distribution.Simple.Utils
82-
( die', warn, info, createDirectoryIfMissingVerbose, fromUTF8LBS )
82+
( die', dieNoVerbosity, warn, info
83+
, createDirectoryIfMissingVerbose, fromUTF8LBS )
8384
import Distribution.Client.Setup
8485
( RepoContext(..) )
8586

@@ -106,7 +107,7 @@ import Distribution.Client.Utils ( byteStringToFilePath
106107
, tryFindAddSourcePackageDesc )
107108
import Distribution.Utils.Structured (Structured (..), nominalStructure, structuredEncodeFile, structuredDecodeFileOrFail)
108109
import Distribution.Compat.Time (getFileAge, getModTime)
109-
import System.Directory (doesFileExist, doesDirectoryExist)
110+
import System.Directory (doesFileExist, doesDirectoryExist, getModificationTime)
110111
import System.FilePath
111112
( (</>), (<.>), takeFileName, takeExtension, replaceExtension, splitDirectories, normalise, takeDirectory )
112113
import qualified System.FilePath.Posix as FilePath.Posix
@@ -275,17 +276,12 @@ getSourcePackagesAtIndexState verbosity repoCtxt mb_idxState mb_activeRepos = do
275276
return ()
276277
IndexStateTime ts0 -> do
277278
when (isiMaxTime isi /= ts0) $
278-
if ts0 > isiMaxTime isi
279-
then warn verbosity $
280-
"Requested index-state " ++ prettyShow ts0
281-
++ " is newer than '" ++ unRepoName rname ++ "'!"
282-
++ " Falling back to older state ("
283-
++ prettyShow (isiMaxTime isi) ++ ")."
284-
else info verbosity $
285-
"Requested index-state " ++ prettyShow ts0
286-
++ " does not exist in '"++ unRepoName rname ++"'!"
287-
++ " Falling back to older state ("
288-
++ prettyShow (isiMaxTime isi) ++ ")."
279+
info verbosity $
280+
"There is no index-state for '"
281+
++ unRepoName rname ++ "' exactly at the requested timestamp ("
282+
++ prettyShow ts0 ++ "). Falling back to the previous index-state that exists: "
283+
++ prettyShow (isiMaxTime isi)
284+
289285
info verbosity ("index-state("++ unRepoName rname ++") = " ++
290286
prettyShow (isiMaxTime isi) ++ " (HEAD = " ++
291287
prettyShow (isiHeadTime isi) ++ ")")
@@ -364,7 +360,11 @@ readRepoIndex :: Verbosity -> RepoContext -> Repo -> RepoIndexState
364360
-> IO (PackageIndex UnresolvedSourcePackage, [Dependency], IndexStateInfo)
365361
readRepoIndex verbosity repoCtxt repo idxState =
366362
handleNotFound $ do
367-
when (isRepoRemote repo) $ warnIfIndexIsOld =<< getIndexFileAge repo
363+
when (isRepoRemote repo) $ do
364+
dt <- getIndexFileAge repo
365+
ts <- getIndexTimestamp repo
366+
warnIfIndexIsOld dt
367+
dieIfRequestedIdxIsNewer ts
368368
-- note that if this step fails due to a bad repo cache, the the procedure can still succeed by reading from the existing cache, which is updated regardless.
369369
updateRepoIndexCache verbosity (RepoIndex repoCtxt repo) `catchIO`
370370
(\e -> warn verbosity $ "unable to update the repo index cache -- " ++ displayException e)
@@ -406,6 +406,16 @@ readRepoIndex verbosity repoCtxt repo idxState =
406406
RepoSecure{..} -> warn verbosity $ errOutdatedPackageList repoRemote dt
407407
RepoLocalNoIndex {} -> return ()
408408

409+
dieIfRequestedIdxIsNewer ts = do
410+
case idxState of
411+
IndexStateHead -> return ()
412+
IndexStateTime ts0
413+
| ts < ts0 -> die' verbosity $
414+
"Latest known index-state for '" ++ unRepoName (repoName repo) ++ "' ("
415+
++ prettyShow ts ++ ") is older than the requested index-state ("
416+
++ prettyShow ts0 ++ ").\nRun 'cabal update' or set the index-state to a value at or before " ++ prettyShow ts ++ "."
417+
| otherwise -> return ()
418+
409419
errMissingPackageList repoRemote =
410420
"The package list for '" ++ unRepoName (remoteRepoName repoRemote)
411421
++ "' does not exist. Run 'cabal update' to download it."
@@ -418,6 +428,14 @@ readRepoIndex verbosity repoCtxt repo idxState =
418428
getIndexFileAge :: Repo -> IO Double
419429
getIndexFileAge repo = getFileAge $ indexBaseName repo <.> "tar"
420430

431+
-- | Get the timestamp of a file.
432+
getIndexTimestamp :: Repo -> IO Timestamp
433+
getIndexTimestamp repo = do
434+
mb_ts <- utcTimeToTimestamp <$> getModificationTime (indexBaseName repo <.> "tar")
435+
case mb_ts of
436+
Nothing -> dieNoVerbosity "Can't convert modification time to UTC timestamp"
437+
Just ts -> pure ts
438+
421439
-- | A set of files (or directories) that can be monitored to detect when
422440
-- there might have been a change in the source packages.
423441
--

0 commit comments

Comments
 (0)