Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 53acb90

Browse files
authored
Merge pull request #1406 from jneira/stack-install-cabal
Find and run cabal in user original $PATH
2 parents 25678ff + b5e388a commit 53acb90

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

install/hie-install.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ library
2121
build-depends: base >= 4.9 && < 5
2222
, shake == 0.17.8
2323
, directory
24+
, filepath
2425
, extra
2526
, text
2627
default-extensions: LambdaCase

install/src/Cabal.hs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import Print
1717
import Env
1818
import Stack
1919

20-
2120
execCabal :: CmdResult r => [String] -> Action r
22-
execCabal = command [] "cabal"
21+
execCabal = execCabalWithOriginalPath
2322

2423
execCabal_ :: [String] -> Action ()
25-
execCabal_ = command_ [] "cabal"
24+
execCabal_ = execCabalWithOriginalPath
25+
26+
execCabalWithOriginalPath :: CmdResult r => [String] -> Action r
27+
execCabalWithOriginalPath = withoutStackCachedBinaries . (command [] "cabal")
2628

2729
cabalBuildData :: Action ()
2830
cabalBuildData = do
@@ -72,18 +74,17 @@ cabalInstallHie versionNumber = do
7274
++ minorVerExe
7375
++ " to " ++ localBin
7476

75-
installCabal :: Action ()
76-
installCabal = do
77+
installCabalWithStack :: Action ()
78+
installCabalWithStack = do
7779
-- try to find existing `cabal` executable with appropriate version
78-
cabalExeOk <- do
79-
c <- liftIO (findExecutable "cabal")
80-
when (isJust c) checkCabal
81-
return $ isJust c
82-
83-
-- install `cabal-install` if not already installed
84-
if cabalExeOk
85-
then printLine "There is already a cabal executable in $PATH with the required minimum version."
86-
else execStackShake_ ["install", "cabal-install"]
80+
mbc <- withoutStackCachedBinaries (liftIO (findExecutable "cabal"))
81+
82+
case mbc of
83+
Just c -> do
84+
checkCabal
85+
printLine "There is already a cabal executable in $PATH with the required minimum version."
86+
-- install `cabal-install` if not already installed
87+
Nothing -> execStackShake_ ["install", "cabal-install"]
8788

8889
-- | check `cabal` has the required version
8990
checkCabal :: Action ()
@@ -117,7 +118,7 @@ cabalInstallNotSuportedFailMsg =
117118
-- | Error message when the `cabal` binary is an older version
118119
cabalInstallIsOldFailMsg :: String -> String
119120
cabalInstallIsOldFailMsg cabalVersion =
120-
"The `cabal` executable is outdated.\n"
121+
"The `cabal` executable found in $PATH is outdated.\n"
121122
++ "found version is `"
122123
++ cabalVersion
123124
++ "`.\n"

install/src/HieInstall.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defaultMain = do
6363
want ["short-help"]
6464
-- general purpose targets
6565
phony "submodules" updateSubmodules
66-
phony "cabal" installCabal
66+
phony "cabal" installCabalWithStack
6767
phony "short-help" shortHelpMessage
6868
phony "all" shortHelpMessage
6969
phony "help" (helpMessage versions)
@@ -117,9 +117,9 @@ defaultMain = do
117117
forM_
118118
ghcVersions
119119
(\version -> phony ("cabal-hie-" ++ version) $ do
120-
validateCabalNewInstallIsSupported
121120
need ["submodules"]
122121
need ["cabal"]
122+
validateCabalNewInstallIsSupported
123123
cabalBuildHie version
124124
cabalInstallHie version
125125
)

install/src/Stack.hs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import Development.Shake
44
import Development.Shake.Command
55
import Development.Shake.FilePath
66
import Control.Monad
7+
import Data.List
78
import System.Directory ( copyFile )
8-
9+
import System.FilePath ( searchPathSeparator, (</>) )
10+
import System.Environment ( lookupEnv, setEnv, getEnvironment )
11+
import BuildSystem
912
import Version
1013
import Print
1114
import Env
1215

13-
1416
stackBuildHie :: VersionNumber -> Action ()
1517
stackBuildHie 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
++ "\thttps://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

Comments
 (0)