Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target package names #4889

Merged
merged 18 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions cabal-install/Distribution/Client/CmdErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Distribution.Client.TargetSelector
( ComponentKindFilter, componentKind, showTargetSelector )

import Distribution.Package
( packageId, packageName )
( packageId, PackageName, packageName )
import Distribution.Types.ComponentName
( showComponentName )
import Distribution.Solver.Types.OptionalStanza
Expand Down Expand Up @@ -108,17 +108,24 @@ renderTargetSelector (TargetAllPackages (Just kfilter)) =
"all the " ++ renderComponentKind Plural kfilter
++ " in the project"

renderTargetSelector (TargetComponent pkgid CLibName WholeComponent) =
"the library in the package " ++ display pkgid
renderTargetSelector (TargetComponent pkgid cname subtarget) =
renderSubComponentTarget subtarget ++ "the "
++ renderComponentName (packageName pkgid) cname

renderTargetSelector (TargetComponent _pkgid cname WholeComponent) =
"the " ++ showComponentName cname
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname) subtarget) =
renderSubComponentTarget subtarget ++ "the component " ++ display ucname
++ " in the package " ++ display pkgname

renderTargetSelector (TargetComponent _pkgid cname (FileTarget filename)) =
"the file " ++ filename ++ " in the " ++ showComponentName cname
renderTargetSelector (TargetComponentUnknown pkgname (Right cname) subtarget) =
renderSubComponentTarget subtarget ++ "the "
++ renderComponentName pkgname cname

renderTargetSelector (TargetComponent _pkgid cname (ModuleTarget modname)) =
"the module " ++ display modname ++ " in the " ++ showComponentName cname
renderSubComponentTarget :: SubComponentTarget -> String
renderSubComponentTarget WholeComponent = ""
renderSubComponentTarget (FileTarget filename) =
"the file " ++ filename ++ "in "
renderSubComponentTarget (ModuleTarget modname) =
"the module" ++ display modname ++ "in "


renderOptionalStanza :: Plural -> OptionalStanza -> String
Expand All @@ -139,20 +146,31 @@ targetSelectorPluralPkgs :: TargetSelector -> Plural
targetSelectorPluralPkgs (TargetAllPackages _) = Plural
targetSelectorPluralPkgs (TargetPackage _ pids _) = listPlural pids
targetSelectorPluralPkgs (TargetPackageNamed _ _) = Singular
targetSelectorPluralPkgs (TargetComponent _ _ _) = Singular
targetSelectorPluralPkgs TargetComponent{} = Singular
targetSelectorPluralPkgs TargetComponentUnknown{} = Singular

-- | Does the 'TargetSelector' refer to packages or to components?
targetSelectorRefersToPkgs :: TargetSelector -> Bool
targetSelectorRefersToPkgs (TargetAllPackages mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackage _ _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackageNamed _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetComponent _ _ _) = False
targetSelectorRefersToPkgs TargetComponent{} = False
targetSelectorRefersToPkgs TargetComponentUnknown{} = False

targetSelectorFilter :: TargetSelector -> Maybe ComponentKindFilter
targetSelectorFilter (TargetPackage _ _ mkfilter) = mkfilter
targetSelectorFilter (TargetPackageNamed _ mkfilter) = mkfilter
targetSelectorFilter (TargetAllPackages mkfilter) = mkfilter
targetSelectorFilter (TargetComponent _ _ _) = Nothing
targetSelectorFilter TargetComponent{} = Nothing
targetSelectorFilter TargetComponentUnknown{} = Nothing

renderComponentName :: PackageName -> ComponentName -> String
renderComponentName pkgname CLibName = "library " ++ display pkgname
renderComponentName _ (CSubLibName name) = "library " ++ display name
renderComponentName _ (CFLibName name) = "foreign library " ++ display name
renderComponentName _ (CExeName name) = "executable " ++ display name
renderComponentName _ (CTestName name) = "test suite " ++ display name
renderComponentName _ (CBenchName name) = "benchmark " ++ display name

