Skip to content

Commit 59e8bb9

Browse files
authored
Preserve import paths for implicit cradles (#768)
* Preserve import paths for implicit cradles Implicit cradles do not list targets, see discussion in https://github.com/haskell/ghcide/issues/765 * Really preserve import paths
1 parent 0b34b1e commit 59e8bb9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

session-loader/Development/IDE/Session.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ loadSession dir = do
212212

213213
-- New HscEnv for the component in question, returns the new HscEnvEq and
214214
-- a mapping from FilePath to the newly created HscEnvEq.
215-
let new_cache = newComponentCache logger hscEnv uids
215+
let new_cache = newComponentCache logger isImplicit hscEnv uids
216+
isImplicit = isNothing hieYaml
216217
(cs, res) <- new_cache new
217218
-- Modified cache targets for everything else in the hie.yaml file
218219
-- which now uses the same EPS and so on
@@ -369,16 +370,18 @@ setNameCache nc hsc = hsc { hsc_NC = nc }
369370
-- | Create a mapping from FilePaths to HscEnvEqs
370371
newComponentCache
371372
:: Logger
373+
-> Bool -- ^ Is this for an implicit/crappy cradle
372374
-> HscEnv
373375
-> [(InstalledUnitId, DynFlags)]
374376
-> ComponentInfo
375377
-> IO ([(NormalizedFilePath, (IdeResult HscEnvEq, DependencyInfo))], (IdeResult HscEnvEq, DependencyInfo))
376-
newComponentCache logger hsc_env uids ci = do
378+
newComponentCache logger isImplicit hsc_env uids ci = do
377379
let df = componentDynFlags ci
378380
let hscEnv' = hsc_env { hsc_dflags = df
379381
, hsc_IC = (hsc_IC hsc_env) { ic_dflags = df } }
380382

381-
henv <- newHscEnvEq hscEnv' uids
383+
let newFunc = if isImplicit then newHscEnvEqPreserveImportPaths else newHscEnvEq
384+
henv <- newFunc hscEnv' uids
382385
let res = (([], Just henv), componentDependencyInfo ci)
383386
logDebug logger ("New Component Cache HscEnvEq: " <> T.pack (show res))
384387

src/Development/IDE/GHC/Util.hs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Development.IDE.GHC.Util(
3030
setHieDir,
3131
dontWriteHieFiles,
3232
disableWarningsAsErrors,
33-
) where
33+
newHscEnvEqPreserveImportPaths) where
3434

3535
import Control.Concurrent
3636
import Data.List.Extra
@@ -178,24 +178,35 @@ data HscEnvEq = HscEnvEq
178178
-- ^ In memory components for this HscEnv
179179
-- This is only used at the moment for the import dirs in
180180
-- the DynFlags
181-
, envImportPaths :: [String]
182-
-- ^ Import dirs originally configured in this env
183-
-- We remove them to prevent GHC from loading modules on its own
181+
, envImportPaths :: Maybe [String]
182+
-- ^ If Just, import dirs originally configured in this env
183+
-- If Nothing, the env import dirs are unaltered
184184
}
185185

186186
-- | Wrap an 'HscEnv' into an 'HscEnvEq'.
187187
newHscEnvEq :: HscEnv -> [(InstalledUnitId, DynFlags)] -> IO HscEnvEq
188188
newHscEnvEq hscEnv0 deps = do
189189
envUnique <- newUnique
190-
let envImportPaths = importPaths $ hsc_dflags hscEnv0
190+
let envImportPaths = Just $ importPaths $ hsc_dflags hscEnv0
191191
hscEnv = removeImportPaths hscEnv0
192192
return HscEnvEq{..}
193193

194+
-- | Wrap an 'HscEnv' into an 'HscEnvEq'.
195+
newHscEnvEqPreserveImportPaths
196+
:: HscEnv -> [(InstalledUnitId, DynFlags)] -> IO HscEnvEq
197+
newHscEnvEqPreserveImportPaths hscEnv deps = do
198+
let envImportPaths = Nothing
199+
envUnique <- newUnique
200+
return HscEnvEq{..}
201+
194202
-- | Unwrap the 'HscEnv' with the original import paths.
195203
-- Used only for locating imports
196204
hscEnvWithImportPaths :: HscEnvEq -> HscEnv
197-
hscEnvWithImportPaths HscEnvEq{..} =
198-
hscEnv{hsc_dflags = (hsc_dflags hscEnv){importPaths = envImportPaths}}
205+
hscEnvWithImportPaths HscEnvEq{..}
206+
| Just imps <- envImportPaths
207+
= hscEnv{hsc_dflags = (hsc_dflags hscEnv){importPaths = imps}}
208+
| otherwise
209+
= hscEnv
199210

200211
removeImportPaths :: HscEnv -> HscEnv
201212
removeImportPaths hsc = hsc{hsc_dflags = (hsc_dflags hsc){importPaths = []}}

0 commit comments

Comments
 (0)