Commit 3b990b1c authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Merge pull request #1469 from 23Skidoo/issue-1463

Add a workaround for issue #1463.
parents 4c6fa93e bcf8b2d4
......@@ -106,6 +106,8 @@ data PackageIndex = PackageIndex
-- of the same package version. These are unique by InstalledPackageId
-- and are kept in preference order.
--
-- FIXME: Clarify what "preference order" means. Check that this invariant is
-- preserved. See #1463 for discussion.
!(Map PackageName (Map Version [InstalledPackageInfo]))
deriving (Show, Read)
......
......@@ -87,7 +87,7 @@ import System.IO ( Handle, hPutStr )
import System.Exit ( ExitCode(..), exitWith )
import System.Process ( runProcess, waitForProcess )
import Control.Monad ( when, unless )
import Data.List ( maximumBy )
import Data.List ( foldl1' )
import Data.Maybe ( fromMaybe, isJust )
import Data.Monoid ( mempty )
import Data.Char ( isSpace )
......@@ -270,8 +270,24 @@ externalSetupMethod verbosity options pkg bt mkargs = do
pkgs -> return $ bestVersion id (map fst pkgs)
bestVersion :: (a -> Version) -> [a] -> a
bestVersion f = maximumBy (comparing (preference . f))
bestVersion f = firstMaximumBy (comparing (preference . f))
where
-- Like maximumBy, but picks the first maximum element instead of the
-- last. In general, we expect the preferred version to go first in the
-- list. For the default case, this has the effect of choosing the version
-- installed in the user package DB instead of the global one. See #1463.
--
-- Note: firstMaximumBy could be written as just
-- `maximumBy cmp . reverse`, but the problem is that the behaviour of
-- maximumBy is not fully specified in the case when there is not a single
-- greatest element.
firstMaximumBy :: (a -> a -> Ordering) -> [a] -> a
firstMaximumBy _ [] =
error "Distribution.Client.firstMaximumBy: empty list"
firstMaximumBy cmp xs = foldl1' maxBy xs
where
maxBy x y = case cmp x y of { GT -> x; EQ -> x; LT -> y; }
preference version = (sameVersion, sameMajorVersion
,stableVersion, latestVersion)
where
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment