Skip to content

Commit 18fcd9c

Browse files
committed
Properly assign component ID/build dir for LibV09 test libraries
Cabal's LibV09 support has always been a bit skeevy. The general idea was that a detailed-0.9 test-suite is built as a library and an Cabal-provided stub executable. In particular, the test suite library must be installed to the installed package database so that the executable can be compiled. Old versions of Cabal did something very skeevy here: they installed the test library as a "package", with the same package name as the "test-suite" stanza; furthermore, they built the products into the same directory as the library proper. Consequently, a lot of bad things could happen (both of which I've added tests for): 1. If the name of the test suite and the name of some other package coincide (and have the same version), they will clobber each other. In GHC 7.8 and earlier, this just flat out kills the build, because it will shadow. There's an explicit test to make sure test suites don't conflict with the package name, but you can get unlucky. 2. The test suite library is built into the same directory as the main library, which means that if the test library implements the same module name as something in the main library it will clobber the interface file and badness will ensue. This patchset fixes both of these issues, by (1) giving internal test libraries proper names which are guaranteed to be unique up to Cabal's dependency resolution, and (2) building the test suite library into a separate directory. In doing so, it also lays the groundwork for other types of internal libraries, e.g. #269, as well as extra (invisible) libraries which we may install. For GHC 7.8 and earlier, we follow the reserved namespace convention as per #3017. Signed-off-by: Edward Z. Yang <[email protected]>
1 parent 98cd8ca commit 18fcd9c

File tree

18 files changed

+318
-94
lines changed

18 files changed

+318
-94
lines changed

Cabal/Cabal.cabal

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ extra-source-files:
8282
tests/PackageTests/CMain/my.cabal
8383
tests/PackageTests/DeterministicAr/Lib.hs
8484
tests/PackageTests/DeterministicAr/my.cabal
85+
tests/PackageTests/DuplicateModuleName/DuplicateModuleName.cabal
86+
tests/PackageTests/DuplicateModuleName/src/Foo.hs
87+
tests/PackageTests/DuplicateModuleName/tests/Foo.hs
88+
tests/PackageTests/DuplicateModuleName/tests2/Foo.hs
8589
tests/PackageTests/EmptyLib/empty/empty.cabal
8690
tests/PackageTests/Haddock/CPP.hs
8791
tests/PackageTests/Haddock/Literate.lhs
@@ -115,6 +119,11 @@ extra-source-files:
115119
tests/PackageTests/TemplateHaskell/vanilla/Lib.hs
116120
tests/PackageTests/TemplateHaskell/vanilla/TH.hs
117121
tests/PackageTests/TemplateHaskell/vanilla/my.cabal
122+
tests/PackageTests/TestNameCollision/child/Child.hs
123+
tests/PackageTests/TestNameCollision/child/child.cabal
124+
tests/PackageTests/TestNameCollision/child/tests/Test.hs
125+
tests/PackageTests/TestNameCollision/parent/Parent.hs
126+
tests/PackageTests/TestNameCollision/parent/parent.cabal
118127
tests/PackageTests/TestOptions/TestOptions.cabal
119128
tests/PackageTests/TestOptions/test-TestOptions.hs
120129
tests/PackageTests/TestStanza/my.cabal

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,6 @@ checkTestSuite pkg test =
297297
PackageDistInexcusable $
298298
"The package uses a C/C++/obj-C source file for the 'main-is' field. "
299299
++ "To use this feature you must specify 'cabal-version: >= 1.18'."
300-
301-
-- Test suites might be built as (internal) libraries named after
302-
-- the test suite and thus their names must not clash with the
303-
-- name of the package.
304-
, check libNameClash $
305-
PackageBuildImpossible $
306-
"The test suite " ++ testName test
307-
++ " has the same name as the package."
308300
]
309301
where
310302
moduleDuplicates = dups $ testModules test
@@ -317,13 +309,8 @@ checkTestSuite pkg test =
317309
TestSuiteExeV10 _ f -> takeExtension f `notElem` [".hs", ".lhs"]
318310
_ -> False
319311

320-
libNameClash = testName test `elem` [ libName
321-
| _lib <- maybeToList (library pkg)
322-
, let PackageName libName =
323-
pkgName (package pkg) ]
324-
325312
checkBenchmark :: PackageDescription -> Benchmark -> [PackageCheck]
326-
checkBenchmark pkg bm =
313+
checkBenchmark _pkg bm =
327314
catMaybes [
328315

329316
case benchmarkInterface bm of
@@ -349,12 +336,6 @@ checkBenchmark pkg bm =
349336
PackageBuildImpossible $
350337
"The 'main-is' field must specify a '.hs' or '.lhs' file "
351338
++ "(even if it is generated by a preprocessor)."
352-
353-
-- See comment for similar check on test suites.
354-
, check libNameClash $
355-
PackageBuildImpossible $
356-
"The benchmark " ++ benchmarkName bm
357-
++ " has the same name as the package."
358339
]
359340
where
360341
moduleDuplicates = dups $ benchmarkModules bm
@@ -363,11 +344,6 @@ checkBenchmark pkg bm =
363344
BenchmarkExeV10 _ f -> takeExtension f `notElem` [".hs", ".lhs"]
364345
_ -> False
365346

