9

I'm working on a Haskell project (Happstack server + Blaze HTML/front-end as main libraries) and I want to add a static data directory.

Looks like you can do so with Cabal using the auto-generated Path_<package_name> module. So in my example, the package is called new-website, so that module should be called Paths_new_website.

Link to Cabal docs re: a custom package's Paths_pkgname module.

From the command line and using cabal repl, I am trying to confirm that I'll have access to the Paths_ module. However, I'm finding that the Paths_new_website module isn't being imported by Cabal when running cabal_repl.

Here's a link to some relevant code and terminal output via a gist.

Does anyone have experience with this, getting a finicky Paths_ module to load with my package? I suspect this may be a question of lexical scope between Main.hs (the primary source file) versus the context in cabal_repl...

mecampbellsoup
  • 1,260
  • 18
  • 16
  • 1
    Are you having trouble compiling or loading it into GHCi? Cabal only produces these `Paths_*` modules during the build. (Edit: Oh, just saw it, `cabal repl`) – J. Abrahamson Feb 05 '14 at 21:16
  • 1
    @J.Abrahamson thanks man! Turns out, I just needed to do `import Paths_*` (where `*` is the package name in my `.cabal` build file) inside of `Main.hs`. The cabal documentation made it seem like I only needed to add `Paths_*` inside of my `.cabal` file beneath `other-modules` and I would automatically have access to the `Paths_*` module. Not the case! In fact, even without that addition to my `.cabal` file I'm able to import the auto-generated `Paths_*` module into `Main`. – mecampbellsoup Feb 08 '14 at 03:08
  • @J.Abrahamson When you say "during the build", it's my understanding that `cabal repl` is executing a build - is that correct? – mecampbellsoup Feb 08 '14 at 03:13

1 Answers1

6

Paths_* modules are only generated by Cabal during builds. If you're running the package using GHCi or cabal repl then they simply won't exist and your code will fail with "Cannot find module" errors.

There's a sneaky development mode trick, though: just build your own Paths_* module and place it in your haskell-source-dir. During development, GHCi will load that module and you can adjust its exported symbols to make your development environment fly. During build, Cabal will overwrite your module with its own and take into account the final information needed to build the Paths_* module.

So in this particular case, just make a file src/Paths_stackbuilders.hs and provide it a relative path to the datadir.

J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
  • 5
    You can also do `ghci -idist/build/autogen` – Adam Bergmark Feb 06 '14 at 05:00
  • 4
    I tried it the way you suggested, with a handwritten drop-in-fake-module. But `cabal build` used the fake-module and did not overwrite or superseed it (cabal 1.18) – NobbZ Feb 26 '14 at 10:36
  • 1
    Why does `cabal repl` compile the `Paths_pkgname.hs` if it's not usable inside the repl? – CMCDragonkai Aug 30 '18 at 10:47
  • If you use `stack build` then do `:set -i.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/autogen` or include it on the command line (or in the .ghci). It would be desirable, if `stack exec ghci` would include this automatically. – user855443 Mar 21 '19 at 08:44
  • that sneaky development mode trick doesn't work when the package with `Paths_*` is a dependency, unfortunately. The ghci flag isn't available for me either, I use `ob run` from obsidiansystems/obelisk – ruben.moor Feb 21 '23 at 19:54