Skip to content

Commit

Permalink
Warn if TH and Mac and static binary (#2470)
Browse files Browse the repository at this point in the history
* Warn if TH and Mac and static binary

* fix up the instructions

* use hostIsDynamic

* Upgrade hie-bios version

* bump cabal index

* fix more Stack descriptors

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
pepeiborra and mergify[bot] authored Dec 13, 2021
1 parent 3b581a1 commit 807cb8f
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cabal-ghc901.project
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ package *

write-ghc-environment-files: never

index-state: 2021-11-29T08:11:07Z
index-state: 2021-11-29T12:30:07Z

constraints:
-- These plugins don't work on GHC9 yet
Expand Down
2 changes: 1 addition & 1 deletion cabal-ghc921.project
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ package *

write-ghc-environment-files: never

index-state: 2021-11-29T08:11:07Z
index-state: 2021-11-29T12:30:07Z

constraints:
-- These plugins doesn't work on GHC92 yet
Expand Down
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ package *

write-ghc-environment-files: never

index-state: 2021-11-29T08:11:07Z
index-state: 2021-11-29T12:30:07Z

constraints:
hyphenation +embed
Expand Down
10 changes: 3 additions & 7 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ An usual symptom is the presence of errors containing `unknown symbol` and it is

The workaround is to use a version of haskell-language-server compiled from source with the ghc option `-dynamic` enabled. See more details [here](https://github.com/haskell/haskell-language-server/issues/1160#issuecomment-756566273).

### Problems with Template Haskell
### Support for Template Haskell

Due to how Template Haskell code is evaluated at compile time and some limitations in the interaction between HLS and GHC, the loading of modules using TH can be problematic.
Template Haskell should work fine in Linux and Windows with the distributed binaries. In Mac Os a dynamically linked binary of HLS is required to avoid segmentation faults. The easiest way to obtain a dynamically linked HLS binary is to build it locally. With cabal install this can be done as follows:

The errors thrown are usually related to linking and usually make HLS crash: `Segmentation fault`, `GHC runtime linker: fatal error`, etc

A workaround which has helped in some cases is to compile HLS from source with the ghc option `-dynamic` enabled, as in the previous issue.

We have a [dedicated label](https://github.com/haskell/haskell-language-server/issues?q=is%3Aissue+is%3Aopen+label%3A%22type%3A+template+haskell+related%22) in the issue tracker and an [general issue](https://github.com/haskell/haskell-language-server/issues/1431) tracking support for TH.
cabal update && cabal install haskell-language-server --enable-executable-dynamic

## Troubleshooting the server

Expand Down
2 changes: 1 addition & 1 deletion ghcide/ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ library
ghc-check >=0.5.0.4,
ghc-paths,
cryptohash-sha1 >=0.11.100 && <0.12,
hie-bios >= 0.7.1 && < 0.9.0,
hie-bios >= 0.8 && < 0.9.0,
implicit-hie-cradle >= 0.3.0.5 && < 0.4,
base16-bytestring >=0.1.1 && <1.1
if os(windows)
Expand Down
40 changes: 36 additions & 4 deletions ghcide/src/Development/IDE/Core/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ import qualified GHC.LanguageExtensions as LangExt
import qualified HieDb
import Ide.Plugin.Config
import qualified Language.LSP.Server as LSP
import Language.LSP.Types (SMethod (SCustomMethod))
import Language.LSP.Types (SMethod (SCustomMethod, SWindowShowMessage), ShowMessageParams (ShowMessageParams), MessageType (MtInfo))
import Language.LSP.VFS
import System.Directory (makeAbsolute)
import Data.Default (def, Default)
Expand All @@ -149,6 +149,15 @@ import Ide.PluginUtils (configForPlugin)
import Ide.Types (DynFlagsModifications (dynFlagsModifyGlobal, dynFlagsModifyParser),
PluginId)
import Control.Concurrent.STM.Stats (atomically)
import Language.LSP.Server (LspT)
import System.Environment (getExecutablePath)
import System.Process.Extra (readProcessWithExitCode)
import Text.Read (readMaybe)
import System.Info.Extra (isMac)
import HIE.Bios.Ghc.Gap (hostIsDynamic)

templateHaskellInstructions :: T.Text
templateHaskellInstructions = "https://haskell-language-server.readthedocs.io/en/latest/troubleshooting.html#support-for-template-haskell"

-- | This is useful for rules to convert rules that can only produce errors or
-- a result into the more general IdeResult type that supports producing
Expand Down Expand Up @@ -820,8 +829,26 @@ isHiFileStableRule = defineEarlyCutoff $ RuleNoDiagnostics $ \IsHiFileStable f -
summarize SourceUnmodified = BS.singleton 2
summarize SourceUnmodifiedAndStable = BS.singleton 3

displayTHWarning :: LspT c IO ()
displayTHWarning
| isMac && not hostIsDynamic = do
LSP.sendNotification SWindowShowMessage $
ShowMessageParams MtInfo $ T.unwords
[ "This HLS binary does not support Template Haskell."
, "Follow the [instructions](" <> templateHaskellInstructions <> ")"
, "to build an HLS binary with support for Template Haskell."
]
| otherwise = return ()

newtype DisplayTHWarning = DisplayTHWarning (IO ())
instance IsIdeGlobal DisplayTHWarning

getModSummaryRule :: Rules ()
getModSummaryRule = do
env <- lspEnv <$> getShakeExtrasRules
displayItOnce <- liftIO $ once $ LSP.runLspT (fromJust env) displayTHWarning
addIdeGlobal (DisplayTHWarning displayItOnce)

defineEarlyCutoff $ Rule $ \GetModSummary f -> do
session' <- hscEnv <$> use_ GhcSession f
modify_dflags <- getModifyDynFlags dynFlagsModifyGlobal
Expand All @@ -832,6 +859,10 @@ getModSummaryRule = do
getModSummaryFromImports session fp modTime (textToStringBuffer <$> mFileContent)
case modS of
Right res -> do
-- Check for Template Haskell
when (uses_th_qq $ msrModSummary res) $ do
DisplayTHWarning act <- getIdeGlobalAction
liftIO act
bufFingerPrint <- liftIO $
fingerprintFromStringBuffer $ fromJust $ ms_hspp_buf $ msrModSummary res
let fingerPrint = Util.fingerprintFingerprints
Expand Down Expand Up @@ -1027,9 +1058,6 @@ needsCompilationRule file = do

pure (Just $ encodeLinkableType res, Just res)
where
uses_th_qq (ms_hspp_opts -> dflags) =
xopt LangExt.TemplateHaskell dflags || xopt LangExt.QuasiQuotes dflags

computeLinkableType :: ModSummary -> [Maybe ModSummary] -> [Maybe LinkableType] -> Maybe LinkableType
computeLinkableType this deps xs
| Just ObjectLinkable `elem` xs = Just ObjectLinkable -- If any dependent needs object code, so do we
Expand All @@ -1039,6 +1067,10 @@ needsCompilationRule file = do
where
this_type = computeLinkableTypeForDynFlags (ms_hspp_opts this)

uses_th_qq :: ModSummary -> Bool
uses_th_qq (ms_hspp_opts -> dflags) =
xopt LangExt.TemplateHaskell dflags || xopt LangExt.QuasiQuotes dflags

-- | How should we compile this module?
-- (assuming we do in fact need to compile it).
-- Depends on whether it uses unboxed tuples or sums
Expand Down
1 change: 1 addition & 0 deletions stack-8.10.6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extra-deps:
- data-tree-print-0.1.0.2@sha256:d845e99f322df70e0c06d6743bf80336f5918d5423498528beb0593a2afc1703,1620
- floskell-0.10.5@sha256:77f0bc1569573d9666b10975a5357fef631d32266c071733739393ccae521dab,3803
- heapsize-0.3.0.1@sha256:0b69aa97a46d819b700ac7b145f3b5493c3565cf2c5b8298682238d405d0326e,1417
- hie-bios-0.8.0
- hiedb-0.4.1.0
- implicit-hie-0.1.2.6@sha256:f50a908979a574a881f753c0f9a5224f023f438b30fdefc5b7fa01803b07a280,2998
- implicit-hie-cradle-0.3.0.5@sha256:5f5e575f549b2a9db664be7650b5c3c9226e313bddc46c79e2e83eb349f8e692,2610
Expand Down
1 change: 1 addition & 0 deletions stack-8.10.7.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extra-deps:
- data-tree-print-0.1.0.2@sha256:d845e99f322df70e0c06d6743bf80336f5918d5423498528beb0593a2afc1703,1620
- floskell-0.10.5@sha256:77f0bc1569573d9666b10975a5357fef631d32266c071733739393ccae521dab,3803
- heapsize-0.3.0.1@sha256:0b69aa97a46d819b700ac7b145f3b5493c3565cf2c5b8298682238d405d0326e,1417
- hie-bios-0.8.0
- hiedb-0.4.1.0
- implicit-hie-0.1.2.6@sha256:f50a908979a574a881f753c0f9a5224f023f438b30fdefc5b7fa01803b07a280,2998
- implicit-hie-cradle-0.3.0.5@sha256:5f5e575f549b2a9db664be7650b5c3c9226e313bddc46c79e2e83eb349f8e692,2610
Expand Down
2 changes: 1 addition & 1 deletion stack-8.6.5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extra-deps:
- haddock-library-1.10.0
- hashable-1.3.0.0
- heapsize-0.3.0
- hie-bios-0.7.5
- hie-bios-0.8.0
- hlint-3.2.3
- HsYAML-0.2.1.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:2
Expand Down
2 changes: 1 addition & 1 deletion stack-8.8.4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extra-deps:
- ghc-trace-events-0.1.2.1
- haskell-src-exts-1.21.1
- heapsize-0.3.0
- hie-bios-0.7.5
- hie-bios-0.8.0
- hlint-3.2.3
- HsYAML-aeson-0.2.0.0@rev:2
- hoogle-5.0.17.11
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extra-deps:
- data-tree-print-0.1.0.2@sha256:d845e99f322df70e0c06d6743bf80336f5918d5423498528beb0593a2afc1703,1620
- floskell-0.10.5@sha256:77f0bc1569573d9666b10975a5357fef631d32266c071733739393ccae521dab,3803
- heapsize-0.3.0.1@sha256:0b69aa97a46d819b700ac7b145f3b5493c3565cf2c5b8298682238d405d0326e,1417
- hie-bios-0.8.0
- hiedb-0.4.1.0
- implicit-hie-0.1.2.6@sha256:f50a908979a574a881f753c0f9a5224f023f438b30fdefc5b7fa01803b07a280,2998
- implicit-hie-cradle-0.3.0.5@sha256:5f5e575f549b2a9db664be7650b5c3c9226e313bddc46c79e2e83eb349f8e692,2610
Expand Down

0 comments on commit 807cb8f

Please sign in to comment.