Skip to content

Commit 1f6f517

Browse files
committed
session-loader: Set working directory on GHC 9.4+
We set the working directory appropriately so that TH code can access it and read files relative to the root of the current component. Fixes #481
1 parent 6d6907a commit 1f6f517

File tree

8 files changed

+42
-2
lines changed

8 files changed

+42
-2
lines changed

ghcide/session-loader/Development/IDE/Session.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags = do
11151115
Nothing -> compRoot
11161116
Just wdir -> compRoot </> wdir
11171117
let dflags''' =
1118+
setWorkingDirectory root $
11181119
disableWarningsAsErrors $
11191120
-- disabled, generated directly by ghcide instead
11201121
flip gopt_unset Opt_WriteInterface $
@@ -1125,7 +1126,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags = do
11251126
setBytecodeLinkerOptions $
11261127
disableOptimisation $
11271128
Compat.setUpTypedHoles $
1128-
makeDynFlagsAbsolute compRoot
1129+
makeDynFlagsAbsolute root
11291130
dflags''
11301131
-- initPackages parses the -package flags and
11311132
-- sets up the visibility for each component.

ghcide/src/Development/IDE/GHC/Compat/Env.hs

+8-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ module Development.IDE.GHC.Compat.Env (
5555
setBackend,
5656
ghciBackend,
5757
Development.IDE.GHC.Compat.Env.platformDefaultBackend,
58-
workingDirectory
58+
workingDirectory,
59+
setWorkingDirectory,
5960
) where
6061

6162
import GHC (setInteractiveDynFlags)
@@ -91,6 +92,12 @@ hsc_EPS = Env.hsc_unit_env
9192
#if !MIN_VERSION_ghc(9,3,0)
9293
workingDirectory :: a -> Maybe b
9394
workingDirectory _ = Nothing
95+
96+
setWorkingDirectory :: FilePath -> DynFlags -> DynFlags
97+
setWorkingDirectory = const id
98+
#else
99+
setWorkingDirectory :: FilePath -> DynFlags -> DynFlags
100+
setWorkingDirectory p d = d { workingDirectory = Just p }
94101
#endif
95102

96103
setHomeUnitId_ :: UnitId -> DynFlags -> DynFlags

ghcide/test/data/working-dir/a/A.hs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module A(th_a) where
3+
4+
import Language.Haskell.TH
5+
import Language.Haskell.TH.Syntax
6+
import Control.Monad.IO.Class
7+
8+
th_a :: DecsQ
9+
th_a = do
10+
str <- makeRelativeToProject "wdtest" >>= liftIO . readFile
11+
[d| a = $(lift str) |]

ghcide/test/data/working-dir/a/B.hs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module B() where
3+
4+
import A
5+
6+
$th_a
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: a
2+
version: 1.0.0
3+
build-type: Simple
4+
cabal-version: >= 1.2
5+
extra-source-files: wdtest
6+
7+
library
8+
build-depends: base, template-haskell
9+
exposed-modules: A B
10+
ghc-options: -Wmissing-signatures
11+
hs-source-dirs: .

ghcide/test/data/working-dir/a/wdtest

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: a

ghcide/test/data/working-dir/hie.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cradle:
2+
cabal:

0 commit comments

Comments
 (0)