12

After having picked up stack as a step in starting to write non-trivial haskell programs (larger than a single file) I've run across the problem of not knowing how to get stack to recognize .ini files and such. It doesn't seem to fit anywhere in the .cabal or stack.yaml files.

For clarification: after running stack build/install, the folder with the generated .exe doesn't have the resources, and so of course the program crashes with a bunch of IO errors (file not found).

1 Answer 1

11

There are two options. The one I assume you're looking for is data files. To do this, you'll essentially:

  1. Add the relevant files to the data-files field in your .cabal file so that they are installed when building your package
  2. Add the Paths_package_name module to your other-modules in your .cabal file (replace package_name with your package name)
  3. Import that Paths_* module where needed
  4. Use the generated getDataFileName :: FilePath -> IO FilePath function to get the absolute path to the data file you need

The alternative is to embed the data file contents inside the executable itself using Template Haskell, such as with the file-embed package.

4
  • solved using the data-files field! this is also yet another excuse to get going with exploring template haskell soon ^^
    – sara
    Commented Jul 30, 2017 at 23:33
  • @michael-snoyman could you also share how to do this with package.yaml file if I am using hpack? I've been looking how to do it with that and haven't had any luck.
    – s.kacer
    Commented May 30, 2020 at 8:22
  • 2
    followup: I experimented a bit and found that adding data-dir: data is enough in package.yaml I imported Paths_project into my code and everything compiles fine. But what I am confused by is that when I do stack build, it doesnt seem to copy the data files to .stack-work/install/... where the executable expects to find it when I use getDataFileName... furthermore when I stack install, it copies the built executable, which using its getDataFileName still expects to find the data file in .stack-work/install/...I am very confused. why doesn't stack build copy over the data files.
    – s.kacer
    Commented May 30, 2020 at 9:27
  • @s.kacer you aren't alone. See this Stack issue, it's a bug... although I'm still unsure if it's solved or not.
    – ulidtko
    Commented Oct 27, 2020 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.