Skip to content

Commit e497bbf

Browse files
committed
Distinguish between true package names, and munged package names
There should be little-to-no functional changes with this commit
1 parent dd0de02 commit e497bbf

33 files changed

+414
-193
lines changed

Cabal/Cabal.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ library
219219
Distribution.Types.PkgconfigDependency
220220
Distribution.Types.DependencyMap
221221
Distribution.Types.ComponentId
222+
Distribution.Types.MungedId
222223
Distribution.Types.PackageId
223224
Distribution.Types.UnitId
224225
Distribution.Types.Executable
@@ -230,6 +231,7 @@ library
230231
Distribution.Types.ModuleReexport
231232
Distribution.Types.ModuleRenaming
232233
Distribution.Types.ComponentName
234+
Distribution.Types.MungedName
233235
Distribution.Types.PackageName
234236
Distribution.Types.PkgconfigName
235237
Distribution.Types.UnqualComponentName

Cabal/Distribution/Backpack/Configure.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Distribution.Simple.Setup as Setup
3939
import Distribution.Simple.LocalBuildInfo
4040
import Distribution.Types.ComponentRequestedSpec
4141
import Distribution.Types.ComponentInclude
42+
import Distribution.Types.MungedId
4243
import Distribution.Verbosity
4344
import qualified Distribution.Compat.Graph as Graph
4445
import Distribution.Compat.Graph (Graph, IsNode(..))
@@ -81,7 +82,7 @@ configureComponentLocalBuildInfos
8182
(dispComponentsGraph graph0)
8283

