Skip to content

Commit 0d02ed3

Browse files
committed
Only bring units actually depended on into scope on 9.4+
1 parent ea6850f commit 0d02ed3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

ghcide/src/Development/IDE/Import/FindImports.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Data.Maybe
2929
import System.FilePath
3030
#if MIN_VERSION_ghc(9,3,0)
3131
import GHC.Types.PkgQual
32+
import GHC.Unit.State
3233
#endif
3334

3435
data Import
@@ -147,13 +148,32 @@ locateModule env comp_info exts targetFor modName mbPkgName isSource = do
147148
map snd import_paths
148149
#endif
149150

150-
mbFile <- locateModuleFile ((homeUnitId_ dflags, importPaths dflags) : import_paths') exts targetFor isSource $ unLoc modName
151+
mbFile <- locateModuleFile ((homeUnitId_ dflags, importPaths dflags) : other_imports) exts targetFor isSource $ unLoc modName
151152
case mbFile of
152153
Nothing -> lookupInPackageDB env
153154
Just (uid, file) -> toModLocation uid file
154155
where
155156
dflags = hsc_dflags env
156157
import_paths = mapMaybe (mkImportDirs env) comp_info
158+
other_imports =
159+
#if MIN_VERSION_ghc(9,4,0)
160+
-- On 9.4+ instead of bringing all the units into scope, only bring into scope the units
161+
-- this one depends on
162+
-- This way if you have multiple units with the same module names, we won't get confused
163+
-- For example if unit a imports module M from unit B, when there is also a module M in unit C,
164+
-- and unit a only depends on unit b, without this logic there is the potential to get confused
165+
-- about which module unit a imports.
166+
-- Without multi-component support it is hard to recontruct the dependency environment so
167+
-- unit a will have both unit b and unit c in scope.
168+
map (\uid -> (uid, importPaths (homeUnitEnv_dflags (ue_findHomeUnitEnv uid ue)))) hpt_deps
169+
ue = hsc_unit_env env
170+
units = homeUnitEnv_units $ ue_findHomeUnitEnv (homeUnitId_ dflags) ue
171+
hpt_deps :: [UnitId]
172+
hpt_deps = homeUnitDepends units
173+
#else
174+
import_paths'
175+
#endif
176+
157177
toModLocation uid file = liftIO $ do
158178
loc <- mkHomeModLocation dflags (unLoc modName) (fromNormalizedFilePath file)
159179
#if MIN_VERSION_ghc(9,0,0)

0 commit comments

Comments
 (0)