How to use TemplateHaskell addDependentFile in a file relative to a compiled file?

I want my TemplateHaskell expression, which uses IO and depends on the MyDependency.txt file, to be re-evaluated when this file changes.

Therefore, I use addDependentFile "MyDependency.txt" to tell ghc to check this file for modification when compiling my code.

Unfortunately, this does not work, because addDependentFile only works with the directory from which ghc is called.

How can I use it to depend on the file that is next to (in the same directory as) that I am compiling?

+4
source share
1 answer

You can use location from Language.Haskell.TH.Syntax to extract the file name of the compiled file and use it to build the correct path:

 -- | Uses 'addDependentFile' on a file relative to the current file -- to mark it as being checked for changes when compiled with TemplateHaskell. -- -- Returns an empty list of declarations so that it can be used with: -- -- >$(addDependentFileRelative "MyDependency.txt") addDependentFileRelative :: FilePath -> Q [Dec] addDependentFileRelative relativeFile = do currentFilename <- loc_filename <$> location pwd <- runIO getCurrentDirectory let invocationRelativePath = takeDirectory (pwd </> currentFilename) </> relativeFile addDependentFile invocationRelativePath return [] 

(public domain code)

+5
source

All Articles