Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including data files in cabal builds

Tags:

haskell

cabal

I have a package with the following structure (okay, this is greatly simplified, but...)

app/
  src/
    Main.hs
  data/
    data.txt
  app.cabal
  Paths_app.hs
  Setup.hs

In Paths_app.hs I have:

module Paths_app where
getDataFileName :: FilePath -> IO FilePath
getDataFileName = return

and in Main.hs I have:

module Main where
import Paths_app
main = do
    file <- getDataFileName "data/data.txt"
    data <- readFile file
    putStrLn $ "Your data is: " ++ data

the relevant parts of my app.cabal file look like this:

name: app
version: 1.0
build-type: Simple
data-files: data/data.txt

executable foo
  build-depends: base, haskell98
  main-is: Main.hs
  hs-source-dirs: src

This builds fine (using cabal configure followed by cabal install) but the executable complains that it can't find the data.txt file. I've tried replacing the line

file <- getDataFileName "data/data.txt"

with

file <- getDataFileName "data.txt"

but the same thing occurs. Is there something obvious I'm doing wrong?

like image 837
Chris Taylor Avatar asked Apr 27 '12 10:04

Chris Taylor


2 Answers

I've tried to reproduce it, but it works fine for me.

In the setup you describe, I had to drop the dependency on haskell98 as both base and haskell98 were providing Prelude. Furthermore, the file Main wouldn't compile as it used the keyword data as a variable name, so I renamed the variable to dat. But then it worked just fine.

Some info on my setup:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.4.1

$ cabal --version
cabal-install version 0.13.3
using version 1.14.0 of the Cabal library 

$ ls ~/.cabal/bin/
...  foo  ...   

$ ls ~/.cabal/share/app-1.0/data/
data.txt
like image 155
Stefan Holdermans Avatar answered Nov 16 '22 00:11

Stefan Holdermans


The problem was that I was building on a Windows system, and when I used the filename returned by getDataFileName to load data into my program, I wasn't escaping the backslashes.

like image 23
Chris Taylor Avatar answered Nov 16 '22 02:11

Chris Taylor