From dcba107cbde824c022cd514a94edf320942dd044 Mon Sep 17 00:00:00 2001
From: gbaz <gershomb@gmail.com>
Date: Sat, 7 Jan 2023 05:40:18 -0500
Subject: [PATCH] prepend rather than append extra prog path (#8506)

* prepend rather than append extra prog path

* changelog

* test

* skip on windows

* update test

* fix test

* reenable windows skip

* Update setup.test.hs

Co-authored-by: Gershom Bazerman <gershom@arista.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
---
 Cabal/src/Distribution/Simple/Configure.hs    |  4 ++--
 .../Distribution/Client/ProjectPlanning.hs    |  4 ++--
 .../PackageTests/ExtraProgPath/MyLibrary.hs   |  1 +
 .../PackageTests/ExtraProgPath/cabal.project  |  1 +
 .../PackageTests/ExtraProgPath/my.cabal       | 20 +++++++++++++++++++
 .../PackageTests/ExtraProgPath/pkg-config     |  3 +++
 .../PackageTests/ExtraProgPath/setup.out      | 11 ++++++++++
 .../PackageTests/ExtraProgPath/setup.test.hs  |  8 ++++++++
 changelog.d/pr-8506                           |  7 +++++++
 9 files changed, 55 insertions(+), 4 deletions(-)
 create mode 100644 cabal-testsuite/PackageTests/ExtraProgPath/MyLibrary.hs
 create mode 100644 cabal-testsuite/PackageTests/ExtraProgPath/cabal.project
 create mode 100644 cabal-testsuite/PackageTests/ExtraProgPath/my.cabal
 create mode 100755 cabal-testsuite/PackageTests/ExtraProgPath/pkg-config
 create mode 100644 cabal-testsuite/PackageTests/ExtraProgPath/setup.out
 create mode 100644 cabal-testsuite/PackageTests/ExtraProgPath/setup.test.hs
 create mode 100644 changelog.d/pr-8506

diff --git a/Cabal/src/Distribution/Simple/Configure.hs b/Cabal/src/Distribution/Simple/Configure.hs
index d8d2aecb80..ac7bd852f0 100644
--- a/Cabal/src/Distribution/Simple/Configure.hs
+++ b/Cabal/src/Distribution/Simple/Configure.hs
@@ -850,9 +850,9 @@ mkProgramDb cfg initialProgramDb = programDb
                  . userSpecifyPaths (configProgramPaths cfg)
                  . setProgramSearchPath searchpath
                  $ initialProgramDb
-    searchpath = getProgramSearchPath initialProgramDb
-                 ++ map ProgramSearchPathDir
+    searchpath = map ProgramSearchPathDir
                  (fromNubList $ configProgramPathExtra cfg)
+                 ++ getProgramSearchPath initialProgramDb
 
 -- -----------------------------------------------------------------------------
 -- Helper functions for configure
diff --git a/cabal-install/src/Distribution/Client/ProjectPlanning.hs b/cabal-install/src/Distribution/Client/ProjectPlanning.hs
index 2f0d79c928..978af213b1 100644
--- a/cabal-install/src/Distribution/Client/ProjectPlanning.hs
+++ b/cabal-install/src/Distribution/Client/ProjectPlanning.hs
@@ -446,8 +446,8 @@ configureCompiler verbosity
         progdb   =
             userSpecifyPaths (Map.toList (getMapLast packageConfigProgramPaths))
           . modifyProgramSearchPath
-              (++ [ ProgramSearchPathDir dir
-                  | dir <- fromNubList packageConfigProgramPathExtra ])
+              ([ ProgramSearchPathDir dir
+               | dir <- fromNubList packageConfigProgramPathExtra ] ++)
           $ defaultProgramDb
 
 
diff --git a/cabal-testsuite/PackageTests/ExtraProgPath/MyLibrary.hs b/cabal-testsuite/PackageTests/ExtraProgPath/MyLibrary.hs
new file mode 100644
index 0000000000..a51c414bcd
--- /dev/null
+++ b/cabal-testsuite/PackageTests/ExtraProgPath/MyLibrary.hs
@@ -0,0 +1 @@
+module MyLibrary () where
diff --git a/cabal-testsuite/PackageTests/ExtraProgPath/cabal.project b/cabal-testsuite/PackageTests/ExtraProgPath/cabal.project
new file mode 100644
index 0000000000..5a93e28e87
--- /dev/null
+++ b/cabal-testsuite/PackageTests/ExtraProgPath/cabal.project
@@ -0,0 +1 @@
+packages: *.cabal
\ No newline at end of file
diff --git a/cabal-testsuite/PackageTests/ExtraProgPath/my.cabal b/cabal-testsuite/PackageTests/ExtraProgPath/my.cabal
new file mode 100644
index 0000000000..2ddc23e47a
--- /dev/null
+++ b/cabal-testsuite/PackageTests/ExtraProgPath/my.cabal
@@ -0,0 +1,20 @@
+name: CheckExtraProgPath
+version: 0.1
+license: BSD3
+license-file: LICENSE
+author: Alexander Vershilov
+maintainer: Alexander Vershilov
+synopsis: Check Extra Prog Path
+category: PackageTests
+build-type: Simple
+cabal-version: 2.0
+
+description:
+    Check that Cabal recognizes an override of the prog path.
+
+Library
+    pkgconfig-depends: zlib
+    default-language: Haskell2010
+    build-depends: base <5.0
+    exposed-modules:
+        MyLibrary
diff --git a/cabal-testsuite/PackageTests/ExtraProgPath/pkg-config b/cabal-testsuite/PackageTests/ExtraProgPath/pkg-config
new file mode 100755
index 0000000000..7c5fafbf0c
--- /dev/null
+++ b/cabal-testsuite/PackageTests/ExtraProgPath/pkg-config
@@ -0,0 +1,3 @@
+#!/usr/bin/sh
+
+exit 1;
diff --git a/cabal-testsuite/PackageTests/ExtraProgPath/setup.out b/cabal-testsuite/PackageTests/ExtraProgPath/setup.out
new file mode 100644
index 0000000000..b0edde0184
--- /dev/null
+++ b/cabal-testsuite/PackageTests/ExtraProgPath/setup.out
@@ -0,0 +1,11 @@
+# cabal v2-build
+Warning: cannot determine version of <ROOT>/./pkg-config :
+""
+Warning: cannot determine version of <ROOT>/./pkg-config :
+""
+Resolving dependencies...
+Error: cabal: Could not resolve dependencies:
+[__0] next goal: CheckExtraProgPath (user goal)
+[__0] rejecting: CheckExtraProgPath-0.1 (conflict: pkg-config package zlib-any, not found in the pkg-config database)
+[__0] fail (backjumping, conflict set: CheckExtraProgPath)
+After searching the rest of the dependency tree exhaustively, these were the goals I've had most trouble fulfilling: CheckExtraProgPath (2)
diff --git a/cabal-testsuite/PackageTests/ExtraProgPath/setup.test.hs b/cabal-testsuite/PackageTests/ExtraProgPath/setup.test.hs
new file mode 100644
index 0000000000..80ee56f628
--- /dev/null
+++ b/cabal-testsuite/PackageTests/ExtraProgPath/setup.test.hs
@@ -0,0 +1,8 @@
+import Test.Cabal.Prelude
+
+-- Test that extra-prog-path overrides the path for pkg-config
+main = cabalTest $ do
+  -- skipped on windows because using a script to dummy up an executable doesn't work the same.
+  skipIfWindows
+  cdir <- testCurrentDir `fmap` getTestEnv
+  fails $ cabal "v2-build" ["--extra-prog-path="++cdir]
diff --git a/changelog.d/pr-8506 b/changelog.d/pr-8506
new file mode 100644
index 0000000000..445f2320e6
--- /dev/null
+++ b/changelog.d/pr-8506
@@ -0,0 +1,7 @@
+synopsis: prepend rather than append extra prog path
+packages: Cabal
+prs: #8506
+issues: #6304
+description: {
+    Prepends the extra-prog-path to the system path rather than appending, to allow binaries in the extra path to override defaults.
+}
-- 
GitLab