8384
let conf_pkg_map = Map.fromList
84-
[((pc_pkgname pkg, pc_compname pkg), (pc_cid pkg, pc_pkgid pkg))
85+
[((pc_pkgname pkg, pc_compname pkg), (pc_cid pkg, packageId pkg))
8586
| pkg <- prePkgDeps]
8687
graph1 = toConfiguredComponents use_external_internal_deps
8788
flagAssignment
@@ -107,9 +108,9 @@ configureComponentLocalBuildInfos
107108
(vcat (map dispLinkedComponent graph2))
108109

109110
let pid_map = Map.fromList $
110-
[ (pc_uid pkg, pc_pkgid pkg)
111+
[ (pc_uid pkg, pc_munged_id pkg)
111112
| pkg <- prePkgDeps] ++
112-
[ (Installed.installedUnitId pkg, Installed.sourcePackageId pkg)
113+
[ (Installed.installedUnitId pkg, Installed.sourceMungedId pkg)
113114
| (_, Module uid _) <- instantiate_with
114115
, Just pkg <- [PackageIndex.lookupUnitId
115116
installedPackageSet (unDefUnitId uid)] ]
@@ -204,12 +205,12 @@ toComponentLocalBuildInfos
204205
-- TODO: This is probably wrong for Backpack
205206
let pseudoTopPkg :: InstalledPackageInfo
206207
pseudoTopPkg = emptyInstalledPackageInfo {
207-
Installed.installedUnitId =
208-
mkLegacyUnitId (packageId pkg_descr),
209-
Installed.sourcePackageId = packageId pkg_descr,
210-
Installed.depends =
211-
map pc_uid externalPkgDeps
208+
Installed.installedUnitId = mkLegacyUnitId munged_id,
209+
Installed.sourceMungedId = munged_id,
210+
Installed.depends = map pc_uid externalPkgDeps
212211
}
212+
where munged_id = computeCompatPackageId (packageId pkg_descr)
213+
CLibName
213214
case PackageIndex.dependencyInconsistencies
214215
. PackageIndex.insert pseudoTopPkg
215216
$ packageDependsIndex of

Cabal/Distribution/Backpack/ConfiguredComponent.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,17 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component =
108108
-- Resolve each @mixins@ into the actual dependency
109109
-- from @lib_deps@.
110110
explicit_includes
111-
= [ let (cid, pid) =
112-
case Map.lookup (fixFakePkgName pkg_decr name) deps_map of
111+
= [ let keys@(_, cname) = fixFakePkgName pkg_decr name
112+
(cid, pid) =
113+
case Map.lookup keys deps_map of
113114
Nothing ->
114115
error $ "Mix-in refers to non-existent package " ++ display name ++
115116
" (did you forget to add the package to build-depends?)"
116117
Just r -> r
117118
in ComponentInclude {
118119
ci_id = cid,
119-
-- TODO: We set pkgName = name here to make error messages
120-
-- look better. But it would be better to properly
121-
-- record component name here.
122-
ci_pkgid = pid { pkgName = name },
120+
ci_pkgid = pid,
121+
ci_compname = cname,
123122
ci_renaming = rns,
124123
ci_implicit = False
125124
}
@@ -129,10 +128,10 @@ mkConfiguredComponent pkg_decr this_cid lib_deps exe_deps component =
129128
-- @backpack-include@ is converted into an "implicit" include.
130129
used_explicitly = Set.fromList (map ci_id explicit_includes)
131130
implicit_includes
132-
= map (\((pn, _), (cid, pid)) -> ComponentInclude {
131+
= map (\((_, cn), (cid, pid)) -> ComponentInclude {
133132
ci_id = cid,
134-
-- See above ci_pkgid
135-
ci_pkgid = pid { pkgName = pn },
133+
ci_pkgid = pid,
134+
ci_compname = cn,
136135
ci_renaming = defaultIncludeRenaming,
137136
ci_implicit = True
138137
})

Cabal/Distribution/Backpack/Id.hs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Distribution.Simple.LocalBuildInfo
2020
import Distribution.Types.ComponentId
2121
import Distribution.Types.PackageId
2222
import Distribution.Types.UnitId
23-
import Distribution.Types.PackageName
23+
import Distribution.Types.MungedName
2424
import Distribution.Utils.Base62
2525
import Distribution.Version
2626

@@ -71,55 +71,6 @@ computeComponentId deterministic mb_ipid mb_cid pid cname mb_details =
7171
Nothing -> ""
7272
Just s -> "-" ++ unUnqualComponentName s)
7373

74-
-- | Computes the package name for a library. If this is the public
75-
-- library, it will just be the original package name; otherwise,
76-
-- it will be a munged package name recording the original package
77-
-- name as well as the name of the internal library.
78-
--
79-
-- A lot of tooling in the Haskell ecosystem assumes that if something
80-
-- is installed to the package database with the package name 'foo',
81-
-- then it actually is an entry for the (only public) library in package
82-
-- 'foo'. With internal packages, this is not necessarily true:
83-
-- a public library as well as arbitrarily many internal libraries may
84-
-- come from the same package. To prevent tools from getting confused
85-
-- in this case, the package name of these internal libraries is munged
86-
-- so that they do not conflict the public library proper. A particular
87-
-- case where this matters is ghc-pkg: if we don't munge the package
88-
-- name, the inplace registration will OVERRIDE a different internal
89-
-- library.
90-
--
91-
-- We munge into a reserved namespace, "z-", and encode both the
92-
-- component name and the package name of an internal library using the
93-
-- following format:
94-
--
95-
-- compat-pkg-name ::= "z-" package-name "-z-" library-name
96-
--
97-
-- where package-name and library-name have "-" ( "z" + ) "-"
98-
-- segments encoded by adding an extra "z".
99-
--
100-
-- When we have the public library, the compat-pkg-name is just the
101-
-- package-name, no surprises there!
102-
--
103-
computeCompatPackageName :: PackageName -> ComponentName -> PackageName
104-
-- First handle the cases where we can just use the original 'PackageName'.
105-
-- This is for the PRIMARY library, and it is non-Backpack, or the
106-
-- indefinite package for us.
107-
computeCompatPackageName pkg_name CLibName = pkg_name
108-
computeCompatPackageName pkg_name cname
109-
= mkPackageName $ "z-" ++ zdashcode (display pkg_name)
110-
++ (case componentNameString cname of
111-
Just cname_u -> "-z-" ++ zdashcode cname_str
112-
where cname_str = unUnqualComponentName cname_u
113-
Nothing -> "")
114-
115-
zdashcode :: String -> String
116-
zdashcode s = go s (Nothing :: Maybe Int) []
117-
where go [] _ r = reverse r
118-
go ('-':z) (Just n) r | n > 0 = go z (Just 0) ('-':'z':r)
119-
go ('-':z) _ r = go z (Just 0) ('-':r)
120-
go ('z':z) (Just n) r = go z (Just (n+1)) ('z':r)
121-
go (c:z) _ r = go z Nothing (c:r)
122-
12374
-- | In GHC 8.0, the string we pass to GHC to use for symbol
12475
-- names for a package can be an arbitrary, IPID-compatible string.
12576
-- However, prior to GHC 8.0 there are some restrictions on what
@@ -169,7 +120,7 @@ zdashcode s = go s (Nothing :: Maybe Int) []
169120
--
170121
computeCompatPackageKey
171122
:: Compiler
172-
-> PackageName
123+
-> MungedName
173124
-> Version
174125
-> UnitId
175126
-> String

Cabal/Distribution/Backpack/LinkedComponent.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Distribution.Types.ModuleRenaming
2929
import Distribution.Types.IncludeRenaming
3030
import Distribution.Types.ComponentInclude
3131
import Distribution.Types.ComponentId
32+
import Distribution.Types.MungedId
3233
import Distribution.Types.PackageId
3334
import Distribution.Package
3435
import Distribution.PackageDescription as PD hiding (Flag)
@@ -106,6 +107,10 @@ dispLinkedComponent lc =
106107
instance Package LinkedComponent where
107108
packageId = lc_pkgid
108109

110+
instance HasMungedId LinkedComponent where
111+
mungedId LinkedComponent { lc_pkgid = pkgid, lc_component = component }
112+
= computeCompatPackageId pkgid (componentName component)
113+
109114
toLinkedComponent
110115
:: Verbosity
111116
-> FullDb
@@ -138,8 +143,8 @@ toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
138143
-- *unlinked* unit identity. We will use unification (relying
139144
-- on the ModuleShape) to resolve these into linked identities.
140145
unlinked_includes :: [ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming]
141-
unlinked_includes = [ ComponentInclude (lookupUid cid) pid rns i
142-
| ComponentInclude cid pid rns i <- cid_includes ]
146+
unlinked_includes = [ ComponentInclude (lookupUid cid) pid cn rns i
147+
| ComponentInclude cid pid cn rns i <- cid_includes ]
143148

144149
lookupUid :: ComponentId -> (OpenUnitId, ModuleShape)
145150
lookupUid cid = fromMaybe (error "linkComponent: lookupUid")
@@ -179,11 +184,12 @@ toLinkedComponent verbosity db this_pid pkg_map ConfiguredComponent {
179184
-- src_reqs_u <- mapM convertReq src_reqs
180185
-- Read out all the final results by converting back
181186
-- into a pure representation.
182-
let convertIncludeU (ComponentInclude uid_u pid rns i) = do
187+
let convertIncludeU (ComponentInclude uid_u pid cn rns i) = do
183188
uid <- convertUnitIdU uid_u
184189
return (ComponentInclude {
185190
ci_id = uid,
186191
ci_pkgid = pid,
192+
ci_compname = cn,
187193
ci_renaming = rns,
188194
ci_implicit = i
189195
})

Cabal/Distribution/Backpack/PreExistingComponent.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import Distribution.Compat.Prelude
1010
import Distribution.Backpack.ModuleShape
1111
import Distribution.Backpack
1212
import Distribution.Types.ComponentId
13+
import Distribution.Types.MungedId
1314
import Distribution.Types.PackageId
1415
import Distribution.Types.UnitId
1516
import Distribution.Types.ComponentName
17+
import Distribution.Types.MungedName
1618
import Distribution.Types.PackageName
1719
import Distribution.Package
1820

@@ -31,7 +33,7 @@ data PreExistingComponent
3133
pc_pkgname :: PackageName,
3234
-- | The actual name of the component.
3335
pc_compname :: ComponentName,
34-
pc_pkgid :: PackageId,
36+
pc_munged_id :: MungedId,
3537
pc_uid :: UnitId,
3638
pc_cid :: ComponentId,
3739
pc_open_uid :: OpenUnitId,
@@ -46,9 +48,10 @@ ipiToPreExistingComponent ipi =
4648
PreExistingComponent {
4749
pc_pkgname = case Installed.sourcePackageName ipi of
4850
Just n -> n
49-
Nothing -> pkgName $ Installed.sourcePackageId ipi,
51+
Nothing -> mkPackageName $ unMungedName
52+
$ mungedName $ Installed.sourceMungedId ipi,
5053
pc_compname = libraryComponentName $ Installed.sourceLibName ipi,
51-
pc_pkgid = Installed.sourcePackageId ipi,
54+
pc_munged_id = Installed.sourceMungedId ipi,
5255
pc_uid = Installed.installedUnitId ipi,
5356
pc_cid = Installed.installedComponentId ipi,
5457
pc_open_uid =
@@ -57,8 +60,12 @@ ipiToPreExistingComponent ipi =
5760
pc_shape = shapeInstalledPackage ipi
5861
}
5962

63+
instance HasMungedId PreExistingComponent where
64+
mungedId = pc_munged_id
65+
6066
instance Package PreExistingComponent where
61-
packageId = pc_pkgid
67+
packageId pec = PackageIdentifier (pc_pkgname pec) v
68+
where MungedId _ v = pc_munged_id pec
6269

6370
instance HasUnitId PreExistingComponent where
6471
installedUnitId = pc_uid

Cabal/Distribution/Backpack/ReadyComponent.hs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import Distribution.Types.PackageId
2828
import Distribution.Types.UnitId
2929
import Distribution.Compat.Graph (IsNode(..))
3030
import Distribution.Types.Module
31-
import Distribution.Types.PackageName
31+
import Distribution.Types.MungedId
32+
import Distribution.Types.MungedName
3233

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

117118
-- | Compute the dependencies of a 'ReadyComponent' that should
118119
-- be recorded in the @depends@ field of 'InstalledPackageInfo'.
119-
rc_depends :: ReadyComponent -> [(UnitId, PackageId)]
120+
rc_depends :: ReadyComponent -> [(UnitId, MungedId)]
120121
rc_depends rc = ordNub $
121122
case rc_i rc of
122123
Left indefc ->
123-
map (\ci -> (abstractUnitId (ci_id ci), ci_pkgid ci))
124+
map (\ci -> (abstractUnitId $ ci_id ci, toMungedId ci))
124125
(indefc_includes indefc)
125126
Right instc ->
126-
map (\ci -> (unDefUnitId (ci_id ci), ci_pkgid ci))
127+
map (\ci -> (unDefUnitId $ ci_id ci, toMungedId ci))
127128
(instc_includes instc)
128129
++ instc_insts_deps instc
130+
where
131+
toMungedId :: ComponentInclude x y -> MungedId
132+
toMungedId ci = computeCompatPackageId (ci_pkgid ci) (ci_compname ci)
129133

130134
instance Package ReadyComponent where
131135
packageId = rc_pkgid
132136

137+
instance HasMungedId ReadyComponent where
138+
mungedId ReadyComponent { rc_pkgid = pkgid, rc_component = component }
139+
= computeCompatPackageId pkgid (componentName component)
140+
133141
instance HasUnitId ReadyComponent where
134142
installedUnitId = rc_uid
135143

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

148-
rc_compat_name :: ReadyComponent -> PackageName
149-
rc_compat_name ReadyComponent{
156+
rc_compat_name :: ReadyComponent -> MungedName
157+
rc_compat_name ReadyComponent {
150158
rc_pkgid = PackageIdentifier pkg_name _,
151159
rc_component = component
152160
}
@@ -212,7 +220,7 @@ instance Monad InstM where
212220
-- instantiated components are given 'HashedUnitId'.
213221
--
214222
toReadyComponents
215-
:: Map UnitId PackageId
223+
:: Map UnitId MungedId
216224
-> Map ModuleName Module -- subst for the public component
217225
-> [LinkedComponent]
218226
-> [ReadyComponent]
@@ -256,11 +264,10 @@ toReadyComponents pid_map subst0 comps
256264
= [(dep_uid,
257265
fromMaybe err_pid $
258266
Map.lookup dep_uid pid_map A.<|>
259-
fmap rc_pkgid (join (Map.lookup dep_uid s)))]
267+
fmap mungedId (join (Map.lookup dep_uid s)))]
260268
where
261-
err_pid =
262-
PackageIdentifier
263-
(mkPackageName "nonexistent-package-this-is-a-cabal-bug")
269+
err_pid = MungedId
270+
(mkMungedName "nonexistent-package-this-is-a-cabal-bug")
264271
(mkVersion [0])
265272
instc = InstantiatedComponent {
266273
instc_insts = Map.toList insts,

Cabal/Distribution/Backpack/UnifyM.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ convertInclude
450450
convertInclude ci@(ComponentInclude {
451451
ci_id = (uid, ModuleShape provs reqs),
452452
ci_pkgid = pid,
453+
ci_compname = compname,
453454
ci_renaming = incl@(IncludeRenaming prov_rns req_rns),
454455
ci_implicit = implicit
455456
}) = addErrContext (text "In" <+> ci_msg ci) $ do
@@ -587,6 +588,7 @@ convertInclude ci@(ComponentInclude {
587588
else Left) (ComponentInclude {
588589
ci_id = uid_u,
589590
ci_pkgid = pid,
591+
ci_compname = compname,
590592
ci_renaming = prov_rns',
591593
ci_implicit = ci_implicit ci
592594
}))

0 commit comments

Comments
 (0)