Skip to content

Exception when running compile-time code when using autogenerated cabal Paths_pkgname module in template haskell #4611

@NeuroCorgi

Description

@NeuroCorgi

Which OS do you use?
MacOS
Which version of GHC do you use and how did you install it?
GHC 9.10.1 with ghcup
How is your project built (alternative: link to the project)?
cabal build
Which LSP client (editor/plugin) do you use?
emacs+eglot
Which version of HLS do you use and how did you install it?
haskell-language-server 2.9.0.1 with ghcup

Steps to reproduce

When using autogenerated Paths_<pkgname> module from Cabal to read project files from a template haskell function, HLS reports an error diagnostic at the splice sites that use this function.

Reproducing project

reproducing project

project.cabal

cabal-version:      3.0
name:               project
version:            0.1.0.0
license:            MIT
license-file:       LICENSE
author:             NeuroCorgi
build-type:         Simple

executable project
    main-is:          Main.hs
    other-modules:    TH
                      Paths_project
    autogen-modules:  Paths_project
    build-depends:    base, template-haskell
    hs-source-dirs:   src
    default-language: Haskell2010

src/Main.hs

{-# LANGUAGE TemplateHaskell #-}

import TH

main :: IO ()
main = putStrLn $(embedFileFromProject "src/TH.hs")

src/TH.hs

module TH where

import System.IO
import Language.Haskell.TH

import qualified Paths_project as Paths

embedFileFromProject :: FilePath -> ExpQ
embedFileFromProject filePath = stringE =<< (runIO $ contents =<< Paths.getDataFileName filePath)
  where contents path = withFile path ReadMode hGetContents'

Expected behaviour

No diagnostic reported.

Actual behaviour

Diagnostic reported at the template haskell splice in src/Main.hs:

• Exception when trying to run compile-time code:
    /Users/neurocorgi/.cabal/share/aarch64-osx-ghc-9.10.1-64dd/project-0.1.0.0/src/TH.hs: withFile: does not exist (No such file or directory)
  Code: (embedFileFromProject "src/TH.hs")
• In the untyped splice: $(embedFileFromProject "src/TH.hs")

Activity

added
type: bugSomething isn't right: doesn't work as intended, documentation is missing/outdated, etc..
on Jun 3, 2025
fendor

fendor commented on Jun 4, 2025

@fendor
Collaborator

Hi, thank you for your bug report!

I think your example isn't quite correct, for example, cabal install fails with the error message:

> cabal install
Wrote tarball sdist to
/home/hugin/Documents/haskell/issues-hls/hls-reproduce/dist-newstyle/sdist/project-0.1.0.0.tar.gz
Resolving dependencies...
Build profile: -w ghc-9.6.7 -O1
In order, the following will be built (use -v for more details):
 - project-0.1.0.0 (exe:project) (requires build)
Starting     project-0.1.0.0 (exe:project)
Building     project-0.1.0.0 (exe:project)

Failed to build exe:project from project-0.1.0.0.
Build log (
/home/hugin/.cache/cabal/logs/ghc-9.6.7/project-0.1.0.0-e-project-ad861676a0877a40dbc4514112988a4044f938cd9a305469a0a30cacf99c6cb3.log
):
Configuring executable 'project' for project-0.1.0.0...
Preprocessing executable 'project' for project-0.1.0.0...
Building executable 'project' for project-0.1.0.0...
[1 of 3] Compiling Paths_project    ( dist/build/project/autogen/Paths_project.hs, dist/build/project/project-tmp/Paths_project.o, dist/build/project/project-tmp/Paths_project.dyn_o )
[2 of 3] Compiling TH               ( src/TH.hs, dist/build/project/project-tmp/TH.o, dist/build/project/project-tmp/TH.dyn_o )
[3 of 3] Compiling Main             ( src/Main.hs, dist/build/project/project-tmp/Main.o )

src/Main.hs:6:17: error: [GHC-87897]
    • Exception when trying to run compile-time code:
        /home/hugin/.local/state/cabal/store/ghc-9.6.7/project-0.1.0.0-e-project-ad861676a0877a40dbc4514112988a4044f938cd9a305469a0a30cacf99c6cb3/share/src/TH.hs: withFile: does not exist (No such file or directory)
      Code: (embedFileFromProject "src/TH.hs")
    • In the untyped splice: $(embedFileFromProject "src/TH.hs")
  |
6 | main = putStrLn $(embedFileFromProject "src/TH.hs")
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can have a look at https://hackage.haskell.org/package/file-embed-0.0.16.0/docs/src/Data.FileEmbed.html#embedFileRelative, which makes sure to change to the root directory before running the TH code.

michaelpj

michaelpj commented on Jul 9, 2025

@michaelpj
Collaborator

This is a known issue, you have to use the makeRelativeToProject functions from file-embed if you want it to work reliably, as the working directory for TH code is not specified and can vary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: ghcidetype: bugSomething isn't right: doesn't work as intended, documentation is missing/outdated, etc..

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @michaelpj@fendor@NeuroCorgi

        Issue actions

          Exception when running compile-time code when using autogenerated cabal `Paths_pkgname` module in template haskell · Issue #4611 · haskell/haskell-language-server