@@ -27,6 +27,42 @@ import Language.LSP.Server (resRootPath)
27
27
import System.Directory (doesDirectoryExist )
28
28
import System.FilePath ((<.>) , (</>) )
29
29
30
+ {- Note [Going to definitions in dependencies]
31
+ - There are two main components of the functionality that enables gotoDefinition for
32
+ - third party dependencies:
33
+ - + the changes to the lookupMod function in ghcide/src/Development/IDE/Core/Actions.hs,
34
+ - which are triggered on calls to gotoDefinition.
35
+ - + the code that indexes dependencies in the hiedb, which can be found in this module.
36
+ - This gets run asynchronously, triggering every time newHscEnvEqWithImportPaths gets called.
37
+ -
38
+ - The gotoDefinition code was originally written in such a way that it was
39
+ - expecting that we would eventually be able to go to dependency definitions.
40
+ - Before the funtionality was implemented, lookupMod was a no-op stub intended to
41
+ - be where functionality would eventually go for dependencies. You can see the
42
+ - code that eventually ends up calling lookupMod in the function nameToLocation in
43
+ - ghcide/src/Development/IDE/Spans/AtPoint.hs. To summarize, gotoDefinition will look
44
+ - for a file in the project, and look in the hiedb if it can't find it. In this sense,
45
+ - the name lookupMod might be a little misleading, because by the time it gets called,
46
+ - the HIE file has already been looked up in the database and we have the FilePath
47
+ - of its location. A more appropriate name might be something like loadModule,
48
+ - since what it does is load the module source code from an HIE file and write it out to
49
+ - .hls/dependencies. The way nameToLocation works, if we have already opened a
50
+ - dependency file once, lookupMod won't get called. In addition to loading the
51
+ - dependency source and writing it out, lookupMod handles indexing the source file
52
+ - that we wrote out, which can't happen in the initial indexing since the
53
+ - source file doesn't exist at that point. To summarize, for gotoDefinition to work
54
+ - for a dependency we need to have already indexed the HIE file for that dependency module.
55
+ -
56
+ - The indexing process gets the packages and modules for dependencies from the HscEnv.
57
+ - It filters them for packages we know are direct or transitive dependencies, using the
58
+ - function calculateTransitiveDependencies. indexDependencyHieFiles attempts to load an
59
+ - HIE file for each module, checking for it in the extra-compilation-artifacts directory,
60
+ - found in the package lib directory. This fails for the packages that ship with GHC,
61
+ - because it doesn't yet generate HIE files. If it is able to load the HIE file,
62
+ - it indexes it in hiedb using indexHieFile, which is the same function used to
63
+ - index project HIE files.
64
+ -}
65
+
30
66
newtype Package = Package GHC. UnitInfo deriving Eq
31
67
instance Ord Package where
32
68
compare (Package u1) (Package u2) = compare (GHC. unitId u1) (GHC. unitId u2)
0 commit comments