366-
libNameClash = benchmarkName bm `elem` [ libName
367-
| _lib <- maybeToList (library pkg)
368-
, let PackageName libName =
369-
pkgName (package pkg) ]
370-
371347
-- ------------------------------------------------------------
372348
-- * Additional pure checks
373349
-- ------------------------------------------------------------

Cabal/Distribution/Simple/Build.hs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import Distribution.Simple.LocalBuildInfo
4747
import Distribution.Simple.Program.Types
4848
import Distribution.Simple.Program.Db
4949
import Distribution.Simple.BuildPaths
50+
import Distribution.Simple.Configure
5051
import Distribution.Simple.Register
5152
import Distribution.Simple.Test.LibV09
5253
import Distribution.Simple.Utils
@@ -225,7 +226,10 @@ buildComponent verbosity numJobs pkg_descr lbi0 suffixes
225226
extras <- preprocessExtras comp lbi
226227
info verbosity $ "Building test suite " ++ testName test ++ "..."
227228
buildLib verbosity numJobs pkg lbi lib libClbi
228-
registerPackage verbosity (compiler lbi) (withPrograms lbi) False
229+
-- NB: need to enable multiple instances here, because on 7.10+
230+
-- the package name is the same as the library, and we still
231+
-- want the registration to go through.
232+
registerPackage verbosity (compiler lbi) (withPrograms lbi) True
229233
(withPackageDB lbi) ipi
230234
let ebi = buildInfo exe
231235
exe' = exe { buildInfo = addExtraCSources ebi extras }
@@ -377,17 +381,22 @@ testSuiteLibV09AsLibAndExe pkg_descr
377381
libExposed = True,
378382
libBuildInfo = bi
379383
}
384+
cid = computeComponentId (package pkg_descr)
385+
(CTestName (testName test))
386+
(map fst (componentPackageDeps clbi))
387+
(flagAssignment lbi)
388+
(pkg_name, compat_key) = computeCompatPackageKey
389+
(compiler lbi) (package pkg_descr)
390+
(CTestName (testName test)) cid
380391
libClbi = LibComponentLocalBuildInfo
381392
{ componentPackageDeps = componentPackageDeps clbi
382393
, componentPackageRenaming = componentPackageRenaming clbi
383-
, componentId = ComponentId $ display (packageId pkg)
384-
, componentCompatPackageKey = ComponentId $ display (packageId pkg)
394+
, componentId = cid
395+
, componentCompatPackageKey = compat_key
385396
, componentExposedModules = [IPI.ExposedModule m Nothing Nothing]
386397
}
387398
pkg = pkg_descr {
388-
package = (package pkg_descr) {
389-
pkgName = PackageName (testName test)
390-
}
399+
package = (package pkg_descr) { pkgName = pkg_name }
391400
, buildDepends = targetBuildDepends $ testBuildInfo test
392401
, executables = []
393402
, testSuites = []
@@ -415,9 +424,7 @@ testSuiteLibV09AsLibAndExe pkg_descr
415424
: (filter (\(_, x) -> let PackageName name = pkgName x
416425
in name == "Cabal" || name == "base")
417426
(componentPackageDeps clbi)),
418-
componentPackageRenaming =
419-
Map.insert (packageName ipi) defaultRenaming
420-
(componentPackageRenaming clbi)
427+
componentPackageRenaming = Map.empty
421428
}
422429
testSuiteLibV09AsLibAndExe _ TestSuite{} _ _ _ _ = error "testSuiteLibV09AsLibAndExe: wrong kind"
423430

@@ -447,7 +454,7 @@ createInternalPackageDB :: Verbosity -> LocalBuildInfo -> FilePath
447454
createInternalPackageDB verbosity lbi distPref = do
448455
existsAlready <- doesPackageDBExist dbPath
449456
when existsAlready $ deletePackageDB dbPath
450-
createPackageDB verbosity (compiler lbi) (withPrograms lbi) True dbPath
457+
createPackageDB verbosity (compiler lbi) (withPrograms lbi) False dbPath
451458
return (SpecificPackageDB dbPath)
452459
where
453460
dbPath = case compilerFlavor (compiler lbi) of

0 commit comments

Comments
 (0)