haskellfile-iocaballeksah

getDataFileName returns a non-existent directory


I can't get data files to be packaged up in a cabal dist. The directory that is produced with getDataFileName doesn't exist after installing the package.

Main.hs:

module Main (
    main
) where

import Paths_mypackage

main = do file <- getDataFileName "mydata.txt"
          putStrLn file
          print =<< readFile file

mypackage.cabal (generated by Leksah after browsing for the file):

 data-files: /Users/peter/Documents/haskell/mypackage/data/mydata.txt
 mydata-dir: ""

Output:

mypackage: /Users/peter/.cabal/share/mypackage-0.0.1/mydata.txt: openFile: does not exist (No such file or directory)
/Users/peter/.cabal/share/mypackage-0.0.1/mydata.txt

Solution

  • The lines in the .cabal file should be

    data-files: mydata.txt
    data-dir:   data
    

    Then cabal will install the data file in the appropriate location, /Users/peter/.cabal/share/mypackage-0.0.1/mydata.txt in your case, and the programme will work.