From 80312d7b339bea6e824b273c24e3b92b9b1cb27d Mon Sep 17 00:00:00 2001 From: Rodrigo Mesquita Date: Mon, 5 Feb 2024 16:08:46 +0000 Subject: [PATCH] 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 76670ebddeb0a914d816f4420c511c1bc1f044eb, 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. --- .../src/Distribution/Client/CmdInstall.hs | 38 ++++++++++++---- .../Install/T7297-8909-7236/Main.hs | 5 +++ .../Install/T7297-8909-7236/cabal.out | 44 +++++++++++++++++++ .../Install/T7297-8909-7236/cabal.project | 1 + .../Install/T7297-8909-7236/cabal.test.hs | 12 +++++ .../T7297-8909-7236/t7297-89097236a.cabal | 8 ++++ 6 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 cabal-testsuite/PackageTests/Install/T7297-8909-7236/Main.hs create mode 100644 cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.out create mode 100644 cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.project create mode 100644 cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.test.hs create mode 100644 cabal-testsuite/PackageTests/Install/T7297-8909-7236/t7297-89097236a.cabal diff --git a/cabal-install/src/Distribution/Client/CmdInstall.hs b/cabal-install/src/Distribution/Client/CmdInstall.hs index 0adeca99446..36fc4d10bfa 100644 --- a/cabal-install/src/Distribution/Client/CmdInstall.hs +++ b/cabal-install/src/Distribution/Client/CmdInstall.hs @@ -2,6 +2,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} -- | cabal-install CLI command: build module Distribution.Client.CmdInstall @@ -104,6 +105,7 @@ import Distribution.Client.Types , PackageSpecifier (..) , SourcePackageDb (..) , UnresolvedSourcePackage + , pkgSpecifierTarget ) import Distribution.Client.Types.OverwritePolicy ( OverwritePolicy (..) @@ -371,7 +373,7 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt -- First, we need to learn about what's available to be installed. localBaseCtx <- - establishProjectBaseContext reducedVerbosity cliConfig InstallCommand + establishProjectBaseContext reducedVerbosity baseCliConfig InstallCommand let localDistDirLayout = distDirLayout localBaseCtx pkgDb <- projectConfigWithBuilderRepoContext @@ -432,7 +434,7 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt withoutProject globalConfig = do tss <- traverse (parseWithoutProjectTargetSelector verbosity) targetStrings' let - projectConfig = globalConfig <> cliConfig + projectConfig = globalConfig <> baseCliConfig ProjectConfigBuildOnly { projectConfigLogsDir @@ -478,10 +480,17 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt return (packageSpecifiers, uris, packageTargets, projectConfig) - (specs, uris, targetSelectors, config) <- + (specs, uris, targetSelectors, baseConfig) <- withProjectOrGlobalConfig verbosity ignoreProject globalConfigFlag withProject withoutProject + -- We compute the base context again to determine packages available in the + -- project to be installed, so we can list the available package names when + -- the "all:..." variants of the target selectors are used. + localPkgs <- localPackages <$> establishProjectBaseContext verbosity baseConfig InstallCommand + let + config = addLocalConfigToPkgs baseConfig (map pkgSpecifierTarget specs ++ concatMap (targetPkgNames localPkgs) targetSelectors) + ProjectConfig { projectConfigBuildOnly = ProjectConfigBuildOnly @@ -631,8 +640,7 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt globalFlags flags{configFlags = configFlags'} clientInstallFlags' - cliConfig = addLocalConfigToTargets baseCliConfig targetStrings - globalConfigFlag = projectConfigConfigFile (projectConfigShared cliConfig) + globalConfigFlag = projectConfigConfigFile (projectConfigShared baseCliConfig) -- Do the install action for each executable in the install configuration. traverseInstall :: InstallAction -> InstallCfg -> IO () @@ -641,9 +649,9 @@ installAction flags@NixStyleFlags{extraFlags = clientInstallFlags', ..} targetSt actionOnExe <- action v overwritePolicy <$> prepareExeInstall cfg traverse_ actionOnExe . Map.toList $ targetsMap buildCtx --- | Treat all direct targets of install command as local packages: #8637 -addLocalConfigToTargets :: ProjectConfig -> [String] -> ProjectConfig -addLocalConfigToTargets config targetStrings = +-- | Treat all direct targets of install command as local packages: #8637 and later #7297, #8909, #7236. +addLocalConfigToPkgs :: ProjectConfig -> [PackageName] -> ProjectConfig +addLocalConfigToPkgs config pkgs = config { projectConfigSpecificPackage = projectConfigSpecificPackage config @@ -651,7 +659,19 @@ addLocalConfigToTargets config targetStrings = } where localConfig = projectConfigLocalPackages config - targetPackageConfigs = map (\x -> (mkPackageName x, localConfig)) targetStrings + targetPackageConfigs = map (,localConfig) pkgs + +targetPkgNames + :: [PackageSpecifier UnresolvedSourcePackage] + -- ^ The local packages, to resolve 'TargetAllPackages' selectors + -> TargetSelector + -> [PackageName] +targetPkgNames localPkgs = \case + TargetPackage _ pkgIds _ -> map pkgName pkgIds + TargetPackageNamed name _ -> [name] + TargetAllPackages _ -> map pkgSpecifierTarget localPkgs + TargetComponent pkgId _ -> [pkgName pkgId] + TargetComponentUnknown name _ -> [name] -- | Verify that invalid config options were not passed to the install command. -- diff --git a/cabal-testsuite/PackageTests/Install/T7297-8909-7236/Main.hs b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/Main.hs new file mode 100644 index 00000000000..2de6b7b008d --- /dev/null +++ b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/Main.hs @@ -0,0 +1,5 @@ +{-# LANGUAGE CPP #-} + +#ifdef HELLO +main = putStrLn "hi" +#endif diff --git a/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.out b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.out new file mode 100644 index 00000000000..faeab934292 --- /dev/null +++ b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.out @@ -0,0 +1,44 @@ +# cabal install +Wrote tarball sdist to /cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz +Resolving dependencies... +Build profile: -w ghc- -O1 +In order, the following will be built: + - t7297-89097236a-1.0 (exe:my-exe) (requires build) +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Configuring t7297-89097236a-1.0... +Preprocessing executable 'my-exe' for t7297-89097236a-1.0... +Building executable 'my-exe' for t7297-89097236a-1.0... +Installing executable my-exe in +Warning: The directory /cabal.dist/home/.cabal/store/ghc-/incoming/new-/cabal.dist/home/.cabal/store/ghc-/-/bin is not in the system search path. +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Symlinking 'my-exe' to '/cabal.dist/home/.cabal/bin/my-exe' +# cabal install +Wrote tarball sdist to /cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz +Resolving dependencies... +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Symlinking 'my-exe' to '/cabal.dist/home/.cabal/bin/my-exe' +# cabal install +Wrote tarball sdist to /cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz +Resolving dependencies... +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Symlinking 'my-exe' to '/cabal.dist/home/.cabal/bin/my-exe' +# cabal install +Wrote tarball sdist to /cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz +Resolving dependencies... +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Symlinking 'my-exe' to '/cabal.dist/home/.cabal/bin/my-exe' +# cabal install +Wrote tarball sdist to /cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz +Resolving dependencies... +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Symlinking 'my-exe' to '/cabal.dist/home/.cabal/bin/my-exe' +# cabal install +Wrote tarball sdist to /cabal.dist/work/./dist/sdist/t7297-89097236a-1.0.tar.gz +Resolving dependencies... +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Warning: installdir is not defined. Set it in your cabal config file or use --installdir=. Using default installdir: "/cabal.dist/home/.cabal/bin" +Symlinking 'my-exe' to '/cabal.dist/home/.cabal/bin/my-exe' diff --git a/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.project b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.test.hs b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.test.hs new file mode 100644 index 00000000000..528fee74883 --- /dev/null +++ b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/cabal.test.hs @@ -0,0 +1,12 @@ +import Test.Cabal.Prelude + +main = cabalTest $ do + let commonOpts = ["--ghc-options=-DHELLO", "--overwrite-policy=always"] + installWithTgt tgt = cabal "install" (tgt:commonOpts) + + cabal "install" commonOpts -- no target + installWithTgt "t7297-89097236a" + installWithTgt "exe:my-exe" + installWithTgt "my-exe" + installWithTgt "all" + installWithTgt "all:exes" diff --git a/cabal-testsuite/PackageTests/Install/T7297-8909-7236/t7297-89097236a.cabal b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/t7297-89097236a.cabal new file mode 100644 index 00000000000..84ca78363c7 --- /dev/null +++ b/cabal-testsuite/PackageTests/Install/T7297-8909-7236/t7297-89097236a.cabal @@ -0,0 +1,8 @@ +name: t7297-89097236a +version: 1.0 +build-type: Simple +cabal-version: >= 1.2 + +executable my-exe + main-is: Main.hs + build-depends: base