Skip to content

Commit ae442fb

Browse files
committed
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.
1 parent 23aa27d commit ae442fb

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

bindist/wrapper.in

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,16 @@ infer_ghc_pkg() {
8989
unset infer_ghc_path infer_ghc_bin infer_ghc_ver_suffix path_prefix
9090
}
9191

92-
# Get the bin dir from GHCs internal libdir. This is wobbly.
93-
ghc_bin_from_libdir() {
94-
if [ -e "$1"/bin/ghc ] ; then
95-
echo "$1"/bin/ghc
96-
elif [ -e "$1"/bin/ghc-${GHC_VERSION} ] ; then
97-
echo "$1"/bin/ghc-${GHC_VERSION}
98-
elif [ -e "$1"/../bin/ghc ] ; then
99-
echo "$1"/../bin/ghc
100-
elif [ -e "$1"/../bin/ghc-${GHC_VERSION} ] ; then
101-
echo "$1"/../bin/ghc-${GHC_VERSION}
102-
fi
103-
}
104-
105-
# try GHC_LIBDIR from the environment (e.g. user set it)
92+
# try GHC_LIBDIR from the environment (e.g. user set it, or haskell-language-server-wrapper)
10693
if [ -n "${GHC_LIBDIR}" ] &&
107-
check_ghc "${GHC_LIBDIR}" "$(ghc_bin_from_libdir "${GHC_LIBDIR}")" "$(infer_ghc_pkg "$(ghc_bin_from_libdir "${GHC_LIBDIR}")")" "GHC_LIBDIR"
94+
[ -n "${GHC_BIN}" ] &&
95+
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_LIBDIR and GHC_BIN from env"
10896
then
10997
:
11098
# try GHC_BIN from the environment (e.g. user set it)
11199
elif [ -n "${GHC_BIN}" ] &&
112100
GHC_LIBDIR="$("${GHC_BIN}" --print-libdir)" &&
113-
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_BIN"
101+
check_ghc "${GHC_LIBDIR}" "${GHC_BIN}" "$(infer_ghc_pkg "${GHC_BIN}")" "GHC_BIN from env"
114102
then
115103
:
116104
# try ghcup
@@ -123,13 +111,13 @@ then
123111
# try ghc-${GHC_VERSION}
124112
elif command -v ghc-${GHC_VERSION} >/dev/null &&
125113
GHC_LIBDIR="$("ghc-${GHC_VERSION}" --print-libdir)" &&
126-
check_ghc "${GHC_LIBDIR}" "ghc-${GHC_VERSION}" "$(infer_ghc_pkg "ghc-${GHC_VERSION}")" "ghc-<ver>"
114+
check_ghc "${GHC_LIBDIR}" "ghc-${GHC_VERSION}" "$(infer_ghc_pkg "ghc-${GHC_VERSION}")" "ghc-${GHC_VERSION} in PATH"
127115
then
128116
:
129117
# try ghc
130118
elif command -v ghc >/dev/null &&
131119
GHC_LIBDIR="$(ghc --print-libdir)" &&
132-
check_ghc "${GHC_LIBDIR}" "ghc" "$(infer_ghc_pkg "ghc")" "ghc"
120+
check_ghc "${GHC_LIBDIR}" "ghc" "$(infer_ghc_pkg "ghc")" "ghc in PATH"
133121
then
134122
:
135123
# try stack

exe/Wrapper.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE RecordWildCards #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
23
{-# LANGUAGE CPP #-}
34
-- | This module is based on the hie-wrapper.sh script in
45
-- https://github.com/alanz/vscode-hie-server
@@ -112,9 +113,11 @@ launchHaskellLanguageServer parsedArgs = do
112113
#ifdef mingw32_HOST_OS
113114
callProcess e args
114115
#else
116+
let Cradle { cradleOptsProg = CradleAction { runGhcCmd } } = cradle
117+
(CradleSuccess ghcBinary) <- fmap trim <$> runGhcCmd ["-v0", "-package-env=-", "-e", "putStr =<< System.Environment.getExecutablePath"]
115118
(CradleSuccess libdir) <- HieBios.getRuntimeGhcLibDir cradle
116119
env <- Map.fromList <$> getEnvironment
117-
let newEnv = Map.insert "GHC_LIBDIR" libdir env
120+
let newEnv = Map.insert "GHC_BIN" ghcBinary $ Map.insert "GHC_LIBDIR" libdir env
118121
executeFile e True args (Just (Map.toList newEnv))
119122
#endif
120123

0 commit comments

Comments
 (0)