Skip to content

Commit 811e7d8

Browse files
committed
Apply local configuration to install targets
The target of `cabal install` is not considered to be a local package, which means local configuration (e.g. in cabal.project, or flags like --enable-profiling) does not apply to it. In 76670eb, we changed the behaviour to applying the local flags to cabal install targets, but it used the literal target string as a package name to which the flags were additionally applied. However, `cabal install` targets are NOT necessarily package names, so, e.g., if we did `cabal install exe:mycomp`, the local flags would not apply since "exe:mycomp" is not a recognized /package/. The solution is to parse the target selectors first, and apply the local flags to the package of the resolved targets. Fixes #7297, #8909, the install part of #7236, #8529, #7832
1 parent 0e66a4b commit 811e7d8

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

cabal-install/src/Distribution/Client/CmdInstall.hs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE NamedFieldPuns #-}
33
{-# LANGUAGE RecordWildCards #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE TupleSections #-}
56

67
-- | cabal-install CLI command: build
78
module Distribution.Client.CmdInstall
@@ -104,6 +105,7 @@ import Distribution.Client.Types
104105
, PackageSpecifier (..)
105106
, SourcePackageDb (..)
106107
, UnresolvedSourcePackage
108+
, pkgSpecifierTarget
107109
)
108110
import Distribution.Client.Types.OverwritePolicy
109111
( OverwritePolicy (..)
@@ -371,7 +373,7 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt
371373

372374
-- First, we need to learn about what's available to be installed.
373375
localBaseCtx <-
374-
establishProjectBaseContext reducedVerbosity cliConfig InstallCommand
376+
establishProjectBaseContext reducedVerbosity baseCliConfig InstallCommand
375377
let localDistDirLayout = distDirLayout localBaseCtx
376378
pkgDb <-
377379
projectConfigWithBuilderRepoContext
@@ -432,7 +434,7 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt
432434
withoutProject globalConfig = do
433435
tss <- traverse (parseWithoutProjectTargetSelector verbosity) targetStrings'
434436
let
435-
projectConfig = globalConfig <> cliConfig
437+
projectConfig = globalConfig <> baseCliConfig
436438

437439
ProjectConfigBuildOnly
438440
{ projectConfigLogsDir
@@ -478,10 +480,17 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt
478480

479481
return (packageSpecifiers, uris, packageTargets, projectConfig)
480482

481-
(specs, uris, targetSelectors, config) <-
483+
(specs, uris, targetSelectors, baseConfig) <-
482484
withProjectOrGlobalConfig verbosity ignoreProject globalConfigFlag withProject withoutProject
483485

486+
-- We compute the base context again to determine packages available in the
487+
-- project to be installed, so we can list the available package names when
488+
-- the "all:..." variants of the target selectors are used.
489+
localPkgs <- localPackages <$> establishProjectBaseContext verbosity baseConfig InstallCommand
490+
484491
let
492+
config = addLocalConfigToPkgs baseConfig (map pkgSpecifierTarget specs ++ concatMap (targetPkgNames localPkgs) targetSelectors)
493+
485494
ProjectConfig
486495
{ projectConfigBuildOnly =
487496
ProjectConfigBuildOnly
@@ -631,8 +640,7 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt
631640
globalFlags
632641
flags{configFlags = configFlags'}
633642
clientInstallFlags'
634-
cliConfig = addLocalConfigToTargets baseCliConfig targetStrings
635-
globalConfigFlag = projectConfigConfigFile (projectConfigShared cliConfig)
643+
globalConfigFlag = projectConfigConfigFile (projectConfigShared baseCliConfig)
636644

637645
-- Do the install action for each executable in the install configuration.
638646
traverseInstall :: InstallAction -> InstallCfg -> IO ()
@@ -641,17 +649,35 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt
641649
actionOnExe <- action v overwritePolicy <$> prepareExeInstall cfg
642650
traverse_ actionOnExe . Map.toList $ targetsMap buildCtx
643651

644-
-- | Treat all direct targets of install command as local packages: #8637
645-
addLocalConfigToTargets :: ProjectConfig -> [String] -> ProjectConfig
646-
addLocalConfigToTargets config targetStrings =
652+
-- | Treat all direct targets of install command as local packages: #8637 and later #7297, #8909, #7236.
653+
addLocalConfigToPkgs :: ProjectConfig -> [PackageName] -> ProjectConfig
654+
addLocalConfigToPkgs config pkgs =
647655
config
648656
{ projectConfigSpecificPackage =
649657
projectConfigSpecificPackage config
650658
<> MapMappend (Map.fromList targetPackageConfigs)
651659
}
652660
where
653661
localConfig = projectConfigLocalPackages config
654-
targetPackageConfigs = map (\x -> (mkPackageName x, localConfig)) targetStrings
662+
targetPackageConfigs = map (,localConfig) pkgs
663+
664+
targetPkgNames
665+
:: [PackageSpecifier UnresolvedSourcePackage]
666+
-- ^ The local packages, to resolve 'TargetAllPackages' selectors
667+
-> TargetSelector
668+
-> [PackageName]
669+
targetPkgNames localPkgs = \case
670+
TargetPackage _ pkgIds _ -> map pkgName pkgIds
671+
TargetPackageNamed name _ -> [name]
672+
TargetAllPackages _ -> map pkgSpecifierTarget localPkgs
673+
-- Note how the target may select a component only, but we will always apply
674+
-- the local flags to the whole package in which that component is contained.
675+
-- The reason is that our finest level of configuration is per-package, so
676+
-- there is no interface to configure options to a component only. It is not
677+
-- trivial to say whether we could indeed support per-component configuration
678+
-- because of legacy packages which we may always have to build whole.
679+
TargetComponent pkgId _ _ -> [pkgName pkgId]
680+
TargetComponentUnknown name _ -> [name]
655681

656682
-- | Verify that invalid config options were not passed to the install command.
657683
--
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
#ifdef TEST1
4+
main = putStrLn "hi1"
5+
#endif
6+
7+
#ifdef TEST2
8+
main = putStrLn "hi2"
9+
#endif
10+
11+
#ifdef TEST3
12+
main = putStrLn "hi3"
13+
#endif
14+
15+
#ifdef TEST4
16+
main = putStrLn "hi4"
17+
#endif
18+
19+
#ifdef TEST5
20+
main = putStrLn "hi5"
21+
#endif
22+
23+
#ifdef TEST6
24+
main = putStrLn "hi6"
25+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
env <- getTestEnv
5+
recordMode DoNotRecord $ do
6+
let
7+
installdir = testPrefixDir env </> "bin"
8+
commonOpts v = ["--ghc-options=-DTEST" ++ show v, "--overwrite-policy=always", "--installdir=" ++ installdir]
9+
installWithTgt tgt v = do
10+
cabal "install" (tgt:commonOpts v)
11+
runInstalledExe' "my-exe" []
12+
>>= assertOutputContains ("hi" ++ show v)
13+
14+
cabal "install" (commonOpts 1) -- no target
15+
runInstalledExe' "my-exe" []
16+
>>= assertOutputContains "hi1"
17+
18+
installWithTgt "t7297-89097236a" 2
19+
installWithTgt "exe:my-exe" 3
20+
installWithTgt "my-exe" 4
21+
installWithTgt "all" 5
22+
installWithTgt "all:exes" 6
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: t7297-89097236a
2+
version: 1.0
3+
build-type: Simple
4+
cabal-version: >= 1.2
5+
6+
executable my-exe
7+
main-is: Main.hs
8+
build-depends: base

0 commit comments

Comments
 (0)