@@ -4,13 +4,15 @@ import Development.Shake
44import Development.Shake.Command
55import Development.Shake.FilePath
66import Control.Monad
7+ import Data.List
78import System.Directory ( copyFile )
8-
9+ import System.FilePath ( searchPathSeparator , (</>) )
10+ import System.Environment ( lookupEnv , setEnv , getEnvironment )
11+ import BuildSystem
912import Version
1013import Print
1114import Env
1215
13-
1416stackBuildHie :: VersionNumber -> Action ()
1517stackBuildHie versionNumber = execStackWithGhc_ versionNumber [" build" ]
1618 `actionOnException` liftIO (putStrLn stackBuildFailMsg)
@@ -96,3 +98,36 @@ stackBuildFailMsg =
9698 ++ " Try running `stack clean` and restart the build\n "
9799 ++ " If this does not work, open an issue at \n "
98100 ++ " \t https://github.com/haskell/haskell-ide-engine"
101+
102+ -- | Run actions without the stack cached binaries
103+ withoutStackCachedBinaries :: Action a -> Action a
104+ withoutStackCachedBinaries action = do
105+ mbPath <- liftIO (lookupEnv " PATH" )
106+
107+ case (mbPath, isRunFromStack) of
108+
109+ (Just paths, True ) -> do
110+ snapshotDir <- trimmedStdout <$> execStackShake [" path" , " --snapshot-install-root" ]
111+ localInstallDir <- trimmedStdout <$> execStackShake [" path" , " --local-install-root" ]
112+
113+ let cacheBinPaths = [snapshotDir </> " bin" , localInstallDir </> " bin" ]
114+ let origPaths = removePathsContaining cacheBinPaths paths
115+
116+ liftIO (setEnv " PATH" origPaths)
117+ a <- action
118+ liftIO (setEnv " PATH" paths)
119+ return a
120+
121+ otherwise -> action
122+
123+ where removePathsContaining strs path =
124+ joinPaths (filter (not . containsAny) (splitPaths path))
125+ where containsAny p = any (`isInfixOf` p) strs
126+
127+ joinPaths = intercalate [searchPathSeparator]
128+
129+ splitPaths s =
130+ case dropWhile (== searchPathSeparator) s of
131+ " " -> []
132+ s' -> w : words s''
133+ where (w, s'') = break (== searchPathSeparator) s'
0 commit comments