Profiling executables which use Template Haskell fails
Repro:
$ cat EmbedString.hs
module EmbedString (embedStr) where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax (lift)
embedStr :: IO String -> ExpQ
embedStr readStr = lift =<< runIO readStr
$ cat Main.hs
{-# LANGUAGE TemplateHaskell #-}
import EmbedString (embedStr)
main :: IO ()
main = putStr $(embedStr (readFile "Setup.hs"))
$ cat profth.cabal
cabal-version: 2.0
name: profth
version: 0.1.0.0
build-type: Simple
executable profth
main-is: Main.hs
other-modules: EmbedString
build-depends: base, template-haskell
default-language: Haskell2010
$ cabal new-build --enable-profiling
Resolving dependencies...
Build profile: -w ghc-8.6.4 -O1
In order, the following will be built (use -v for more details):
- profth-0.1.0.0 (exe:profth) --enable-profiling (first run)
Configuring executable 'profth' for profth-0.1.0.0..
Preprocessing executable 'profth' for profth-0.1.0.0..
Building executable 'profth' for profth-0.1.0.0..
[1 of 2] Compiling EmbedString ( EmbedString.hs, /Users/michael/Desktop/profth/dist-newstyle/build/x86_64-osx/ghc-8.6.4/profth-0.1.0.0/x/profth/build/profth/profth-tmp/EmbedString.p_o )
[2 of 2] Compiling Main ( Main.hs, /Users/michael/Desktop/profth/dist-newstyle/build/x86_64-osx/ghc-8.6.4/profth-0.1.0.0/x/profth/build/profth/profth-tmp/Main.p_o )
Main.hs:6:15: fatal:
cannot find object file ‘/Users/michael/Desktop/profth/dist-newstyle/build/x86_64-osx/ghc-8.6.4/profth-0.1.0.0/x/profth/build/profth/profth-tmp/EmbedString.dyn_o’
while linking an interpreted expression
$ cabal --version
cabal-install version 2.4.1.0
compiled using version 2.4.1.0 of the Cabal library
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.4
And I just remembered that this is caused by a missing other-extensions: TemplateHaskell, which fixes this problem. It would be nice to get better documentation of this, but closing.
Maybe some message like "did you forget to put TemplateHaskell in other-extensions" would be nice when the fatal error occurs.
Related: #4746, #1562.
Why do I need TemplateHaskell in the other-extensions of the .cabal file in order to build with profiling when my actual source files that use template Haskell enables TemplateHaskell via the language extension pragma?
I don't want TemplateHaskell enabled in every module of my project, only the modules that use it.
Having it enabled produces much, much worse parse error messages involving single quotes (').
Cabal has no knowledge of LANGUAGE pragmas used by the package, and other-extensions (unlike default-extensions) doesn't enable the extension globally, it just informs Cabal that you used it somewhere.
I just ran into this, trying to profile an executable with template haskell and getting the same fatal error as in the OP. The fix (adding other-extensions: TemplateHaskell) indeed works, but I would never have thought of this, because nothing in the documentation of other-extensions mentions anything related to changed semantics: it only says anything about checking GHC support, and even alludes to other-extensions being "informational". Hence, when I read the documentation for this cabal file key, I simply discarded it as not useful to me, because I use bounds on base to constrain GHC versions.
It would be great if not only the other-extensions documentation is updated to reflect its apparently fairly significant effect on what cabal does, and furthermore to detect the situation where the user specified --enable-profiling but is also using TemplateHaskell somehow. Because as a user, I have absolutely no clue how to go from "I'm using profiling and TemplateHaskell and GHC cannot find a .dyn_o file" to "let's use other-extensions", apart from using a search engine and hopefully finding this issue.