Skip to content

Commit 2a52ac2

Browse files
committed
Fix non-buildable bug in Cabal <= 1.22
1 parent 3799830 commit 2a52ac2

File tree

7 files changed

+79
-5
lines changed

7 files changed

+79
-5
lines changed

ChangeLog.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## Unreleased changes
4+
5+
Release notes:
6+
7+
Major changes:
8+
9+
Behaviour changes:
10+
11+
Other enhancements:
12+
13+
Bug fixes:
14+
15+
* For versions of Cabal before 1.24, ensure that the dependencies of
16+
non-buildable components are part of the build plan to work around an old
17+
Cabal bug.
18+
319
## v1.6.1
420

521
Major changes:

src/Stack/BuildPlan.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ gpdPackageDeps
198198
-> Map FlagName Bool
199199
-> Map PackageName VersionRange
200200
gpdPackageDeps gpd cv platform flags =
201-
Map.filterWithKey (const . (/= name)) (packageDependencies pkgDesc)
201+
Map.filterWithKey (const . (/= name)) (packageDependencies cv pkgDesc)
202202
where
203203
name = gpdPackageName gpd
204204
-- Since tests and benchmarks are both enabled, doesn't matter

src/Stack/Package.hs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ packageFromPackageDescription packageConfig pkgFlags (PackageDescriptionPair pkg
356356
pkgId = package pkg
357357
name = fromCabalPackageName (pkgName pkgId)
358358
deps = M.filterWithKey (const . not . isMe) (M.union
359-
(packageDependencies pkg)
359+
(packageDependencies (packageConfigCompilerVersion packageConfig) pkg)
360360
-- We include all custom-setup deps - if present - in the
361361
-- package deps themselves. Stack always works with the
362362
-- invariant that there will be a single installed package
@@ -602,12 +602,36 @@ getBuildComponentDir Nothing = Nothing
602602
getBuildComponentDir (Just name) = parseRelDir (name FilePath.</> (name ++ "-tmp"))
603603

604604
-- | Get all dependencies of the package (buildable targets only).
605-
packageDependencies :: PackageDescription -> Map PackageName VersionRange
606-
packageDependencies pkg =
605+
--
606+
-- Note that for Cabal versions 1.22 and earlier, there is a bug where
607+
-- Cabal requires dependencies for non-buildable components to be
608+
-- present. We're going to use GHC version as a proxy for Cabal
609+
-- library version in this case for simplicity, so we'll check for GHC
610+
-- being 7.10 or earlier. This obviously makes our function a lot more
611+
-- fun to write...
612+
packageDependencies
613+
:: CompilerVersion 'CVActual
614+
-> PackageDescription
615+
-> Map PackageName VersionRange
616+
packageDependencies ghcVersion pkg' =
607617
M.fromListWith intersectVersionRanges $
608618
map (depName &&& depRange) $
609619
concatMap targetBuildDepends (allBuildInfo' pkg) ++
610620
maybe [] setupDepends (setupBuildInfo pkg)
621+
where
622+
pkg
623+
| getGhcVersion ghcVersion >= $(mkVersion "8.0") = pkg'
624+
-- Set all components to buildable. Only need to worry about
625+
-- library, exe, test, and bench, since others didn't exist in
626+
-- older Cabal versions
627+
| otherwise = pkg'
628+
{ library = (\c -> c { libBuildInfo = go (libBuildInfo c) }) <$> library pkg'
629+
, executables = (\c -> c { buildInfo = go (buildInfo c) }) <$> executables pkg'
630+
, testSuites = (\c -> c { testBuildInfo = go (testBuildInfo c) }) <$> testSuites pkg'
631+
, benchmarks = (\c -> c { benchmarkBuildInfo = go (benchmarkBuildInfo c) }) <$> benchmarks pkg'
632+
}
633+
634+
go bi = bi { buildable = True }
611635

612636
-- | Get all dependencies of the package (buildable targets only).
613637
--

src/Stack/Snapshot.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ calculate gpd platform compilerVersion loc flags hide options =
769769
, lpiGhcOptions = options
770770
, lpiPackageDeps = Map.map fromVersionRange
771771
$ Map.filterWithKey (const . (/= name))
772-
$ packageDependencies pd
772+
$ packageDependencies compilerVersion pd
773773
, lpiProvidedExes =
774774
Set.fromList
775775
$ map (ExeName . T.pack . C.unUnqualComponentName . C.exeName)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import StackTest
2+
3+
main :: IO ()
4+
main = do
5+
-- Newer Cabal: dry run and building should succeed, because they'll
6+
-- both ignore the do-not-build
7+
writeFile "stack.yaml" "resolver: ghc-8.0.2"
8+
stack ["build", "--dry-run"]
9+
stack ["build"]
10+
11+
-- Older Cabal: both should fail, because they'll both try to
12+
-- include the non-buildable component. If there's a regression, the
13+
-- dry run will succeed (because Stack will use the proper logic)
14+
-- and build will fail (because Cabal will be using its broken
15+
-- logic).
16+
writeFile "stack.yaml" "resolver: ghc-7.10.3"
17+
stackErr ["build"]
18+
stackErr ["build", "--dry-run"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stack.yaml
2+
foo.cabal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: foo
2+
version: "0"
3+
4+
dependencies:
5+
- base
6+
7+
library: {}
8+
9+
executables:
10+
not-built:
11+
main: Main.hs
12+
dependencies:
13+
- does-not-exist
14+
buildable: false

0 commit comments

Comments
 (0)