Skip to content

Commit

Permalink
Merge pull request #6235 from commercialhaskell/fix6234
Browse files Browse the repository at this point in the history
Fix #6234 Add corresponding `replHook`
  • Loading branch information
mpilgrem authored Sep 12, 2023
2 parents 59923ee + 7dddf5e commit 5faa041
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 49 deletions.
157 changes: 111 additions & 46 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,52 +577,117 @@ which is powered by the
[Haskell Language Server](https://github.com/haskell/haskell-language-server)
(HLS).

If you use Stack to build Stack, command `stack ghci` in the root directory of
the Stack project should work as expected, if you have first commanded
`stack build` once. `stack build` causes Cabal (the library) to create the
automatically generated module `Stack_build`.

If `ghc` is not on your PATH, then Haskell Language Server may report the
following error about `Stack.Constants.ghcShowOptionsOutput`:
~~~text
• Exception when trying to run compile-time code:
ghc: readCreateProcess: does not exist (No such file or directory)
Code: (TH.runIO (readProcess "ghc" ["--show-options"] "")
>>= TH.lift . lines)
• In the untyped splice:
$(TH.runIO (readProcess "ghc" ["--show-options"] "") >>= TH.lift
. lines)
~~~

`ghc` should be on the PATH if you run VS Code itself in the Stack environment:
~~~text
stack exec -- code .
~~~

The following [cradle (`hie.yaml`)](https://github.com/haskell/hie-bios)
should suffice to configure Haskell Language Server (HLS) explicitly for
`./Setup.hs` and each of the buildable components in Stack's Cabal file:
~~~yaml
cradle:
multi:
- path: "./Setup.hs"
config:
cradle:
direct:
arguments: []
- path: "./"
config:
cradle:
stack:
- path: "./src"
component: "stack:lib"
- path: "./app"
component: "stack:exe:stack"
- path: "./tests/integration"
component: "stack:exe:stack-integration-test"
- path: "./tests/unit"
component: "stack:test:stack-unit-test"
~~~
Stack can be built with Stack (which is recommended) or with Cabal (the tool).

=== "Stack"

If you use Stack to build Stack, command `stack ghci` in the root directory
of the Stack project should work as expected, if you have first commanded
`stack build` once. `stack build` causes Cabal (the library) to create the
automatically generated module `Stack_build`.

If `ghc` is not on your PATH, then Haskell Language Server may report the
following error about `Stack.Constants.ghcShowOptionsOutput`:
~~~text
• Exception when trying to run compile-time code:
ghc: readCreateProcess: does not exist (No such file or directory)
Code: (TH.runIO (readProcess "ghc" ["--show-options"] "")
>>= TH.lift . lines)
• In the untyped splice:
$(TH.runIO (readProcess "ghc" ["--show-options"] "") >>= TH.lift
. lines)
~~~

`ghc` should be on the PATH if you run VS Code itself in the Stack
environment:
~~~text
stack exec -- code .
~~~

The following [cradle (`hie.yaml`)](https://github.com/haskell/hie-bios)
should suffice to configure Haskell Language Server (HLS) explicitly for
`./Setup.hs` and each of the buildable components in Stack's Cabal file:
~~~yaml
cradle:
multi:
- path: "./Setup.hs"
config:
cradle:
direct:
arguments: []
- path: "./"
config:
cradle:
stack:
- path: "./src"
component: "stack:lib"
- path: "./app"
component: "stack:exe:stack"
- path: "./tests/integration"
component: "stack:exe:stack-integration-test"
- path: "./tests/unit"
component: "stack:test:stack-unit-test"
~~~

=== "Cabal (the tool)"

If you use Cabal (the tool) to build Stack, command `cabal repl` in the root
directory of the Stack project should work as expected, if you have GHC and
(on Windows) MSYS2 on the PATH. Stack's custom `./Setup.hs` causes
`cabal repl` to cause Cabal (the library) to create the automatically
generated module `Stack_build`.

If `ghc` is not on your PATH, then Haskell Language Server may report the
following error about `Stack.Constants.ghcShowOptionsOutput`:
~~~text
• Exception when trying to run compile-time code:
ghc: readCreateProcess: does not exist (No such file or directory)
Code: (TH.runIO (readProcess "ghc" ["--show-options"] "")
>>= TH.lift . lines)
• In the untyped splice:
$(TH.runIO (readProcess "ghc" ["--show-options"] "") >>= TH.lift
. lines)
~~~

`ghc` and (on Windows) MSYS2 should be on the PATH if you run commands
(including `cabal`) in the Stack environment:
~~~text
stack exec --no-ghc-package-path -- cabal repl
~~~

or
~~~text
stack exec --no-ghc-package-path -- code .
~~~

Use of GHC's environment variable `GHC_PACKAGE_PATH` is not compatible with
Cabal (the tool). That is why the `--no-ghc-package-path` flag must be
specified with `stack exec` when relying on Cabal (the tool).

The following [cradle (`hie.yaml`)](https://github.com/haskell/hie-bios)
should suffice to configure Haskell Language Server (HLS) explicitly for
`./Setup.hs` and each of the buildable components in Stack's Cabal file:
~~~yaml
cradle:
multi:
- path: "./Setup.hs"
config:
cradle:
direct:
arguments: []
- path: "./"
config:
cradle:
cabal:
- path: "./src"
component: "lib:stack"
- path: "./app"
component: "exe:stack"
- path: "./tests/integration"
component: "exe:stack-integration-test"
- path: "./tests/unit"
component: "test:stack-unit-test"
~~~

A cradle is not committed to Stack's repository because it imposes a choice of
build tool.
Expand Down
9 changes: 6 additions & 3 deletions Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.PackageIndex
( allPackages, dependencyClosure )
import Distribution.Simple.Setup
( BuildFlags (buildVerbosity), fromFlag )
( BuildFlags (..), ReplFlags (..), fromFlag )
import Distribution.Simple.Utils
( rewriteFileEx, createDirectoryIfMissingVerbose )
import Distribution.Types.PackageName ( unPackageName )
Expand All @@ -31,8 +31,11 @@ import System.FilePath ( (</>) )
main :: IO ()
main = defaultMainWithHooks simpleUserHooks
{ buildHook = \pkg lbi hooks flags -> do
generateBuildModule (fromFlag (buildVerbosity flags)) pkg lbi
buildHook simpleUserHooks pkg lbi hooks flags
generateBuildModule (fromFlag (buildVerbosity flags)) pkg lbi
buildHook simpleUserHooks pkg lbi hooks flags
, replHook = \pkg lbi hooks flags args -> do
generateBuildModule (fromFlag (replVerbosity flags)) pkg lbi
replHook simpleUserHooks pkg lbi hooks flags args
}

generateBuildModule :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO ()
Expand Down

0 comments on commit 5faa041

Please sign in to comment.