Skip to content

Compiling with coverage fails on TemplateHaskell on Windows when overriding object file extensions

Summary

Calling ghc directly to compile modules one by one with HPC enabled fails when TemplateHaskell is used and explicit destinations for resulting object and interface files are specified.

Steps to reproduce

HereDoc.hs, a module with Template Haskell.

{-# OPTIONS_GHC -Wno-missing-fields #-}

module HereDoc (heredoc) where

import Language.Haskell.TH
import Language.Haskell.TH.Quote

heredoc :: QuasiQuoter
heredoc = QuasiQuoter
  { quoteExp =  stringE . prepare
  , quotePat =  litP . stringL
  }
  where
    prepare = filter (/= '\r')

Test.hs, main module.

{-# LANGUAGE QuasiQuotes #-}

module Test (main) where

import HereDoc

main :: IO ()
main = do

  putStrLn [heredoc|Hello, World!|]

  pure ()

Failing compilation:

$ ghc -fhpc -hpcdir hpc -c -O -osuf c_o -hisuf c_hi -o HereDoc.c_o -ohi HereDoc.c_hi HereDoc.hs
$ ghc -fhpc -hpcdir hpc -c -O -osuf c_o -hisuf c_hi -o Test.c_o -ohi Test.c_hi Test.hs -main-is Test
ghc-9.8.1.exe:  | C:\home\tmp\windows-covererage-bug-report\HereDoc.c_o: unknown symbol `HereDoc_init__hpc'
ghc-9.8.1.exe: ^^ Could not load 'HereDoc_heredoc_closure', dependency unresolved. See top entry above.


GHC.ByteCode.Linker.lookupCE
During interactive linking, GHCi couldn't find the following symbol:
  HereDoc_heredoc_closure
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session.  Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please report this as a GHC bug:
  https://www.haskell.org/ghc/reportabug

When I don't specify extensions and explicit files for .o and .hi files then it doesn't fail:

$ ghc -fhpc -hpcdir hpc -c -O HereDoc.hs
$ ghc -fhpc -hpcdir hpc -c -O Test.hs -main-is Test
$ l
HereDoc.hi  HereDoc.hs  HereDoc.o  hpc/  Test.hi  Test.hs*  Test.o
$ ghc -package template-haskell -o Test.exe Test.o HereDoc.o
$ ./Test.exe 
Hello, World!

This workaround is unsatisfactory because coverage object files are not interchageable with regular object files and thus ought to have different extension to tell them apart.

Expected behavior

Successful compilation.

Environment

  • GHC version used: 9.8.1

Optional:

  • Operating System: Windows
  • System Architecture: x86_64
Edited by Sergey Vinokurov
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information