Skip to content

Commit d515cfa

Browse files
nojafncave
authored andcommitted
Find all transitive dependent files. (#15)
1 parent 8ec1d1b commit d515cfa

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

fcs/service_slim.fs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ type InteractiveChecker internal (compilerStateCache) =
405405
return (parseFileResults, checkFileResults, projectResults)
406406
}
407407

408-
/// Find the dependent files of the current file based on the untyped syntax tree
408+
/// Find the transitive dependent files of the current file based on the untyped syntax tree.
409409
member _.GetDependentFiles(currentFileName: string, fileNames: string[], sourceReader: string -> int * Lazy<string>) = async {
410410
// parse files
411411
let! ct = Async.CancellationToken
@@ -431,23 +431,27 @@ type InteractiveChecker internal (compilerStateCache) =
431431
let currentFileIdx = Array.IndexOf(fileNames, currentFileName)
432432

433433
let filePairs = FilePairMap(sourceFiles)
434-
let graph, _trie = DependencyResolution.mkGraph filePairs sourceFiles
435-
436-
let dependentFiles =
437-
graph.Keys
438-
|> Seq.choose (fun fileIndex ->
439-
let isDependency =
440-
// Only files that came after the current file are relevant
441-
fileIndex > currentFileIdx
442-
&&
443-
// The current file is listed as a dependency
444-
Array.contains currentFileIdx graph.[fileIndex]
445-
if not isDependency then
446-
None
434+
let graph, _trie = DependencyResolution.mkGraph filePairs sourceFiles
435+
let findTransitiveDependencies (startNode : FileIndex) : FileIndex array =
436+
let rec dfs (node : FileIndex) (visited : Set<FileIndex>) (acc : FileIndex array) : FileIndex array =
437+
if (Set.contains node visited) then
438+
acc
447439
else
448-
Some(Array.item fileIndex fileNames)
449-
)
450-
|> Seq.toArray
440+
let neighbors = graph.[node]
441+
let visited' = Set.add node visited
442+
443+
let acc' =
444+
Array.fold (fun innerAcc neighbor -> dfs neighbor visited' innerAcc) acc neighbors
445+
446+
[| yield! acc' ; yield node |]
447+
448+
dfs startNode Set.empty Array.empty
449+
|> Array.sort
450+
451+
let dependentFiles =
452+
findTransitiveDependencies currentFileIdx
453+
|> Array.sort
454+
|> Array.map (fun idx -> Array.item idx fileNames)
451455

452456
return dependentFiles
453-
}
457+
}

0 commit comments

Comments
 (0)