renderComponentKind :: Plural -> ComponentKind -> String
renderComponentKind Singular ckind = case ckind of
Expand Down Expand Up @@ -222,6 +240,18 @@ renderTargetProblemCommon verb (TargetOptionalStanzaDisabledBySolver pkgid cname
where
compkinds = renderComponentKind Plural (componentKind cname)

renderTargetProblemCommon verb (TargetProblemUnknownComponent pkgname ecname) =
"Cannot " ++ verb ++ " the "
++ (case ecname of
Left ucname -> "component " ++ display ucname
Right cname -> renderComponentName pkgname cname)
++ " from the package " ++ display pkgname
++ ", because the package does not contain a "
++ (case ecname of
Left _ -> "component"
Right cname -> renderComponentKind Singular (componentKind cname))
++ " with that name."

renderTargetProblemCommon verb (TargetProblemNoSuchPackage pkgid) =
"Internal error when trying to " ++ verb ++ " the package "
++ display pkgid ++ ". The package is not in the set of available targets "
Expand Down Expand Up @@ -331,6 +361,8 @@ renderTargetProblemNoTargets verb targetSelector =
++ renderComponentKind Plural kfilter
reason ts@TargetComponent{} =
error $ "renderTargetProblemNoTargets: " ++ show ts
reason ts@TargetComponentUnknown{} =
error $ "renderTargetProblemNoTargets: " ++ show ts

-----------------------------------------------------------
-- Renderering error messages for CannotPruneDependencies
Expand Down
48 changes: 47 additions & 1 deletion cabal-install/Distribution/Client/ProjectOrchestration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ import Distribution.Client.TargetSelector
import Distribution.Client.DistDirLayout
import Distribution.Client.Config (defaultCabalDir)
import Distribution.Client.Setup hiding (packageName)
import Distribution.Types.ComponentName
( componentNameString )
import Distribution.Types.UnqualComponentName
( UnqualComponentName, packageNameToUnqualComponentName )

import Distribution.Solver.Types.OptionalStanza

Expand Down Expand Up @@ -501,6 +505,23 @@ resolveTargets selectPackageTargets selectComponentTarget liftProblem
| otherwise
= Left (liftProblem (TargetProblemNoSuchPackage pkgid))

checkTarget (TargetComponentUnknown pkgname ecname subtarget)
| Just ats <- case ecname of
Left ucname ->
Map.lookup (pkgname, ucname)
availableTargetsByPackageNameAndUnqualComponentName
Right cname ->
Map.lookup (pkgname, cname)
availableTargetsByPackageNameAndComponentName
= fmap (componentTargets subtarget)
$ selectComponentTargets subtarget ats

| Map.member pkgname availableTargetsByPackageName
= Left (liftProblem (TargetProblemUnknownComponent pkgname ecname))

| otherwise
= Left (liftProblem (TargetNotInProject pkgname))

checkTarget bt@(TargetPackageNamed pkgname mkfilter)
| Just ats <- fmap (maybe id filterTargetsKind mkfilter)
$ Map.lookup pkgname availableTargetsByPackageName
Expand Down Expand Up @@ -541,7 +562,13 @@ data AvailableTargetIndexes = AvailableTargetIndexes {
:: AvailableTargetsMap PackageId,

availableTargetsByPackageName
:: AvailableTargetsMap PackageName
:: AvailableTargetsMap PackageName,

availableTargetsByPackageNameAndComponentName
:: AvailableTargetsMap (PackageName, ComponentName),

availableTargetsByPackageNameAndUnqualComponentName
:: AvailableTargetsMap (PackageName, UnqualComponentName)
}
type AvailableTargetsMap k = Map k [AvailableTarget (UnitId, ComponentName)]

Expand Down Expand Up @@ -571,6 +598,23 @@ availableTargetIndexes installPlan = AvailableTargetIndexes{..}
(++) packageName
availableTargetsByPackageId

availableTargetsByPackageNameAndComponentName =
Map.mapKeysWith
(++) (\(pkgid, cname) -> (packageName pkgid, cname))
availableTargetsByPackageIdAndComponentName

availableTargetsByPackageNameAndUnqualComponentName =
Map.mapKeysWith
(++) (\(pkgid, cname) -> let pname = packageName pkgid
cname' = unqualComponentName pname cname
in (pname, cname'))
availableTargetsByPackageIdAndComponentName
where
unqualComponentName :: PackageName -> ComponentName -> UnqualComponentName
unqualComponentName pkgname =
fromMaybe (packageNameToUnqualComponentName pkgname)
. componentNameString

-- Add in all the empty packages. These do not appear in the
-- availableTargetsByComponent map, since that only contains components
-- so packages with no components are invisible from that perspective.
Expand Down Expand Up @@ -664,6 +708,8 @@ data TargetProblemCommon
| TargetComponentNotBuildable PackageId ComponentName SubComponentTarget
| TargetOptionalStanzaDisabledByUser PackageId ComponentName SubComponentTarget
| TargetOptionalStanzaDisabledBySolver PackageId ComponentName SubComponentTarget
| TargetProblemUnknownComponent PackageName
(Either UnqualComponentName ComponentName)

-- The target matching stuff only returns packages local to the project,
-- so these lookups should never fail, but if 'resolveTargets' is called
Expand Down
Loading