haskell / haskell/cabal

`data-files` of relocatable libraries are inaccessible in template Haskell

Open
#6,549 5 comments 0 reactions 0 assignees View on GitHub
data-files type: enhancement
Dominant language
Haskell
Stars
1.7k
Forks
750
Avg merge
4d 3h
Merged PRs (30d)
28

Description

**Describe the bug**

The generated `Paths_...` module for `data-files` of `--enable-relocatable` packages produces invalid paths under template Haskell.

If a package, say `provides-data-file`, exposes the path to a `data-file` and is `configure`d with `--enable-relocatable`; and another package, say `embeds-data-file`, tries to access that `data-file` within template Haskell, then this access will fail with `openFile: does not exist`. The reason seems to be that for `--enable-relocatable` the generated `Paths_...` module uses paths relative to `getExecutableName` which, under template Haskell, returns the GHC executable. So, the resulting path points into the GHC installation, instead the package installation.

**To Reproduce**

```
$ git clone https://github.com/aherrmann/cabal_relocatable_data_files_template_haskell.git
$ cd cabal_relocatable_data_files_template_haskell
$ ./issue.sh
```

This should fail with the following error:

```
[1 of 1] Compiling UsesDataFile ( UsesDataFile.hs, dist/build/UsesDataFile.o )

UsesDataFile.hs:9:11: error:
• Exception when trying to run compile-time code:
/opt/ghc/8.8.2/lib/ghc-8.8.2/share/x86_64-linux-ghc-8.8.2/provides-data-file-0.1.0.0/my-data-file: openFile: does not exist (No such file or directory)
Code: do content <- runIO $ readFile =<< dataFile
[| content |]
pending(tc) []
• In the untyped splice:
$(do content <- runIO $ readFile =<< dataFile
[| content |])
|
9 | content = $(do
| ^^^^...
```

The `data-file` path is provided like this:
```
dataFile :: IO FilePath
dataFile = fmap ( "my-data-file") getDataDir
```
and consumed in template Haskell like this:
```
content :: String
content = $(do
content <- runIO $ readFile =<< dataFile
[|content|])
```

This uses `Cabal` the library by executing `runghc Setup.hs` directly.

**Expected behavior**

Compilation should succeed without error. The `data-file` path of a `--enable-relocatable` package should point to the correct location also under template Haskell.

**System information**
- Ubuntu 19.10
- GHC 8.8.2 (installed from `http://ppa.launchpad.net/hvr/ghc/ubuntu`)
- Cabal3.0.1.0

**Additional context**
This was discovered in the context of `rules_haskell`, see https://github.com/tweag/rules_haskell/issues/1246. The `shake` library tries to embed `js-jquery` using `file-embed` when configured with `-f embed-files`. In `rules_haskell` we use `--enable-relocatable` to ensure that no absolute paths leak into the build artifacts, see https://github.com/tweag/rules_haskell/issues/874#issuecomment-492147644. This is a requirement for distributed caching to work across machines and developers under Bazel.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.