diff --git a/cabal-install-solver/src/Distribution/Solver/Types/ProjectConfigPath.hs b/cabal-install-solver/src/Distribution/Solver/Types/ProjectConfigPath.hs index c57ade0c3e3..68b00334392 100644 --- a/cabal-install-solver/src/Distribution/Solver/Types/ProjectConfigPath.hs +++ b/cabal-install-solver/src/Distribution/Solver/Types/ProjectConfigPath.hs @@ -1,4 +1,5 @@ {-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE ViewPatterns #-} module Distribution.Solver.Types.ProjectConfigPath ( @@ -24,7 +25,7 @@ import Prelude (sequence) import Data.Coerce (coerce) import Data.List.NonEmpty ((<|)) -import Network.URI (parseURI) +import Network.URI (parseURI, parseAbsoluteURI) import System.Directory import System.FilePath import qualified Data.List.NonEmpty as NE @@ -44,7 +45,24 @@ import Text.PrettyPrint -- List elements are relative to each other but once canonicalized, elements are -- relative to the directory of the project root. newtype ProjectConfigPath = ProjectConfigPath (NonEmpty FilePath) - deriving (Eq, Ord, Show, Generic) + deriving (Eq, Show, Generic) + +-- | Sorts URIs after local paths and longer paths after shorter ones. +instance Ord ProjectConfigPath where + compare (ProjectConfigPath (NE.toList -> as)) (ProjectConfigPath (NE.toList -> bs)) = + case (as, bs) of + (a:as', b:bs') -> case (parseAbsoluteURI a, parseAbsoluteURI b) of + (Just _, Just _) -> let uriOrd = compare a b in if uriOrd /= EQ then uriOrd else + compare as' bs' + (Just _, Nothing) -> GT + (Nothing, Just _) -> LT + (Nothing, Nothing) -> compare (splitPath a) (splitPath b) + _ -> let lenOrd = compare (length as) (length bs) in if lenOrd /= EQ then lenOrd else + let pathOrd = compare (length ass) (length bss) in if pathOrd /= EQ then pathOrd else + compare ass bss + where + ass = splitPath <$> as + bss = splitPath <$> bs instance Binary ProjectConfigPath instance Structured ProjectConfigPath diff --git a/changelog.d/pr-10546 b/changelog.d/pr-10546 new file mode 100644 index 00000000000..bd849ed0648 --- /dev/null +++ b/changelog.d/pr-10546 @@ -0,0 +1,9 @@ +--- +synopsis: Add an Ord instance for ProjectConfigPath +packages: [cabal-install-solver] +prs: 10546 +--- + +Add an `Ord` instance for `ProjectConfigPath` that sorts URIs after local paths +and longer paths after shorter ones. With this the printing of "Configuration is +affected by the following files" messages is deduplicated.