Skip to content

Commit 547692a

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.
1 parent 32d9319 commit 547692a

File tree

6 files changed

+97
-9
lines changed

6 files changed

+97
-9
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE TupleSections #-}
12
{-# LANGUAGE LambdaCase #-}
23
{-# LANGUAGE NamedFieldPuns #-}
34
{-# LANGUAGE RecordWildCards #-}
@@ -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,27 @@ 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 :: [PackageSpecifier UnresolvedSourcePackage]
665+
-- ^ The local packages, to resolve 'TargetAllPackages' selectors
666+
-> TargetSelector -> [PackageName]
667+
targetPkgNames localPkgs = \case
668+
TargetPackage _ pkgIds _ -> map pkgName pkgIds
669+
TargetPackageNamed name _ -> [name]
670+
TargetAllPackages _ -> map pkgSpecifierTarget localPkgs
671+
TargetComponent pkgId _ -> [pkgName pkgId]
672+
TargetComponentUnknown name _ -> [name]
655673

656674
-- | Verify that invalid config options were not passed to the install command.
657675
--
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
8+
Configuring t7297-89097236a-1.0...
9+
Preprocessing executable 'my-exe' for t7297-89097236a-1.0...
10+
Building executable 'my-exe' for t7297-89097236a-1.0...
11+
Installing executable my-exe in <PATH>
12+
Warning: The directory <ROOT>/cabal.dist/home/.cabal/store/ghc-<GHCVER>/incoming/new-<RAND><ROOT>/cabal.dist/home/.cabal/store/ghc-<GHCVER>/<PACKAGE>-<HASH>/bin is not in the system search path.
13+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
14+
Symlinking 'my-exe' to '<ROOT>/cabal.dist/home/.cabal/bin/my-exe'
15+
# cabal install
16+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
17+
Resolving dependencies...
18+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
19+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
20+
Symlinking 'my-exe' to '<ROOT>/cabal.dist/home/.cabal/bin/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+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
25+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
26+
Symlinking 'my-exe' to '<ROOT>/cabal.dist/home/.cabal/bin/my-exe'
27+
# cabal install
28+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
29+
Resolving dependencies...
30+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
31+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
32+
Symlinking 'my-exe' to '<ROOT>/cabal.dist/home/.cabal/bin/my-exe'
33+
# cabal install
34+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
35+
Resolving dependencies...
36+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
37+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
38+
Symlinking 'my-exe' to '<ROOT>/cabal.dist/home/.cabal/bin/my-exe'
39+
# cabal install
40+
Wrote tarball sdist to <ROOT>/cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz
41+
Resolving dependencies...
42+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
43+
Warning: installdir is not defined. Set it in your cabal config file or use --installdir=<path>. Using default installdir: "<ROOT>/cabal.dist/home/.cabal/bin"
44+
Symlinking 'my-exe' to '<ROOT>/cabal.dist/home/.cabal/bin/my-exe'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
let commonOpts = ["--ghc-options=-DHELLO", "--overwrite-policy=always"]
5+
installWithTgt tgt = cabal "install" (tgt:commonOpts)
6+
7+
cabal "install" commonOpts -- no target
8+
installWithTgt "t7297-89097236a"
9+
installWithTgt "exe:my-exe"
10+
installWithTgt "my-exe"
11+
installWithTgt "all"
12+
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)