@@ -179,13 +179,34 @@ let collectGhostDependencies (fileIndex: int) (trie: TrieNode) (queryTrie: Query
179179 Array.empty)
180180
181181let mkGraph ( files : FileWithAST array ) : Graph < int > =
182+ // File pairs to easily retrieve the signature file from a
183+ let implToSig , sigToImpl =
184+ Array.choose
185+ ( fun f ->
186+ match f.AST with
187+ | ParsedInput.SigFile _ ->
188+ let implIdx =
189+ files
190+ |> Array.skip ( f.Idx + 1 )
191+ |> Array.tryFind ( fun ( implFile : FileWithAST ) -> $" {implFile.AST.FileName}i" = f.File)
192+
193+ Option.map ( fun ( implFile : FileWithAST ) -> ( implFile.Idx, f.Idx), ( f.Idx, implFile.Idx)) implIdx
194+ | ParsedInput.ImplFile _ -> None)
195+ files
196+ |> Array.unzip
197+ |> fun ( implToSig , sigToImpl ) -> Map.ofArray implToSig, Map.ofArray sigToImpl
198+
182199 // Implementation files backed by signatures should be excluded to construct the trie.
200+ // Signature files should link to the implementation index instead.
183201 let trieInput =
184- Array.filter
202+ Array.choose
185203 ( fun f ->
186204 match f.AST with
187- | ParsedInput.SigFile _ -> true
188- | ParsedInput.ImplFile _ -> Array.forall ( fun ( sigFile : FileWithAST ) -> sigFile.File <> $" {f.File}i" ) files)
205+ | ParsedInput.SigFile _ -> Map.tryFind f.Idx sigToImpl |> Option.map ( fun implIdx -> ( f, implIdx))
206+ | ParsedInput.ImplFile _ ->
207+ match Map.tryFind f.Idx implToSig with
208+ | Some _ -> None
209+ | None -> Some( f, f.Idx))
189210 files
190211
191212 let trie = TrieMapping.mkTrie trieInput
@@ -195,7 +216,7 @@ let mkGraph (files: FileWithAST array) : Graph<int> =
195216
196217 let filesWithAutoOpen =
197218 Array.choose
198- ( fun f ->
219+ ( fun ( f , _ ) ->
199220 if AlwaysLinkDetection.doesFileHasAutoOpenBehavior f.AST then
200221 Some f.Idx
201222 else
@@ -221,11 +242,18 @@ let mkGraph (files: FileWithAST array) : Graph<int> =
221242 else
222243 [| 0 .. ( file.Idx - 1 ) |]. Intersect( filesWithAutoOpen) .ToArray()
223244
245+ // Automatically add a link from an implementation to its signature file (if present)
246+ let signatureDependency =
247+ match Map.tryFind file.Idx implToSig with
248+ | None -> Array.empty
249+ | Some sigIdx -> Array.singleton sigIdx
250+
224251 let allDependencies =
225252 [|
226253 yield ! result.FoundDependencies
227254 yield ! ghostDependencies
228255 yield ! topLevelAutoOpenFiles
256+ yield ! signatureDependency
229257 |]
230258 |> Array.distinct
231259
0 commit comments