Skip to content

Commit

Permalink
Initialize GHC plugins
Browse files Browse the repository at this point in the history
Since GHC-8.6 plugins need to be initialized on a per module basis.
Without this any modules using plugins fail to compile inside doctest.

This code is adapted from how this was fixed in haddock:
https://github.com/haskell/haddock/pull/983/files#diff-a6119dd2da927bbff2c39eda86cdc733
  • Loading branch information
leonschoorl committed Mar 14, 2019
1 parent 819ac49 commit 2770c8d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Extract.hs
Expand Up @@ -47,6 +47,10 @@ import Location hiding (unLoc)
import Util (convertDosLineEndings)
import PackageDBs (getPackageDBArgs)

#if __GLASGOW_HASKELL__ >= 806
import DynamicLoading (initializePlugins)
#endif

-- | A wrapper around `SomeException`, to allow for a custom `Show` instance.
newtype ExtractError = ExtractError SomeException
deriving Typeable
Expand Down Expand Up @@ -107,7 +111,7 @@ parse args = withGhc args $ \modules_ -> withTempOutputDir $ do
mods' <- if needsTemplateHaskellOrQQ mods then enableCompilation mods else return mods

let sortedMods = flattenSCCs (topSortModuleGraph False mods' Nothing)
reverse <$> mapM (parseModule >=> typecheckModule >=> loadModule) sortedMods
reverse <$> mapM (loadModPlugins >=> parseModule >=> typecheckModule >=> loadModule) sortedMods
where
-- copied from Haddock/Interface.hs
enableCompilation :: ModuleGraph -> Ghc ModuleGraph
Expand Down Expand Up @@ -162,6 +166,16 @@ parse args = withGhc args $ \modules_ -> withTempOutputDir $ do
, includePaths = addQuoteInclude (includePaths d) [f]
}

#if __GLASGOW_HASKELL__ >= 806
-- Since GHC 8.6, plugins are initialized on a per module basis
loadModPlugins modsum = do
hsc_env <- getSession
dynflags' <- liftIO (initializePlugins hsc_env (GHC.ms_hspp_opts modsum))
return $ modsum { ms_hspp_opts = dynflags' }
#else
loadModPlugins = return
#endif

-- | Extract all docstrings from given list of files/modules.
--
-- This includes the docstrings of all local modules that are imported from
Expand Down

0 comments on commit 2770c8d

Please sign in to comment.