From 1e5c7165fb108985e82ba4ec35713c3011c5b951 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 7 Feb 2022 00:11:59 +0100 Subject: [PATCH] Make ghc{,-pkg} detection more robust We don't need to guess the bin/ dir. Instead we only need ghc libdir (which we already have) and the full path to the ghc binary to reconstruct the ghc wrapper. --- bindist/wrapper.in | 24 ++++++------------------ exe/Wrapper.hs | 5 ++++- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/bindist/wrapper.in b/bindist/wrapper.in index ed15ee316b4..401849a5400 100644 --- a/bindist/wrapper.in +++ b/bindist/wrapper.in @@ -89,28 +89,16 @@ infer_ghc_pkg() { unset infer_ghc_path infer_ghc_bin infer_ghc_ver_suffix path_prefix } -# Get the bin dir from GHCs internal libdir. This is wobbly. -ghc_bin_from_libdir() { - if [ -e "$1"/bin/ghc ] ; then - echo "$1"/bin/ghc - elif [ -e "$1"/bin/ghc-${GHC_VERSION} ] ; then - echo "$1"/bin/ghc-${GHC_VERSION} - elif [ -e "$1"/../bin/ghc ] ; then - echo "$1"/../bin/ghc - elif [ -e "$1"/../bin/ghc-${GHC_VERSION} ] ; then - echo "$1"/../bin/ghc-${GHC_VERSION} - fi -} - -# try GHC_LIBDIR from the environment (e.g. user set it) +# try GHC_LIBDIR from the environment (e.g. user set it, or haskell-language-server-wrapper) if [ -n "${GHC_LIBDIR}" ] && - check_ghc "${GHC_LIBDIR}" "$(ghc_bin_from_libdir "${GHC_LIBDIR}")" "$(infer_ghc_pkg "$(ghc_bin_from_libdir "${GHC_LIBDIR}")")" "GHC_LIBDIR" + [ -n "${GHC_BIN}" ] && + check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_LIBDIR and GHC_BIN from env" then : # try GHC_BIN from the environment (e.g. user set it) elif [ -n "${GHC_BIN}" ] && GHC_LIBDIR="$("${GHC_BIN}" --print-libdir)" && - check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_BIN" + check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_BIN from env" then : # try ghcup @@ -123,13 +111,13 @@ then # try ghc-${GHC_VERSION} elif command -v ghc-${GHC_VERSION} >/dev/null && GHC_LIBDIR="$("ghc-${GHC_VERSION}" --print-libdir)" && - check_ghc "${GHC_LIBDIR}" "ghc-${GHC_VERSION}" "$(infer_ghc_pkg "ghc-${GHC_VERSION}")" "ghc-" + check_ghc "${GHC_LIBDIR}" "ghc-${GHC_VERSION}" "$(infer_ghc_pkg "ghc-${GHC_VERSION}")" "ghc-${GHC_VERSION} in PATH" then : # try ghc elif command -v ghc >/dev/null && GHC_LIBDIR="$(ghc --print-libdir)" && - check_ghc "${GHC_LIBDIR}" "ghc" "$(infer_ghc_pkg "ghc")" "ghc" + check_ghc "${GHC_LIBDIR}" "ghc" "$(infer_ghc_pkg "ghc")" "ghc in PATH" then : # try stack diff --git a/exe/Wrapper.hs b/exe/Wrapper.hs index 926620ef5fa..8d76bf238d1 100644 --- a/exe/Wrapper.hs +++ b/exe/Wrapper.hs @@ -1,4 +1,5 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE CPP #-} -- | This module is based on the hie-wrapper.sh script in -- https://github.com/alanz/vscode-hie-server @@ -112,9 +113,11 @@ launchHaskellLanguageServer parsedArgs = do #ifdef mingw32_HOST_OS callProcess e args #else + let Cradle { cradleOptsProg = CradleAction { runGhcCmd } } = cradle + (CradleSuccess ghcBinary) <- fmap trim <$> runGhcCmd ["-package-env=-", "-e", "putStrLn =<< System.Environment.getExecutablePath"] (CradleSuccess libdir) <- HieBios.getRuntimeGhcLibDir cradle env <- Map.fromList <$> getEnvironment - let newEnv = Map.insert "GHC_LIBDIR" libdir env + let newEnv = Map.insert "GHC_BIN" ghcBinary $ Map.insert "GHC_LIBDIR" libdir env executeFile e True args (Just (Map.toList newEnv)) #endif