Skip to content

Distinguish between true package names, and munged package names #4382

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

Merged
merged 1 commit into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ library
Distribution.Types.PkgconfigDependency
Distribution.Types.DependencyMap
Distribution.Types.ComponentId
Distribution.Types.MungedPackageId
Distribution.Types.PackageId
Distribution.Types.UnitId
Distribution.Types.Executable
Expand All @@ -230,6 +231,7 @@ library
Distribution.Types.ModuleReexport
Distribution.Types.ModuleRenaming
Distribution.Types.ComponentName
Distribution.Types.MungedPackageName
Distribution.Types.PackageName
Distribution.Types.PkgconfigName
Distribution.Types.UnqualComponentName
Expand Down
17 changes: 9 additions & 8 deletions Cabal/Distribution/Backpack/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Distribution.Simple.Setup as Setup
import Distribution.Simple.LocalBuildInfo
import Distribution.Types.ComponentRequestedSpec
import Distribution.Types.ComponentInclude
import Distribution.Types.MungedPackageId
import Distribution.Verbosity
import qualified Distribution.Compat.Graph as Graph
import Distribution.Compat.Graph (Graph, IsNode(..))
Expand Down Expand Up @@ -81,7 +82,7 @@ configureComponentLocalBuildInfos
(dispComponentsGraph graph0)

