From e3121fb732a3d52d9d329fa30de6747e95e36fc4 Mon Sep 17 00:00:00 2001
From: Gershom Bazerman <gershom@arista.com>
Date: Wed, 24 May 2023 16:28:12 -0400
Subject: [PATCH] Fix precedence for PATH for build-tools-depends

---
 Cabal/src/Distribution/Simple/Configure.hs            | 11 +++++++++--
 .../src/Distribution/Client/ProjectPlanning.hs        |  5 ++++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Cabal/src/Distribution/Simple/Configure.hs b/Cabal/src/Distribution/Simple/Configure.hs
index 03a1d45973..c32946fe21 100644
--- a/Cabal/src/Distribution/Simple/Configure.hs
+++ b/Cabal/src/Distribution/Simple/Configure.hs
@@ -851,9 +851,16 @@ mkProgramDb cfg initialProgramDb = programDb
                  . userSpecifyPaths (configProgramPaths cfg)
                  . setProgramSearchPath searchpath
                  $ initialProgramDb
-    searchpath = map ProgramSearchPathDir
+    searchpath = getProgramSearchPath initialProgramDb ++
+                 map ProgramSearchPathDir
                  (fromNubList $ configProgramPathExtra cfg)
-                 ++ getProgramSearchPath initialProgramDb
+                 -- 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
+                 -- has already been extended by both the built-tools-depends paths, as well as the program-path-extra
+                 -- so for v2 builds adding it again is entirely unnecessary. However, it needs to get added again _anyway_
+                 -- so as to take effect for v1 builds or standalone calls to Setup.hs
+                 -- In this instance, the lesser evil is to not allow it to override the system path.
+
 
 -- -----------------------------------------------------------------------------
 -- Helper functions for configure
diff --git a/cabal-install/src/Distribution/Client/ProjectPlanning.hs b/cabal-install/src/Distribution/Client/ProjectPlanning.hs
index 06669fb564..631304ca80 100644
--- a/cabal-install/src/Distribution/Client/ProjectPlanning.hs
+++ b/cabal-install/src/Distribution/Client/ProjectPlanning.hs
@@ -3401,7 +3401,10 @@ setupHsScriptOptions (ReadyPackage elab@ElaboratedConfiguredPackage{..})
       useDistPref              = builddir,
       useLoggingHandle         = Nothing, -- this gets set later
       useWorkingDir            = Just srcdir,
-      useExtraPathEnv          = elabExeDependencyPaths elab,
+      useExtraPathEnv          = elabExeDependencyPaths elab ++ elabProgramPathExtra,
+                                 -- note that the above adds the extra-prog-path directly following the elaborated
+                                 -- dep paths, so that it overrides the normal path, but _not_ the elaborated extensions
+                                 -- for build-tools-depends.
       useExtraEnvOverrides     = dataDirsEnvironmentForPlan distdir plan,
       useWin32CleanHack        = False,   --TODO: [required eventually]
       forceExternalSetupMethod = isParallelBuild,
-- 
GitLab