Skip to content

ghc api: `load LoadAllTargets` is not idempotent

While trying to adapt hint for ghc 8.4.1, I've encountered the following phenomenon where an extra load LoadAllTargets line causes errors.

-- warning: the code below creates files A.hs and B.hs in the current working directory
import System.Directory
import Control.Monad.IO.Class
import GHCi.RemoteTypes

import GHC.Paths -- cf. the ghc-paths package
import Unsafe.Coerce

main = do
    writeFile "A.hs" "module Ahidden(a) where { a :: Int; a = 42 }"
    let mod_nameA = mkModuleName "Ahidden"
        mod_targetA = Target (TargetFile "A.hs" Nothing) False Nothing

    runGhc (Just libdir) $ do
        -- setup
        df0 <- getSessionDynFlags
        (df1, _, _) <- parseDynamicFlags df0 []
        setSessionDynFlags df1{
            ghcMode = CompManager,
            hscTarget = HscInterpreted,
            ghcLink = LinkInMemory,
            verbosity = 0
        }

        -- context : *X
        setTargets [mod_targetA]
        load LoadAllTargets
        load LoadAllTargets -- this line causes the next line to fail
        setContext [IIModule mod_nameA]
-- error:
--   ...: Could not find module ‘Ahidden’
--   Use -v to see a list of the files searched for.
        runIOExpr "print a :: IO ()"

runIOExpr e = do
    HValue h <- compileExpr e
    liftIO (unsafeCoerce h :: IO ())

This code works with ghc 8.2. The new behavior probably originates in 1893ba12, which adds flushing of the finder cache (main/Finder.hs) for every dependency analysis; my guess is that this is where the connection between the module name Ahidden and the file A.hs used be tracked.

http://lpaste.net/364298 is a less artificial example of the same behavior.

Trac metadata
Trac field Value
Version 8.4.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component GHC API
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information