let conf_pkg_map = Map.fromList
[((pc_pkgname pkg, pc_compname pkg), (pc_cid pkg, pc_pkgid pkg))
[((pc_pkgname pkg, pc_compname pkg), (pc_cid pkg, packageId pkg))
| pkg <- prePkgDeps]
graph1 = toConfiguredComponents use_external_internal_deps
flagAssignment
Expand All @@ -107,9 +108,9 @@ configureComponentLocalBuildInfos
(vcat (map dispLinkedComponent graph2))

let pid_map = Map.fromList $
[ (pc_uid pkg, pc_pkgid pkg)
[ (pc_uid pkg, pc_munged_id pkg)
| pkg <- prePkgDeps] ++
[ (Installed.installedUnitId pkg, Installed.sourcePackageId pkg)
[ (Installed.installedUnitId pkg, Installed.sourceMungedPackageId pkg)
| (_, Module uid _) <- instantiate_with
, Just pkg <- [PackageIndex.lookupUnitId
installedPackageSet (unDefUnitId uid)] ]
Expand Down Expand Up @@ -204,12 +205,12 @@ toComponentLocalBuildInfos
-- TODO: This is probably wrong for Backpack
let pseudoTopPkg :: InstalledPackageInfo
pseudoTopPkg = emptyInstalledPackageInfo {
Installed.installedUnitId =
mkLegacyUnitId (packageId pkg_descr),
Installed.sourcePackageId = packageId pkg_descr,
Installed.depends =
map pc_uid externalPkgDeps
Installed.installedUnitId = mkLegacyUnitId munged_id,
Installed.sourceMungedPackageId = munged_id,
Installed.depends = map pc_uid externalPkgDeps
}
where munged_id = computeCompatPackageId (packageId pkg_descr)
CLibName
case PackageIndex.dependencyInconsistencies
. PackageIndex.insert pseudoTopPkg
$ packageDependsIndex of
Expand Down
17 changes: 8 additions & 9 deletions Cabal/Distribution/Backpack/ConfiguredComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,17 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component =
-- Resolve each @mixins@ into the actual dependency
-- from @lib_deps@.
explicit_includes
= [ let (cid, pid) =
case Map.lookup (fixFakePkgName pkg_decr name) deps_map of
= [ let keys@(_, cname) = fixFakePkgName pkg_decr name
(cid, pid) =
case Map.lookup keys deps_map of
Nothing ->
error $ "Mix-in refers to non-existent package " ++ display name ++
" (did you forget to add the package to build-depends?)"
Just r -> r
in ComponentInclude {
ci_id = cid,
-- TODO: We set pkgName = name here to make error messages
-- look better. But it would be better to properly
-- record component name here.
ci_pkgid = pid { pkgName = name },
ci_pkgid = pid,
ci_compname = cname,
ci_renaming = rns,
ci_implicit = False
}
Expand All @@ -129,10 +128,10 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component =
-- @backpack-include@ is converted into an "implicit" include.
used_explicitly = Set.fromList (map ci_id explicit_includes)
implicit_includes
= map (\((pn, _), (cid, pid)) -> ComponentInclude {
= map (\((_, cn), (cid, pid)) -> ComponentInclude {
ci_id = cid,
-- See above ci_pkgid
ci_pkgid = pid { pkgName = pn },
ci_pkgid = pid,
ci_compname = cn,
ci_renaming = defaultIncludeRenaming,
ci_implicit = True
})
Expand Down
53 changes: 2 additions & 51 deletions Cabal/Distribution/Backpack/Id.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Distribution.Simple.LocalBuildInfo
import Distribution.Types.ComponentId
import Distribution.Types.PackageId
import Distribution.Types.UnitId
import Distribution.Types.PackageName
import Distribution.Types.MungedPackageName
import Distribution.Utils.Base62
import Distribution.Version

Expand Down Expand Up @@ -71,55 +71,6 @@ computeComponentId deterministic mb_ipid mb_cid pid cname mb_details =
Nothing -> ""
Just s -> "-" ++ unUnqualComponentName s)

-- | Computes the package name for a library. If this is the public
-- library, it will just be the original package name; otherwise,
-- it will be a munged package name recording the original package
-- name as well as the name of the internal library.
--
-- A lot of tooling in the Haskell ecosystem assumes that if something
-- is installed to the package database with the package name 'foo',
-- then it actually is an entry for the (only public) library in package
-- 'foo'. With internal packages, this is not necessarily true:
-- a public library as well as arbitrarily many internal libraries may
-- come from the same package. To prevent tools from getting confused
-- in this case, the package name of these internal libraries is munged
-- so that they do not conflict the public library proper. A particular
-- case where this matters is ghc-pkg: if we don't munge the package
-- name, the inplace registration will OVERRIDE a different internal
-- library.
--
-- We munge into a reserved namespace, "z-", and encode both the
-- component name and the package name of an internal library using the
-- following format:
--
-- compat-pkg-name ::= "z-" package-name "-z-" library-name
--
-- where package-name and library-name have "-" ( "z" + ) "-"
-- segments encoded by adding an extra "z".
--
-- When we have the public library, the compat-pkg-name is just the
-- package-name, no surprises there!
--
computeCompatPackageName :: PackageName -> ComponentName -> PackageName
-- First handle the cases where we can just use the original 'PackageName'.
-- This is for the PRIMARY library, and it is non-Backpack, or the
-- indefinite package for us.
computeCompatPackageName pkg_name CLibName = pkg_name
computeCompatPackageName pkg_name cname
= mkPackageName $ "z-" ++ zdashcode (display pkg_name)
++ (case componentNameString cname of
Just cname_u -> "-z-" ++ zdashcode cname_str
where cname_str = unUnqualComponentName cname_u
Nothing -> "")

zdashcode :: String -> String
zdashcode s = go s (Nothing :: Maybe Int) []
where go [] _ r = reverse r
go ('-':z) (Just n) r | n > 0 = go z (Just 0) ('-':'z':r)
go ('-':z) _ r = go z (Just 0) ('-':r)
go ('z':z) (Just n) r = go z (Just (n+1)) ('z':r)
go (c:z) _ r = go z Nothing (c:r)

-- | In GHC 8.0, the string we pass to GHC to use for symbol
-- names for a package can be an arbitrary, IPID-compatible string.
-- However, prior to GHC 8.0 there are some restrictions on what
Expand Down Expand Up @@ -169,7 +120,7 @@ zdashcode s = go s (Nothing :: Maybe Int) []
--
computeCompatPackageKey
:: Compiler
-> PackageName
-> MungedPackageName
-> Version
-> UnitId
-> String
Expand Down
12 changes: 9 additions & 3 deletions Cabal/Distribution/Backpack/LinkedComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Distribution.Types.ModuleRenaming
import Distribution.Types.IncludeRenaming
import Distribution.Types.ComponentInclude
import Distribution.Types.ComponentId
import Distribution.Types.MungedPackageId
import Distribution.Types.PackageId
import Distribution.Package
import Distribution.PackageDescription as PD hiding (Flag)
Expand Down Expand Up @@ -106,6 +107,10 @@ dispLinkedComponent lc =
instance Package LinkedComponent where
packageId = lc_pkgid

instance HasMungedPackageId LinkedComponent where
mungedId LinkedComponent { lc_pkgid = pkgid, lc_component = component }
= computeCompatPackageId pkgid (componentName component)

toLinkedComponent
:: Verbosity
-> FullDb
Expand Down Expand Up @@ -138,8 +143,8 @@ toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
-- *unlinked* unit identity. We will use unification (relying
-- on the ModuleShape) to resolve these into linked identities.
unlinked_includes :: [ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming]
unlinked_includes = [ ComponentInclude (lookupUid cid) pid rns i
| ComponentInclude cid pid rns i <- cid_includes ]
unlinked_includes = [ ComponentInclude (lookupUid cid) pid cn rns i
| ComponentInclude cid pid cn rns i <- cid_includes ]

lookupUid :: ComponentId -> (OpenUnitId, ModuleShape)
lookupUid cid = fromMaybe (error "linkComponent: lookupUid")
Expand Down Expand Up @@ -179,11 +184,12 @@ toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
-- src_reqs_u <- mapM convertReq src_reqs
-- Read out all the final results by converting back
-- into a pure representation.
let convertIncludeU (ComponentInclude uid_u pid rns i) = do
let convertIncludeU (ComponentInclude uid_u pid cn rns i) = do
uid <- convertUnitIdU uid_u
return (ComponentInclude {
ci_id = uid,
ci_pkgid = pid,
ci_compname = cn,
ci_renaming = rns,
ci_implicit = i
})
Expand Down
15 changes: 9 additions & 6 deletions Cabal/Distribution/Backpack/PreExistingComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Distribution.Compat.Prelude
import Distribution.Backpack.ModuleShape
import Distribution.Backpack
import Distribution.Types.ComponentId
import Distribution.Types.MungedPackageId
import Distribution.Types.PackageId
import Distribution.Types.UnitId
import Distribution.Types.ComponentName
Expand All @@ -31,7 +32,7 @@ data PreExistingComponent
pc_pkgname :: PackageName,
-- | The actual name of the component.
pc_compname :: ComponentName,
pc_pkgid :: PackageId,
pc_munged_id :: MungedPackageId,
pc_uid :: UnitId,
pc_cid :: ComponentId,
pc_open_uid :: OpenUnitId,
Expand All @@ -44,11 +45,9 @@ data PreExistingComponent
ipiToPreExistingComponent :: InstalledPackageInfo -> PreExistingComponent
ipiToPreExistingComponent ipi =
PreExistingComponent {
pc_pkgname = case Installed.sourcePackageName ipi of
Just n -> n
Nothing -> pkgName $ Installed.sourcePackageId ipi,
pc_pkgname = Installed.sourcePackageName' ipi,
pc_compname = libraryComponentName $ Installed.sourceLibName ipi,
pc_pkgid = Installed.sourcePackageId ipi,
pc_munged_id = Installed.sourceMungedPackageId ipi,
pc_uid = Installed.installedUnitId ipi,
pc_cid = Installed.installedComponentId ipi,
pc_open_uid =
Expand All @@ -57,8 +56,12 @@ ipiToPreExistingComponent ipi =
pc_shape = shapeInstalledPackage ipi
}

instance HasMungedPackageId PreExistingComponent where
mungedId = pc_munged_id

instance Package PreExistingComponent where
packageId = pc_pkgid
packageId pec = PackageIdentifier (pc_pkgname pec) v
where MungedPackageId _ v = pc_munged_id pec

instance HasUnitId PreExistingComponent where
installedUnitId = pc_uid
31 changes: 19 additions & 12 deletions Cabal/Distribution/Backpack/ReadyComponent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import Distribution.Types.PackageId
import Distribution.Types.UnitId
import Distribution.Compat.Graph (IsNode(..))
import Distribution.Types.Module
import Distribution.Types.PackageName
import Distribution.Types.MungedPackageId
import Distribution.Types.MungedPackageName

import Distribution.ModuleName
import Distribution.Package
Expand Down Expand Up @@ -91,7 +92,7 @@ data InstantiatedComponent
-- | Dependencies induced by 'instc_insts'. These are recorded
-- here because there isn't a convenient way otherwise to get
-- the 'PackageId' we need to fill 'componentPackageDeps' as needed.
instc_insts_deps :: [(UnitId, PackageId)],
instc_insts_deps :: [(UnitId, MungedPackageId)],
-- | The modules exported/reexported by this library.
instc_provides :: Map ModuleName Module,
-- | The dependencies which need to be passed to the compiler
Expand All @@ -116,20 +117,27 @@ data IndefiniteComponent

-- | Compute the dependencies of a 'ReadyComponent' that should
-- be recorded in the @depends@ field of 'InstalledPackageInfo'.
rc_depends :: ReadyComponent -> [(UnitId, PackageId)]
rc_depends :: ReadyComponent -> [(UnitId, MungedPackageId)]
rc_depends rc = ordNub $
case rc_i rc of
Left indefc ->
map (\ci -> (abstractUnitId (ci_id ci), ci_pkgid ci))
map (\ci -> (abstractUnitId $ ci_id ci, toMungedPackageId ci))
(indefc_includes indefc)
Right instc ->
map (\ci -> (unDefUnitId (ci_id ci), ci_pkgid ci))
map (\ci -> (unDefUnitId $ ci_id ci, toMungedPackageId ci))
(instc_includes instc)
++ instc_insts_deps instc
where
toMungedPackageId :: ComponentInclude x y -> MungedPackageId
toMungedPackageId ci = computeCompatPackageId (ci_pkgid ci) (ci_compname ci)

instance Package ReadyComponent where
packageId = rc_pkgid

instance HasMungedPackageId ReadyComponent where
mungedId ReadyComponent { rc_pkgid = pkgid, rc_component = component }
= computeCompatPackageId pkgid (componentName component)

instance HasUnitId ReadyComponent where
installedUnitId = rc_uid

Expand All @@ -145,8 +153,8 @@ instance IsNode ReadyComponent where
_ -> []) ++
ordNub (map fst (rc_depends rc))

rc_compat_name :: ReadyComponent -> PackageName
rc_compat_name ReadyComponent{
rc_compat_name :: ReadyComponent -> MungedPackageName
rc_compat_name ReadyComponent {
rc_pkgid = PackageIdentifier pkg_name _,
rc_component = component
}
Expand Down Expand Up @@ -212,7 +220,7 @@ instance Monad InstM where
-- instantiated components are given 'HashedUnitId'.
--
toReadyComponents
:: Map UnitId PackageId
:: Map UnitId MungedPackageId
-> Map ModuleName Module -- subst for the public component
-> [LinkedComponent]
-> [ReadyComponent]
Expand Down Expand Up @@ -256,11 +264,10 @@ toReadyComponents pid_map subst0 comps
= [(dep_uid,
fromMaybe err_pid $
Map.lookup dep_uid pid_map A.<|>
fmap rc_pkgid (join (Map.lookup dep_uid s)))]
fmap mungedId (join (Map.lookup dep_uid s)))]
where
err_pid =
PackageIdentifier
(mkPackageName "nonexistent-package-this-is-a-cabal-bug")
err_pid = MungedPackageId
(mkMungedPackageName "nonexistent-package-this-is-a-cabal-bug")
(mkVersion [0])
instc = InstantiatedComponent {
instc_insts = Map.toList insts,
Expand Down
2 changes: 2 additions & 0 deletions Cabal/Distribution/Backpack/UnifyM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ convertInclude
convertInclude ci@(ComponentInclude {
ci_id = (uid, ModuleShape provs reqs),
ci_pkgid = pid,
ci_compname = compname,
ci_renaming = incl@(IncludeRenaming prov_rns req_rns),
ci_implicit = implicit
}) = addErrContext (text "In" <+> ci_msg ci) $ do
Expand Down Expand Up @@ -587,6 +588,7 @@ convertInclude ci@(ComponentInclude {
else Left) (ComponentInclude {
ci_id = uid_u,
ci_pkgid = pid,
ci_compname = compname,
ci_renaming = prov_rns',
ci_implicit = ci_implicit ci
}))
Expand Down
Loading