Skip to content

Commit 78fe225

Browse files
committed
Generate a FileTarget for each possible target location
If a target file has multiple possible locations, then we assume they are all separate file targets. This happens with '.hs-boot' files if they are in the root directory of the project. GHC reports options such as '-i. A' as 'TargetFile A.hs' instead of 'TargetModule A'. In 'fromTargetId', we dutifully look for '.hs-boot' files and add them to the targetLocations of the TargetDetails. Then we add everything to the 'knownTargetsVar'. However, when we look for a 'Foo.hs-boot' file in 'FindImports.hs', we look for either * TargetFile Foo.hs-boot * TargetModule Foo If we don't generate a TargetFile for each potential location, we will only have 'TargetFile Foo.hs' in the 'knownTargetsVar', thus not find 'TargetFile Foo.hs-boot' and also not find 'TargetModule Foo'.
1 parent e4a3e44 commit 78fe225

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

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

+31-15
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import Data.Hashable hiding (hash)
4141
import qualified Data.HashMap.Strict as HM
4242
import Data.IORef
4343
import Data.List
44-
import Data.List.NonEmpty (NonEmpty (..))
4544
import Data.List.Extra as L
45+
import Data.List.NonEmpty (NonEmpty (..))
4646
import qualified Data.List.NonEmpty as NE
4747
import qualified Data.Map.Strict as Map
4848
import Data.Maybe
@@ -113,22 +113,23 @@ import System.Random (RandomGen)
113113

114114
import qualified Development.IDE.Session.Implicit as GhcIde
115115

116-
import Development.IDE.GHC.Compat.CmdLine
116+
import Development.IDE.GHC.Compat.CmdLine
117117

118118

119119
-- See Note [Guidelines For Using CPP In GHCIDE Import Statements]
120120
#if MIN_VERSION_ghc(9,3,0)
121121
import qualified Data.Set as OS
122122

123-
import GHC.Driver.Errors.Types
124-
import GHC.Driver.Env (hscSetActiveUnitId, hsc_all_home_unit_ids)
125-
import GHC.Driver.Make (checkHomeUnitsClosed)
126-
import GHC.Unit.State
127-
import GHC.Types.Error (errMsgDiagnostic)
128-
import GHC.Data.Bag
123+
import GHC.Data.Bag
124+
import GHC.Driver.Env (hscSetActiveUnitId,
125+
hsc_all_home_unit_ids)
126+
import GHC.Driver.Errors.Types
127+
import GHC.Driver.Make (checkHomeUnitsClosed)
128+
import GHC.Types.Error (errMsgDiagnostic)
129+
import GHC.Unit.State
129130
#endif
130131

131-
import GHC.ResponseFile
132+
import GHC.ResponseFile
132133

133134
data Log
134135
= LogSettingInitialDynFlags
@@ -479,12 +480,27 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
479480
-- files in the project so that `knownFiles` can learn about them and
480481
-- we can generate a complete module graph
481482
let extendKnownTargets newTargets = do
482-
knownTargets <- forM newTargets $ \TargetDetails{..} ->
483+
knownTargets <- concatForM newTargets $ \TargetDetails{..} ->
483484
case targetTarget of
484-
TargetFile f -> pure (targetTarget, [f])
485+
TargetFile _ -> do
486+
-- If a target file has multiple possible locations, then we
487+
-- assume they are all separate file targets.
488+
-- This happens with '.hs-boot' files if they are in the root directory of the project.
489+
-- GHC reports options such as '-i. A' as 'TargetFile A.hs' instead of 'TargetModule A'.
490+
-- In 'fromTargetId', we dutifully look for '.hs-boot' files and add them to the
491+
-- targetLocations of the TargetDetails. Then we add everything to the 'knownTargetsVar'.
492+
-- However, when we look for a 'Foo.hs-boot' file in 'FindImports.hs', we look for either
493+
--
494+
-- * TargetFile Foo.hs-boot
495+
-- * TargetModule Foo
496+
--
497+
-- If we don't generate a TargetFile for each potential location, we will only have
498+
-- 'TargetFile Foo.hs' in the 'knownTargetsVar', thus not find 'TargetFile Foo.hs-boot'
499+
-- and also not find 'TargetModule Foo'.
500+
pure $ map (\f -> (TargetFile f, [f])) (nubOrd targetLocations)
485501
TargetModule _ -> do
486502
found <- filterM (IO.doesFileExist . fromNormalizedFilePath) targetLocations
487-
return (targetTarget, found)
503+
return [(targetTarget, found)]
488504
hasUpdate <- join $ atomically $ do
489505
known <- readTVar knownTargetsVar
490506
let known' = flip mapHashed known $ \k ->
@@ -981,13 +997,13 @@ data ComponentInfo = ComponentInfo
981997
-- | Internal units, such as local libraries, that this component
982998
-- is loaded with. These have been extracted from the original
983999
-- ComponentOptions.
984-
, componentInternalUnits :: [UnitId]
1000+
, componentInternalUnits :: [UnitId]
9851001
-- | All targets of this components.
9861002
, componentTargets :: [GHC.Target]
9871003
-- | Filepath which caused the creation of this component
9881004
, componentFP :: NormalizedFilePath
9891005
-- | Component Options used to load the component.
990-
, componentCOptions :: ComponentOptions
1006+
, componentCOptions :: ComponentOptions
9911007
-- | Maps cradle dependencies, such as `stack.yaml`, or `.cabal` file
9921008
-- to last modification time. See Note [Multi Cradle Dependency Info]
9931009
, componentDependencyInfo :: DependencyInfo
@@ -1112,7 +1128,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags = do
11121128

11131129
let targets = makeTargetsAbsolute root targets'
11141130
root = case workingDirectory dflags'' of
1115-
Nothing -> compRoot
1131+
Nothing -> compRoot
11161132
Just wdir -> compRoot </> wdir
11171133
let dflags''' =
11181134
setWorkingDirectory root $

0 commit comments

Comments
 (0)