Skip to content

Commit 129946d

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 32d9319 commit 129946d

File tree

6 files changed

+89
-9
lines changed

6 files changed

+89
-9
lines changed

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

Lines changed: 29 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,29 @@ 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+
TargetComponent pkgId _ -> [pkgName pkgId]
674+
TargetComponentUnknown name _ -> [name]
655675

656676
-- | Verify that invalid config options were not passed to the install command.
657677
--
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
#ifdef HELLO
4+
main = putStrLn "hi"
5+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# cabal install
2+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
3+
Resolving dependencies...
4+
Build profile: -w ghc-<GHCVER> -O1
5+
In order, the following will be built:
6+
- t7297-89097236a-1.0 (exe:my-exe) (requires build)
7+
Configuring t7297-89097236a-1.0...
8+
Preprocessing executable 'my-exe' for t7297-89097236a-1.0...
9+
Building executable 'my-exe' for t7297-89097236a-1.0...
10+
Installing executable my-exe in <PATH>
11+
Warning: The directory <GBLTMPDIR>/ghc-<GHCVER>/incoming/new-<RAND><GBLTMPDIR>/ghc-<GHCVER>/<PACKAGE>-<HASH>/bin is not in the system search path.
12+
Symlinking 'my-exe' to '<GBLTMPDIR>/my-exe'
13+
# cabal install
14+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
15+
Resolving dependencies...
16+
Symlinking 'my-exe' to '<GBLTMPDIR>/my-exe'
17+
# cabal install
18+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
19+
Resolving dependencies...
20+
Symlinking 'my-exe' to '<GBLTMPDIR>/my-exe'
21+
# cabal install
22+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
23+
Resolving dependencies...
24+
Symlinking 'my-exe' to '<GBLTMPDIR>/my-exe'
25+
# cabal install
26+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
27+
Resolving dependencies...
28+
Symlinking 'my-exe' to '<GBLTMPDIR>/my-exe'
29+
# cabal install
30+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
31+
Resolving dependencies...
32+
Symlinking 'my-exe' to '<GBLTMPDIR>/my-exe'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Test.Cabal.Prelude
2+
3+
main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ do
4+
let
5+
cabalOpts = [ "--store-dir=" ++ storeDir, "--installdir=" ++ storeDir]
6+
commonOpts = ["--ghc-options=-DHELLO", "--overwrite-policy=always"]
7+
installWithTgt tgt = cabalG cabalOpts "install" (tgt:commonOpts)
8+
9+
cabalG cabalOpts "install" commonOpts -- no target
10+
installWithTgt "t7297-89097236a"
11+
installWithTgt "exe:my-exe"
12+
installWithTgt "my-exe"
13+
installWithTgt "all"
14+
installWithTgt "all:exes"
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)