Skip to content

Commit

Permalink
Display provenance information when packages are duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Feb 13, 2022
1 parent 66b03d5 commit 36bfef2
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions cabal-install/src/Distribution/Client/TargetSelector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ resolveTargetSelectors (KnownTargets{knownPackagesPrimary = []}) [] _ =
([TargetSelectorNoTargetsInCwd], [])

resolveTargetSelectors (KnownTargets{knownPackagesPrimary}) [] _
| [pkgid] <- pkgids =
| [(pkgid, _)] <- knownPackages =
([], [TargetPackage TargetImplicitCwd [pkgid] Nothing])
| otherwise =
([TargetSelectorNotSinglePackage pkgids], [])
([TargetSelectorNotSinglePackage knownPackages], [])
where
pkgids = [ pinfoId | KnownPackage{pinfoId} <- knownPackagesPrimary ]
knownPackages = [ (pinfoId, pinfoPackageFile) | KnownPackage{pinfoId, pinfoPackageFile} <- knownPackagesPrimary ]

resolveTargetSelectors knowntargets targetStrs mfilter =
partitionEithers
Expand Down Expand Up @@ -596,7 +596,11 @@ data TargetSelectorProblem
| TargetSelectorUnrecognised String
-- ^ Syntax error when trying to parse a target string.
| TargetSelectorNoCurrentPackage TargetString
| TargetSelectorNotSinglePackage [PackageId]
| TargetSelectorNotSinglePackage [(PackageId, Maybe (FilePath, FilePath))]
-- ^ Some PackageIds were duplicated.
-- Track them and their provenance if available.
-- The tuple is expected to hold an absolute and a relative filepath to
-- the respective cabal file.
| TargetSelectorNoTargetsInCwd
| TargetSelectorNoTargetsInProject
| TargetSelectorNoScript TargetString
Expand Down Expand Up @@ -812,23 +816,29 @@ reportTargetSelectorProblems verbosity problems = do
_:_ ->
die' verbosity noPackageErrorMessage

case [ pkgids | TargetSelectorNotSinglePackage pkgids <- problems ] of
case [ knownPkgs | TargetSelectorNotSinglePackage knownPkgs <- problems ] of
[] -> return ()
mpkgids ->
mknownPkgs ->
let
allpkgids = concat mpkgids
allknownPkgs = concat mknownPkgs
in
case allpkgids of
case allknownPkgs of
[] ->
die' verbosity noPackageErrorMessage
pkgids ->
die' verbosity $
"Multiple packages have been found:\n "
++ unlines (map prettyShow pkgids)

"Multiple packages have been found:\n"
++ unlines (
map
(uncurry prettyAmbiguousPackageProvenance)
pkgids)

where

prettyAmbiguousPackageProvenance :: PackageId -> Maybe (FilePath, FilePath) -> String
prettyAmbiguousPackageProvenance pkgid provenance =
prettyShow pkgid ++ (case provenance of
Nothing -> mempty
Just (_, relativeCabalFile) -> " defined in: " ++ relativeCabalFile)

case [ t | TargetSelectorNoScript t <- problems ] of
[] -> return ()
Expand Down Expand Up @@ -1752,7 +1762,9 @@ data KnownPackage =
KnownPackage {
pinfoId :: PackageId,
pinfoDirectory :: Maybe (FilePath, FilePath),
-- ^ Absolute path and relative path of the root directory of this package.
pinfoPackageFile :: Maybe (FilePath, FilePath),
-- ^ Absolute path and relative path to the .cabal file of this package.
pinfoComponents :: [KnownComponent]
}
| KnownPackageName {
Expand Down Expand Up @@ -1828,12 +1840,12 @@ collectKnownPackageInfo dirActions@DirActions{..}
(pkgdir, pkgfile) <-
case loc of
--TODO: local tarballs, remote tarballs etc
LocalUnpackedPackage dir _cabalFile -> do
LocalUnpackedPackage dir cabalFile -> do
dirabs <- canonicalizePath dir
dirrel <- makeRelativeToCwd dirActions dirabs
--TODO: ought to get this earlier in project reading
let fileabs = dirabs </> prettyShow (packageName pkg) <.> "cabal"
filerel = dirrel </> prettyShow (packageName pkg) <.> "cabal"
let fileabs = dirabs </> fromMaybe (prettyShow (packageName pkg) <.> "cabal") cabalFile
filerel = dirrel </> fromMaybe (prettyShow (packageName pkg) <.> "cabal") cabalFile
exists <- doesFileExist fileabs
return ( Just (dirabs, dirrel)
, if exists then Just (fileabs, filerel) else Nothing
Expand Down

0 comments on commit 36bfef2

Please sign in to comment.