From 2da7e9bf8c5d792f5b4681bd8ae964a77efc846a Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sat, 7 Aug 2021 13:46:28 +0200 Subject: [PATCH] Fix hook running on windows --- src/Stack/Setup.hs | 27 +++++++++---------- .../tests/ghc-install-hooks/Main.hs | 2 +- .../tests/ghc-install-hooks/files/run.sh | 6 ++--- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Stack/Setup.hs b/src/Stack/Setup.hs index 8f9bde3160..50988d0283 100644 --- a/src/Stack/Setup.hs +++ b/src/Stack/Setup.hs @@ -97,7 +97,7 @@ import Stack.Types.Docker import Stack.Types.SourceMap import Stack.Types.Version import qualified System.Directory as D -import System.Environment (getExecutablePath, lookupEnv, getEnvironment) +import System.Environment (getExecutablePath, lookupEnv) import System.IO.Error (isPermissionError) import System.FilePath (searchPathSeparator) import qualified System.FilePath as FP @@ -447,21 +447,18 @@ ensureCompilerAndMsys => SetupOpts -> RIO env (CompilerPaths, ExtraDirs) ensureCompilerAndMsys sopts = do + getSetupInfo' <- memoizeRef getSetupInfo + mmsys2Tool <- ensureMsys sopts getSetupInfo' + msysPaths <- maybe (pure Nothing) (fmap Just . extraDirs) mmsys2Tool + actual <- either throwIO pure $ wantedToActual $ soptsWantedCompiler sopts didWarn <- warnUnsupportedCompiler $ getGhcVersion actual - getSetupInfo' <- memoizeRef getSetupInfo (cp, ghcPaths) <- ensureCompiler sopts getSetupInfo' warnUnsupportedCompilerCabal cp didWarn - mmsys2Tool <- ensureMsys sopts getSetupInfo' - paths <- - case mmsys2Tool of - Nothing -> pure ghcPaths - Just msys2Tool -> do - msys2Paths <- extraDirs msys2Tool - pure $ ghcPaths <> msys2Paths + let paths = maybe ghcPaths (ghcPaths <>) msysPaths pure (cp, paths) -- | See @@ -613,7 +610,9 @@ ensureCompiler sopts getSetupInfo' = do wc <- either throwIO (pure . whichCompiler) $ wantedToActual wanted hook <- ghcInstallHook - hookIsExecutable <- handleIO (\_ -> pure False) $ executable <$> getPermissions hook + hookIsExecutable <- handleIO (\_ -> pure False) $ if osIsWindows + then doesFileExist hook -- can't really detect executable on windows, only file extension + else executable <$> getPermissions hook Platform expectedArch _ <- view platformL @@ -664,10 +663,10 @@ runGHCInstallHook runGHCInstallHook sopts hook = do logDebug "Getting hook installed compiler version" let wanted = soptsWantedCompiler sopts - curEnv <- Map.fromList . map (T.pack *** T.pack) <$> liftIO getEnvironment - let newEnv = Map.union (wantedCompilerToEnv wanted) curEnv - pCtx <- mkProcessContext newEnv - (exit, out) <- withProcessContext pCtx $ proc "sh" [toFilePath hook] readProcessStdout + menv0 <- view processContextL + menv <- mkProcessContext (Map.union (wantedCompilerToEnv wanted) $ + removeHaskellEnvVars (view envVarsL menv0)) + (exit, out) <- withProcessContext menv $ proc "sh" [toFilePath hook] readProcessStdout case exit of ExitSuccess -> do let ghcPath = stripNewline . TL.unpack . TL.decodeUtf8With T.lenientDecode $ out diff --git a/test/integration/tests/ghc-install-hooks/Main.hs b/test/integration/tests/ghc-install-hooks/Main.hs index 271c259449..491eb11761 100644 --- a/test/integration/tests/ghc-install-hooks/Main.hs +++ b/test/integration/tests/ghc-install-hooks/Main.hs @@ -4,4 +4,4 @@ import StackTest import Control.Monad (unless) main :: IO () -main = unless isWindows $ rawSystem "bash" ["run.sh"] >>= throwIO +main = rawSystem "sh" ["run.sh"] >>= throwIO diff --git a/test/integration/tests/ghc-install-hooks/files/run.sh b/test/integration/tests/ghc-install-hooks/files/run.sh index a27bee9420..0d4196566e 100755 --- a/test/integration/tests/ghc-install-hooks/files/run.sh +++ b/test/integration/tests/ghc-install-hooks/files/run.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh -set -exuo pipefail +set -exu stack_bin=$("$STACK_EXE" path --resolver ghc-8.6.5 --compiler-bin) @@ -8,7 +8,7 @@ export STACK_ROOT=$(pwd)/fake-root mkdir -p "${STACK_ROOT}"/hooks -echo "echo ${stack_bin}/ghc" > "${STACK_ROOT}"/hooks/ghc-install.sh +echo "echo '${stack_bin}/ghc'" > "${STACK_ROOT}"/hooks/ghc-install.sh chmod +x "${STACK_ROOT}"/hooks/ghc-install.sh "$STACK_EXE" --no-install-ghc --resolver ghc-8.6.5 ghc -- --info