Skip to content

Commit

Permalink
Make ghc{,-pkg} detection more robust
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hasufell committed Feb 6, 2022
1 parent 23aa27d commit ae442fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
24 changes: 6 additions & 18 deletions bindist/wrapper.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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-<ver>"
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
Expand Down
5 changes: 4 additions & 1 deletion exe/Wrapper.hs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 ["-v0", "-package-env=-", "-e", "putStr =<< 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

Expand Down

0 comments on commit ae442fb

Please sign in to comment.