haskell / haskell/haskell-language-server

Loading Setup.hs into HLS

Open
#3,735 3 comments 12 reactions 0 assignees View on GitHub
build tool: cabal type: enhancement
Dominant language
Haskell
Stars
3k
Forks
455
Avg merge
2d 19h
Merged PRs (30d)
11

Description

## Problem

`Setup.hs` is the build Script used by Cabal to actually build a cabal or stack package.
It usually takes the trivial form of
```haskell
import Distribution.Simple
main = defaultMain
```
where it can be omitted.

However, there exists a number of `Custom` `Setup.hs` files that are far more complex, see for example [this `Setup.hs` from cabal-testsuite](https://github.com/haskell/cabal/blob/a40876dbdb0fe624cb8c64453ece3c768a79832c/cabal-testsuite/Setup.hs)

Presently, it is impossible to load Setup.hs into HLS, neither the trivial `Setup.hs` nor the custom `Setup.hs`.
Users are bombarded with intrusive and impossible to decipher error messages such as:

```
Multi Cradle: No prefixes matched
pwd: /home/hugin/Documents/haskell/hie-bios
filepath: /home/hugin/Documents/haskell/hie-bios/Setup.hs
prefixes:
("./src",Cabal {component = Just "lib:hie-bios"})
("./exe/Main.hs",Cabal {component = Just "hie-bios:exe:hie-bios"})
("./exe/Paths_hie_bios.hs",Cabal {component = Just "hie-bios:exe:hie-bios"})
("./tests/",Cabal {component = Just "hie-bios:test:parser-tests"})
("./tests/",Cabal {component = Just "hie-bios:test:bios-tests"})
cradle
```
and
```
Failed to run ["cabal","v2-repl","/home/hugin/Documents/haskell/cabal/cabal-testsuite/Setup.hs"] in directory "/home/hugin/Documents/haskell/cabal". Consult the logs for full command and error.
Failed command: cabal --builddir=/home/hugin/.cache/hie-bios/dist-cabal-6feed062e565a5220d2d16ac7598fdb5 v2-repl --with-compiler /home/hugin/.cache/hie-bios/wrapper-b54f81dea4c0e6d1626911c526bc4e36 --with-hc-pkg /home/hugin/.cache/hie-bios/ghc-pkg-582c31b3f45642fb349d02d1e6665c7b /home/hugin/Documents/haskell/cabal/cabal-testsuite/Setup.hs

Error: cabal: Failed extracting script block: `{- cabal:` start marker not
found
```

## Solution

To properly load `Setup.hs`, build tools, such as cabal and stack, need to be able to tell us what compilation options we need to build `Setup.hs` files.

* Cabal likely has some support via [`build-info` files](https://cabal.readthedocs.io/en/latest/cabal-commands.html#cmdoption-enable-build-info). However, hie-bios makes no attempt to use these files.
* There is currently no known way to find the compilation options for `Setup.hs` from within Stack projects.

We could also ignore any `Setup.hs` files using some heuristics, so that users are not greeted with the horrible error messages shown above:

## Workarounds

There exists one known workaround by exploiting the [`cabal repl Scriptfile.hs` feature](https://cabal.readthedocs.io/en/latest/getting-started.html#run-a-single-file-haskell-script) on recent cabal versions only.
Requires a `cabal.project` file in the project.

Example:
```haskell
#!/usr/bin/env cabal
{- cabal:
build-depends: base, Cabal
-}
import Distribution.Simple
main = defaultMain
```

in combination with the following `hie.yaml`
```yaml
cradle:
cabal:
```

---

Ignoring the `Setup.hs` can be achieved with a custom `hie.yaml` file at the root of the project, such as:

```yaml
cradle:
multi:
- path: "./Setup.hs"
config:
cradle:
none:
- path: "./"
config:
cradle:
cabal:
```
---

This is a meta-issue and should subsume all `Setup.hs` related issues.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.