From 3a553a78d5b5853bcfc23ced2dee5087a0e814e7 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Wed, 1 Nov 2023 21:02:12 +0100 Subject: [PATCH] Correct algorithm to find dependent files. (#16) --- fcs/service_slim.fs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/fcs/service_slim.fs b/fcs/service_slim.fs index 1ea3ecab5dfa..53a4a32ba9ac 100644 --- a/fcs/service_slim.fs +++ b/fcs/service_slim.fs @@ -432,24 +432,35 @@ type InteractiveChecker internal (compilerStateCache) = let filePairs = FilePairMap(sourceFiles) let graph, _trie = DependencyResolution.mkGraph filePairs sourceFiles - let findTransitiveDependencies (startNode : FileIndex) : FileIndex array = + let findTransitiveDependentFiles (startNode : FileIndex) : FileIndex array = let rec dfs (node : FileIndex) (visited : Set) (acc : FileIndex array) : FileIndex array = if (Set.contains node visited) then acc else - let neighbors = graph.[node] - let visited' = Set.add node visited - - let acc' = - Array.fold (fun innerAcc neighbor -> dfs neighbor visited' innerAcc) acc neighbors - - [| yield! acc' ; yield node |] + let newVisited = Set.add node visited + + let consumers = + // Last node in the project cannot have + if node = graph.Count - 1 then + acc + else + // Look if the next nodes depend on the current node + [| (node + 1) .. (graph.Count - 1) |] + |> Array.fold + (fun innerAcc nextIdx -> + if not (Array.contains node graph.[nextIdx]) then + innerAcc + else + dfs nextIdx newVisited innerAcc) + acc + + [| yield node; yield! consumers |] dfs startNode Set.empty Array.empty |> Array.sort let dependentFiles = - findTransitiveDependencies currentFileIdx + findTransitiveDependentFiles currentFileIdx |> Array.sort |> Array.map (fun idx -> Array.item idx fileNames)