From 12c2bddb7ec59198c2a44ef48cc8a400aaaa01f1 Mon Sep 17 00:00:00 2001 From: Rodrigo Mesquita Date: Fri, 1 Mar 2024 14:39:32 +0000 Subject: [PATCH] Find build-tool installed programs before programs in path We must consider the path to the installed build-tool before the path to existing versions of the build tool in paths such as `extra-prog-path` or in the system path. This was previously fixed by #8972 but undone by #9527. This also renames `appendProgramSearchPath` to `prependProgramSearchPath` to describe correctly what that function does. Fixes #9756 --- Cabal/src/Distribution/Simple/Configure.hs | 8 +++--- .../Distribution/Simple/ConfigureScript.hs | 2 +- Cabal/src/Distribution/Simple/Program/Db.hs | 8 +++--- .../src/Distribution/Simple/Program/Types.hs | 2 +- .../src/Distribution/Client/CmdExec.hs | 6 ++--- .../src/Distribution/Client/CmdInstall.hs | 6 ++--- .../src/Distribution/Client/HttpUtils.hs | 6 ++--- .../Distribution/Client/ProjectPlanning.hs | 2 +- .../src/Distribution/Client/SetupWrapper.hs | 4 +-- cabal-install/src/Distribution/Client/VCS.hs | 4 +-- .../PackageTests/Regression/T9756/OK.hs | 14 ++++++++++ .../T9756/cabal-bug-build-tool.cabal | 10 +++++++ .../PackageTests/Regression/T9756/cabal.out | 27 +++++++++++++++++++ .../Regression/T9756/cabal.project | 1 + .../Regression/T9756/cabal.test.hs | 13 +++++++++ .../T9756/repo/mybuilder-0.1.0.0/CHANGELOG.md | 5 ++++ .../T9756/repo/mybuilder-0.1.0.0/app/Main.hs | 4 +++ .../repo/mybuilder-0.1.0.0/mybuilder.cabal | 18 +++++++++++++ .../T9756/repo/mybuilder-0.2.0.0/CHANGELOG.md | 5 ++++ .../T9756/repo/mybuilder-0.2.0.0/app/Main.hs | 4 +++ .../repo/mybuilder-0.2.0.0/mybuilder.cabal | 18 +++++++++++++ 21 files changed, 144 insertions(+), 23 deletions(-) create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/OK.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/cabal-bug-build-tool.cabal create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/cabal.out create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/cabal.project create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/cabal.test.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/CHANGELOG.md create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/app/Main.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/mybuilder.cabal create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/CHANGELOG.md create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/app/Main.hs create mode 100644 cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/mybuilder.cabal diff --git a/Cabal/src/Distribution/Simple/Configure.hs b/Cabal/src/Distribution/Simple/Configure.hs index 1cf7214f035..589814a91fc 100644 --- a/Cabal/src/Distribution/Simple/Configure.hs +++ b/Cabal/src/Distribution/Simple/Configure.hs @@ -85,7 +85,7 @@ import Distribution.Simple.PackageIndex (InstalledPackageIndex, lookupUnitId) import qualified Distribution.Simple.PackageIndex as PackageIndex import Distribution.Simple.PreProcess import Distribution.Simple.Program -import Distribution.Simple.Program.Db (appendProgramSearchPath, lookupProgramByName) +import Distribution.Simple.Program.Db (lookupProgramByName, modifyProgramSearchPath, prependProgramSearchPath) import Distribution.Simple.Setup.Common as Setup import Distribution.Simple.Setup.Config as Setup import Distribution.Simple.Utils @@ -1236,13 +1236,15 @@ mkPromisedDepsSet comps = Map.fromList [((pn, CLibName ln), cid) | GivenComponen -- arguments. mkProgramDb :: ConfigFlags -> ProgramDb -> IO ProgramDb mkProgramDb cfg initialProgramDb = do - programDb <- appendProgramSearchPath (fromFlagOrDefault normal (configVerbosity cfg)) searchpath initialProgramDb + programDb <- + modifyProgramSearchPath (getProgramSearchPath initialProgramDb ++) + <$> prependProgramSearchPath (fromFlagOrDefault normal (configVerbosity cfg)) searchpath initialProgramDb pure . userSpecifyArgss (configProgramArgs cfg) . userSpecifyPaths (configProgramPaths cfg) $ programDb where - searchpath = fromNubList $ configProgramPathExtra cfg + searchpath = fromNubList (configProgramPathExtra cfg) -- Note. We try as much as possible to _prepend_ rather than postpend the extra-prog-path -- so that we can override the system path. However, in a v2-build, at this point, the "system" path diff --git a/Cabal/src/Distribution/Simple/ConfigureScript.hs b/Cabal/src/Distribution/Simple/ConfigureScript.hs index 2572f4949c1..b7a7f16da25 100644 --- a/Cabal/src/Distribution/Simple/ConfigureScript.hs +++ b/Cabal/src/Distribution/Simple/ConfigureScript.hs @@ -169,7 +169,7 @@ runConfigureScript verbosity flags lbi = do maybeHostFlag = if hp == buildPlatform then [] else ["--host=" ++ show (pretty hp)] args' = configureFile' : args ++ ["CC=" ++ ccProgShort] ++ maybeHostFlag shProg = simpleProgram "sh" - progDb <- appendProgramSearchPath verbosity extraPath emptyProgramDb + progDb <- prependProgramSearchPath verbosity extraPath emptyProgramDb shConfiguredProg <- lookupProgram shProg `fmap` configureProgram verbosity shProg progDb diff --git a/Cabal/src/Distribution/Simple/Program/Db.hs b/Cabal/src/Distribution/Simple/Program/Db.hs index 4b02a7e6f2e..a5e4e4ab381 100644 --- a/Cabal/src/Distribution/Simple/Program/Db.hs +++ b/Cabal/src/Distribution/Simple/Program/Db.hs @@ -34,7 +34,7 @@ module Distribution.Simple.Program.Db -- ** Query and manipulate the program db , addKnownProgram , addKnownPrograms - , appendProgramSearchPath + , prependProgramSearchPath , lookupKnownProgram , knownPrograms , getProgramSearchPath @@ -223,14 +223,14 @@ modifyProgramSearchPath f db = setProgramSearchPath (f $ getProgramSearchPath db) db -- | Modify the current 'ProgramSearchPath' used by the 'ProgramDb' --- by appending the provided extra paths. Also logs the added paths +-- by prepending the provided extra paths. Also logs the added paths -- in info verbosity. -appendProgramSearchPath +prependProgramSearchPath :: Verbosity -> [FilePath] -> ProgramDb -> IO ProgramDb -appendProgramSearchPath verbosity extraPaths db = +prependProgramSearchPath verbosity extraPaths db = if not $ null extraPaths then do logExtraProgramSearchPath verbosity extraPaths diff --git a/Cabal/src/Distribution/Simple/Program/Types.hs b/Cabal/src/Distribution/Simple/Program/Types.hs index f1b42f63853..53105b6c9a3 100644 --- a/Cabal/src/Distribution/Simple/Program/Types.hs +++ b/Cabal/src/Distribution/Simple/Program/Types.hs @@ -97,7 +97,7 @@ type ProgArg = String -- We also use this path to set the environment when running child processes. -- -- The @ProgramDb@ is created with a @ProgramSearchPath@ to which we --- @appendProgramSearchPath@ to add the ones that come from cli flags and from +-- @prependProgramSearchPath@ to add the ones that come from cli flags and from -- configurations. Then each of the programs that are configured in the db -- inherits the same path as part of @configureProgram@. type ProgramSearchPath = [ProgramSearchPathEntry] diff --git a/cabal-install/src/Distribution/Client/CmdExec.hs b/cabal-install/src/Distribution/Client/CmdExec.hs index 82fada98463..fc81f323fdc 100644 --- a/cabal-install/src/Distribution/Client/CmdExec.hs +++ b/cabal-install/src/Distribution/Client/CmdExec.hs @@ -76,8 +76,8 @@ import Distribution.Simple.Program , simpleProgram ) import Distribution.Simple.Program.Db - ( appendProgramSearchPath - , configuredPrograms + ( configuredPrograms + , prependProgramSearchPath , requireProgram ) import Distribution.Simple.Program.Run @@ -168,7 +168,7 @@ execAction flags@NixStyleFlags{..} extraArgs globalFlags = do let extraPaths = pathAdditions baseCtx buildCtx programDb <- - appendProgramSearchPath + prependProgramSearchPath verbosity extraPaths . pkgConfigCompilerProgs diff --git a/cabal-install/src/Distribution/Client/CmdInstall.hs b/cabal-install/src/Distribution/Client/CmdInstall.hs index 7b89b0ac302..097a5488ccc 100644 --- a/cabal-install/src/Distribution/Client/CmdInstall.hs +++ b/cabal-install/src/Distribution/Client/CmdInstall.hs @@ -154,8 +154,8 @@ import Distribution.Simple.GHC import qualified Distribution.Simple.InstallDirs as InstallDirs import qualified Distribution.Simple.PackageIndex as PI import Distribution.Simple.Program.Db - ( appendProgramSearchPath - , defaultProgramDb + ( defaultProgramDb + , prependProgramSearchPath , userSpecifyArgss , userSpecifyPaths ) @@ -426,7 +426,7 @@ installAction flags@NixStyleFlags{extraFlags, configFlags, installFlags, project hcPath = flagToMaybe projectConfigHcPath hcPkg = flagToMaybe projectConfigHcPkg - configProgDb <- appendProgramSearchPath verbosity ((fromNubList packageConfigProgramPathExtra) ++ (fromNubList projectConfigProgPathExtra)) defaultProgramDb + configProgDb <- prependProgramSearchPath verbosity ((fromNubList packageConfigProgramPathExtra) ++ (fromNubList projectConfigProgPathExtra)) defaultProgramDb let -- ProgramDb with directly user specified paths preProgDb = diff --git a/cabal-install/src/Distribution/Client/HttpUtils.hs b/cabal-install/src/Distribution/Client/HttpUtils.hs index 39bd264c744..72ba34d9f9d 100644 --- a/cabal-install/src/Distribution/Client/HttpUtils.hs +++ b/cabal-install/src/Distribution/Client/HttpUtils.hs @@ -46,10 +46,10 @@ import Distribution.Simple.Program import Distribution.Simple.Program.Db ( ProgramDb , addKnownPrograms - , appendProgramSearchPath , configureAllKnownPrograms , emptyProgramDb , lookupProgram + , prependProgramSearchPath , requireProgram ) import Distribution.Simple.Program.Run @@ -408,7 +408,7 @@ configureTransport verbosity extraPath (Just name) = case find (\(name', _, _, _) -> name' == name) supportedTransports of Just (_, mprog, _tls, mkTrans) -> do - baseProgDb <- appendProgramSearchPath verbosity extraPath emptyProgramDb + baseProgDb <- prependProgramSearchPath verbosity extraPath emptyProgramDb progdb <- case mprog of Nothing -> return emptyProgramDb Just prog -> snd <$> requireProgram verbosity prog baseProgDb @@ -424,7 +424,7 @@ configureTransport verbosity extraPath Nothing = do -- for all the transports except plain-http we need to try and find -- their external executable - baseProgDb <- appendProgramSearchPath verbosity extraPath emptyProgramDb + baseProgDb <- prependProgramSearchPath verbosity extraPath emptyProgramDb progdb <- configureAllKnownPrograms verbosity $ addKnownPrograms diff --git a/cabal-install/src/Distribution/Client/ProjectPlanning.hs b/cabal-install/src/Distribution/Client/ProjectPlanning.hs index f779001dc91..6344249a8a6 100644 --- a/cabal-install/src/Distribution/Client/ProjectPlanning.hs +++ b/cabal-install/src/Distribution/Client/ProjectPlanning.hs @@ -471,7 +471,7 @@ configureCompiler ) $ do liftIO $ info verbosity "Compiler settings changed, reconfiguring..." - progdb <- liftIO $ appendProgramSearchPath verbosity (fromNubList packageConfigProgramPathExtra) defaultProgramDb + progdb <- liftIO $ prependProgramSearchPath verbosity (fromNubList packageConfigProgramPathExtra) defaultProgramDb let progdb' = userSpecifyPaths (Map.toList (getMapLast packageConfigProgramPaths)) progdb result@(_, _, progdb'') <- liftIO $ diff --git a/cabal-install/src/Distribution/Client/SetupWrapper.hs b/cabal-install/src/Distribution/Client/SetupWrapper.hs index eba9b833d9d..b3174c96751 100644 --- a/cabal-install/src/Distribution/Client/SetupWrapper.hs +++ b/cabal-install/src/Distribution/Client/SetupWrapper.hs @@ -89,7 +89,7 @@ import Distribution.Simple.Program , runDbProgram ) import Distribution.Simple.Program.Db - ( appendProgramSearchPath + ( prependProgramSearchPath ) import Distribution.Simple.Program.Find ( programSearchPathAsPATHVar @@ -539,7 +539,7 @@ invoke verbosity path args options = do Nothing -> return () Just logHandle -> info verbosity $ "Redirecting build log to " ++ show logHandle - progDb <- appendProgramSearchPath verbosity (useExtraPathEnv options) (useProgramDb options) + progDb <- prependProgramSearchPath verbosity (useExtraPathEnv options) (useProgramDb options) searchpath <- programSearchPathAsPATHVar $ getProgramSearchPath progDb diff --git a/cabal-install/src/Distribution/Client/VCS.hs b/cabal-install/src/Distribution/Client/VCS.hs index f3403029827..7c071de31eb 100644 --- a/cabal-install/src/Distribution/Client/VCS.hs +++ b/cabal-install/src/Distribution/Client/VCS.hs @@ -62,7 +62,7 @@ import Distribution.Simple.Program , simpleProgram ) import Distribution.Simple.Program.Db - ( appendProgramSearchPath + ( prependProgramSearchPath ) import Distribution.Types.SourceRepo ( KnownRepoType (..) @@ -206,7 +206,7 @@ configureVCS -> VCS Program -> IO (VCS ConfiguredProgram) configureVCS verbosity progPaths vcs@VCS{vcsProgram = prog} = do - progPath <- appendProgramSearchPath verbosity progPaths emptyProgramDb + progPath <- prependProgramSearchPath verbosity progPaths emptyProgramDb asVcsConfigured <$> requireProgram verbosity prog progPath where asVcsConfigured (prog', _) = vcs{vcsProgram = prog'} diff --git a/cabal-testsuite/PackageTests/Regression/T9756/OK.hs b/cabal-testsuite/PackageTests/Regression/T9756/OK.hs new file mode 100644 index 00000000000..1846f8c9d0e --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/OK.hs @@ -0,0 +1,14 @@ +{-# LANGUAGE TemplateHaskell #-} +module OK where + +import Data.List +import System.Process +import Language.Haskell.TH + +$(do + out <- runIO $ readProcess "mybuilder" [] "" + if "0.2.0.0" `isInfixOf` out then + [d| x = () |] + else + error ("Expecting Version 0.2.0.0, but got: " ++ out) + ) diff --git a/cabal-testsuite/PackageTests/Regression/T9756/cabal-bug-build-tool.cabal b/cabal-testsuite/PackageTests/Regression/T9756/cabal-bug-build-tool.cabal new file mode 100644 index 00000000000..941ac8fbca6 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/cabal-bug-build-tool.cabal @@ -0,0 +1,10 @@ +cabal-version: 1.12 +name: cabal-bug-build-tool +version: 0 +build-type: Simple + +library + exposed-modules: OK + default-language: Haskell2010 + build-depends: base, template-haskell, process + build-tool-depends: mybuilder:mybuilder >=0.2.0.0 diff --git a/cabal-testsuite/PackageTests/Regression/T9756/cabal.out b/cabal-testsuite/PackageTests/Regression/T9756/cabal.out new file mode 100644 index 00000000000..61a814acd68 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/cabal.out @@ -0,0 +1,27 @@ +# cabal v2-update +Downloading the latest package list from test-local-repo +# cabal v2-install +Resolving dependencies... +Build profile: -w ghc- -O1 +In order, the following will be built: + - mybuilder-0.1.0.0 (exe:mybuilder) (requires build) +Configuring executable 'mybuilder' for mybuilder-0.1.0.0... +Preprocessing executable 'mybuilder' for mybuilder-0.1.0.0... +Building executable 'mybuilder' for mybuilder-0.1.0.0... +Installing executable mybuilder 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. +Symlinking 'mybuilder' to '/cabal.dist/install/mybuilder' +# cabal v2-build +Resolving dependencies... +Build profile: -w ghc- -O1 +In order, the following will be built: + - mybuilder-0.2.0.0 (exe:mybuilder) (requires build) + - cabal-bug-build-tool-0 (lib) (first run) +Configuring executable 'mybuilder' for mybuilder-0.2.0.0... +Preprocessing executable 'mybuilder' for mybuilder-0.2.0.0... +Building executable 'mybuilder' for mybuilder-0.2.0.0... +Installing executable mybuilder 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. +Configuring library for cabal-bug-build-tool-0... +Preprocessing library for cabal-bug-build-tool-0... +Building library for cabal-bug-build-tool-0... diff --git a/cabal-testsuite/PackageTests/Regression/T9756/cabal.project b/cabal-testsuite/PackageTests/Regression/T9756/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/Regression/T9756/cabal.test.hs b/cabal-testsuite/PackageTests/Regression/T9756/cabal.test.hs new file mode 100644 index 00000000000..65b1acb5b70 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/cabal.test.hs @@ -0,0 +1,13 @@ +import Test.Cabal.Prelude + +-- We are testing if the build-tools program is found in path before programs e.g. in extra-prog-path or the system path +-- For that, we need +-- * A repo with a build tool that is up to date +-- * An older version of the build tool in the extra-prog-path +-- * A project that requires the more up-to-date version of the build-tool + +main = cabalTest $ withRepo "repo" $ do + dir <- testWorkDir <$> getTestEnv + cabal "v2-install" ["mybuilder-0.1.0.0", "--installdir=" ++ dir ++ "/install", "--overwrite-policy=always"] + cabal "v2-build" ["cabal-bug-build-tool", "--extra-prog-path=" ++ dir ++ "/install"] + diff --git a/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/CHANGELOG.md b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/CHANGELOG.md new file mode 100644 index 00000000000..60a3351b163 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for mybuilder0100 + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/app/Main.hs b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/app/Main.hs new file mode 100644 index 00000000000..7de3bfe00bb --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/app/Main.hs @@ -0,0 +1,4 @@ +module Main where + +main :: IO () +main = putStrLn "0.1.0.0" diff --git a/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/mybuilder.cabal b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/mybuilder.cabal new file mode 100644 index 00000000000..0649d81c4e0 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.1.0.0/mybuilder.cabal @@ -0,0 +1,18 @@ +cabal-version: 3.0 +name: mybuilder +version: 0.1.0.0 +license: NONE +author: Rodrigo Mesquita +maintainer: rodrigo.m.mesquita@gmail.com +build-type: Simple +extra-doc-files: CHANGELOG.md + +common warnings + ghc-options: -Wall + +executable mybuilder + import: warnings + main-is: Main.hs + build-depends: base + hs-source-dirs: app + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/CHANGELOG.md b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/CHANGELOG.md new file mode 100644 index 00000000000..60a3351b163 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for mybuilder0100 + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/app/Main.hs b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/app/Main.hs new file mode 100644 index 00000000000..3550f30bd2f --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/app/Main.hs @@ -0,0 +1,4 @@ +module Main where + +main :: IO () +main = putStrLn "0.2.0.0" diff --git a/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/mybuilder.cabal b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/mybuilder.cabal new file mode 100644 index 00000000000..c98f493aa14 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T9756/repo/mybuilder-0.2.0.0/mybuilder.cabal @@ -0,0 +1,18 @@ +cabal-version: 3.0 +name: mybuilder +version: 0.2.0.0 +license: NONE +author: Rodrigo Mesquita +maintainer: rodrigo.m.mesquita@gmail.com +build-type: Simple +extra-doc-files: CHANGELOG.md + +common warnings + ghc-options: -Wall + +executable mybuilder + import: warnings + main-is: Main.hs + build-depends: base + hs-source-dirs: app + default-language: Haskell2010