From 6b06b6ca1e049b1916dc435a2a586b95c391eb21 Mon Sep 17 00:00:00 2001
From: Mikhail Glushenkov <mikhail.glushenkov@gmail.com>
Date: Sun, 10 Nov 2013 05:56:41 +0100
Subject: [PATCH] Work around 'findExecutable' being buggy on Windows.

---
 Cabal/Distribution/Simple/Program/Find.hs | 26 +++++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/Cabal/Distribution/Simple/Program/Find.hs b/Cabal/Distribution/Simple/Program/Find.hs
index fc7e11c9cd..9ef1ba2804 100644
--- a/Cabal/Distribution/Simple/Program/Find.hs
+++ b/Cabal/Distribution/Simple/Program/Find.hs
@@ -91,19 +91,31 @@ findProgramOnSearchPath verbosity searchpath prog = do
                        Windows -> ["", "exe"]
                        _       -> [""]
 
-    tryPathElem ProgramSearchPathDefault =
-      findExecutable prog
+    tryPathElem ProgramSearchPathDefault = do
+      -- 'findExecutable' doesn't check that the path really refers to an
+      -- executable on Windows (at least with GHC < 7.8). See
+      -- https://ghc.haskell.org/trac/ghc/ticket/2184
+      mExe <- findExecutable prog
+      case mExe of
+        Just exe -> do isExe <- checkExe exe
+                       if isExe
+                         then return mExe
+                         else return Nothing
+        _        -> return mExe
 
     findFirstExe []     = return Nothing
     findFirstExe (f:fs) = do
+      isExe <- checkExe f
+      if isExe
+        then return (Just f)
+        else findFirstExe fs
+
+    checkExe f = do
       exists <- doesFileExist f
       if exists
         then do perms <- getPermissions f
-                if executable perms
-                  then return (Just f)
-                  else findFirstExe fs
-        else findFirstExe fs
-
+                return (executable perms)
+        else return False
 
 -- | Interpret a 'ProgramSearchPath' to construct a new @$PATH@ env var.
 -- Note that this is close but not perfect because on Windows the search
-- 
GitLab