Skip to content

Commit 6bf2a72

Browse files
xperiandriclaude
andcommitted
Answer the Navigate-To search that runs while the solution loads
Navigate To runs a search of its own during load, dispatched through `IAdvancedNavigateToSearchService`. F# did not implement it, so every F# project was reported complete and searched not at all, and no full search follows by design — nothing F# declares could be found until the user searched again. The search is a parse away. What it lacked is the project's compilation options, which do not exist yet during load, so `GetFSharpParseResultsAsync` raises. `GetFSharpQuickParseResultsAsync` parses with whatever parsing options the project system has already produced, or defaults: a dictionary read, no reactor, no I/O, which is what makes it safe to call for every document of every project while the solution loads. Those defines can be the wrong ones, and the document's version does not change when the real options arrive, so the version stamp alone would let an approximate parse answer the accurate search: a declaration behind `#if` could be missed, or reported from a branch that never compiles. The cache entry carries whether it was approximate, and the accurate path refuses those, reparsing instead. The loading path takes either, since its contract allows out-of-date results. `SearchCachedDocumentsAsync` follows the shape of the C# and VB service: priority documents and the projects that hold them are searched first, results are reported per document rather than per project so the first ones appear while the rest are still parsing, and each project is reported complete once it is done. The parses of every project take turns on one throttle that leaves a core free, so a search during load cannot take the cores away from the load itself. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 9ae20b1 commit 6bf2a72

5 files changed

Lines changed: 253 additions & 11 deletions

File tree

‎docs/release-notes/.VisualStudio/18.vNext.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixed
88

9+
* Navigate To lists F# declarations while the solution is still loading. Until now the search that runs during load skipped F# entirely, and the full search that follows it is never started, so nothing F# declares could be found until the next search. ([PR #20492](https://github.com/dotnet/fsharp/pull/20492))
910
* Improve Find All References performance by throttling parallel typechecks. ([PR #20128](https://github.com/dotnet/fsharp/pull/20128))
1011
* Fixed Rename incorrectly renaming `get` and `set` keywords for properties with explicit accessors. ([Issue #18270](https://github.com/dotnet/fsharp/issues/18270), [PR #19252](https://github.com/dotnet/fsharp/pull/19252))
1112
* Fixed Find All References crash when F# project contains non-F# files like `.cshtml`. ([Issue #16394](https://github.com/dotnet/fsharp/issues/16394), [PR #19252](https://github.com/dotnet/fsharp/pull/19252))

‎vsintegration/src/FSharp.Editor/LanguageService/WorkspaceExtensions.fs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,12 @@ type Document with
581581
return! checker.ParseDocument(this, parsingOptions, userOpName)
582582
}
583583

584+
/// Parses the given F# document with the parsing options its project has already produced, or with defaults when
585+
/// it has none yet: the only parse available while the project system is still loading. The defines can be the
586+
/// wrong ones, so the tree describes a compilation that may never happen.
587+
member this.GetFSharpQuickParseResultsAsync(userOpName) =
588+
this.GetFSharpChecker().ParseDocument(this, this.GetFSharpQuickParsingOptions(), userOpName)
589+
584590
/// Parses and checks the given F# document.
585591
member this.GetFSharpParseAndCheckResultsAsync(userOpName) =
586592
cancellableTask {

‎vsintegration/src/FSharp.Editor/Navigation/NavigateToSearchService.fs‎

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ open System.Collections.Immutable
99
open System.Collections.Concurrent
1010
open System.Globalization
1111
open System.Linq
12+
open System.Threading
1213
open System.Threading.Tasks
1314

1415
open Microsoft.CodeAnalysis
@@ -20,15 +21,24 @@ open Microsoft.VisualStudio.Text.PatternMatching
2021
open FSharp.Compiler.EditorServices
2122
open CancellableTasks
2223

24+
/// The navigable items of one parse of a document, and the text version it was taken from.
25+
[<Struct>]
26+
type private NavigableItemsEntry =
27+
{
28+
Version: VersionStamp
29+
/// Parsed without the project's compilation options, while the solution was still loading.
30+
Approximate: bool
31+
Items: NavigableItem array
32+
}
33+
2334
/// Parse-tree navigable items per document, cached on the document's text version.
2435
/// Shared by NavigateTo and by the Copilot chat mention provider.
2536
[<Export; Shared>]
2637
type internal FSharpNavigableItemsCache
2738
[<ImportingConstructor>]
2839
(patternMatcherFactory: IPatternMatcherFactory, [<Import(AllowDefault = true)>] workspace: VisualStudioWorkspace) =
2940

30-
let cache =
31-
ConcurrentDictionary<DocumentId, struct (VersionStamp * NavigableItem array)>()
41+
let cache = ConcurrentDictionary<DocumentId, NavigableItemsEntry>()
3242

3343
do
3444
match workspace with
@@ -38,18 +48,43 @@ type internal FSharpNavigableItemsCache
3848
if e.NewSolution.Id <> e.OldSolution.Id then
3949
cache.Clear())
4050

51+
let store (document: Document) version approximate parseTree =
52+
let items = NavigateTo.GetNavigableItems parseTree
53+
54+
cache[document.Id] <-
55+
{
56+
Version = version
57+
Approximate = approximate
58+
Items = items
59+
}
60+
61+
items
62+
4163
member _.GetNavigableItems(document: Document) =
4264
cancellableTask {
4365
let! ct = CancellableTask.getCancellationToken ()
4466
let! currentVersion = document.GetTextVersionAsync(ct)
4567

4668
match cache.TryGetValue document.Id with
47-
| true, struct (version, items) when version = currentVersion -> return items
69+
| true, entry when entry.Version = currentVersion && not entry.Approximate -> return entry.Items
4870
| _ ->
4971
let! parseResults = document.GetFSharpParseResultsAsync(nameof (FSharpNavigableItemsCache))
50-
let items = NavigateTo.GetNavigableItems parseResults.ParseTree
51-
cache[document.Id] <- struct (currentVersion, items)
52-
return items
72+
return store document currentVersion false parseResults.ParseTree
73+
}
74+
75+
/// The items of a parse that does not wait for the project's compilation options, for the search that runs while
76+
/// the solution is still loading. A file behind `#if` can be read under the wrong defines, so the entry it leaves
77+
/// behind never answers `GetNavigableItems`.
78+
member _.GetNavigableItemsWhileLoading(document: Document) =
79+
cancellableTask {
80+
let! ct = CancellableTask.getCancellationToken ()
81+
let! currentVersion = document.GetTextVersionAsync(ct)
82+
83+
match cache.TryGetValue document.Id with
84+
| true, entry when entry.Version = currentVersion -> return entry.Items
85+
| _ ->
86+
let! parseResults = document.GetFSharpQuickParseResultsAsync(nameof (FSharpNavigableItemsCache))
87+
return store document currentVersion true parseResults.ParseTree
5388
}
5489

5590
member _.CreateMatcherFor(searchPattern: string) =
@@ -83,7 +118,9 @@ type internal FSharpNavigableItemsCache
83118
[<Export(typeof<IFSharpNavigateToSearchService>); Shared>]
84119
type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache: FSharpNavigableItemsCache) =
85120

86-
let getNavigableItems (document: Document) = itemsCache.GetNavigableItems document
121+
/// The parses of the search that runs while the solution loads take turns across all its projects, and leave a
122+
/// core to the load itself.
123+
let loadingThrottle = new SemaphoreSlim(max 1 (Environment.ProcessorCount - 1))
87124

88125
let kindsProvided =
89126
ImmutableHashSet.Create(
@@ -156,13 +193,18 @@ type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache
156193
let createMatcherFor (searchPattern: string) =
157194
itemsCache.CreateMatcherFor searchPattern
158195

159-
let processDocument (tryMatch: NavigableItem -> PatternMatch voption) (kinds: IImmutableSet<string>) (document: Document) =
196+
let processDocument
197+
(getItems: Document -> CancellableTask<NavigableItem array>)
198+
(tryMatch: NavigableItem -> PatternMatch voption)
199+
(kinds: IImmutableSet<string>)
200+
(document: Document)
201+
=
160202
cancellableTask {
161203
let! ct = CancellableTask.getCancellationToken ()
162204

163205
let! sourceText = document.GetTextAsync ct
164206

165-
let! items = getNavigableItems document
207+
let! items = getItems document
166208

167209
let processed =
168210
seq {
@@ -200,6 +242,22 @@ type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache
200242
return processed |> Seq.toImmutableArray
201243
}
202244

245+
/// Priority items first, each half in its original order, as NavigateTo's own service orders its work.
246+
let prioritize isPriority items =
247+
let priority, rest = items |> Seq.toArray |> Array.partition isPriority
248+
[| yield! priority; yield! rest |]
249+
250+
let throttled (work: CancellableTask<'a>) =
251+
cancellableTask {
252+
let! ct = CancellableTask.getCancellationToken ()
253+
do! loadingThrottle.WaitAsync ct
254+
255+
try
256+
return! work
257+
finally
258+
loadingThrottle.Release() |> ignore
259+
}
260+
203261
interface IFSharpNavigateToSearchService with
204262
member _.SearchProjectAsync
205263
(project, _priorityDocuments, searchPattern, kinds, cancellationToken)
@@ -209,16 +267,60 @@ type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache
209267

210268
let! results =
211269
project.Documents
212-
|> Seq.map (processDocument tryMatch kinds)
270+
|> Seq.map (processDocument itemsCache.GetNavigableItems tryMatch kinds)
213271
|> CancellableTask.whenAll
214272

215273
return results |> Seq.collect _.AsEnumerable() |> Seq.toImmutableArray
216274
}
217275
|> CancellableTask.start cancellationToken
218276

219277
member _.SearchDocumentAsync(document: Document, searchPattern, kinds, cancellationToken) =
220-
processDocument (createMatcherFor searchPattern) kinds document cancellationToken
278+
processDocument itemsCache.GetNavigableItems (createMatcherFor searchPattern) kinds document cancellationToken
221279

222280
member _.KindsProvided = kindsProvided
223281

224282
member _.CanFilter = true
283+
284+
interface IFSharpAdvancedNavigateToSearchService with
285+
member _.SearchCachedDocumentsAsync
286+
(
287+
_solution,
288+
projects,
289+
priorityDocuments,
290+
searchPattern,
291+
kinds,
292+
_activeDocument,
293+
onResultsFound,
294+
onProjectCompleted,
295+
cancellationToken
296+
) : Task =
297+
let tryMatch = createMatcherFor searchPattern
298+
let priorityIds = ImmutableHashSet.CreateRange(priorityDocuments |> Seq.map _.Id)
299+
let isPriority (document: Document) = priorityIds.Contains document.Id
300+
301+
let searchDocumentWhileLoading document =
302+
cancellableTask {
303+
let! results = throttled (processDocument itemsCache.GetNavigableItemsWhileLoading tryMatch kinds document)
304+
305+
if results.Length > 0 then
306+
do! onResultsFound.Invoke results
307+
}
308+
309+
// Every document waits on the throttle in the order it is started, so priority documents, and the
310+
// projects that hold them, are parsed first.
311+
let searchProjectWhileLoading (project: Project) =
312+
cancellableTask {
313+
let! _ =
314+
project.Documents
315+
|> prioritize isPriority
316+
|> Seq.map searchDocumentWhileLoading
317+
|> CancellableTask.whenAll
318+
319+
do! onProjectCompleted.Invoke()
320+
}
321+
322+
projects
323+
|> prioritize (fun project -> project.Documents |> Seq.exists isPriority)
324+
|> Seq.map searchProjectWhileLoading
325+
|> CancellableTask.whenAll
326+
|> CancellableTask.startAsTask cancellationToken

‎vsintegration/tests/FSharp.Editor.Tests/FSharp.Editor.Tests.fsproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="QuickInfoTests.fs" />
3333
<Compile Include="TaskListServiceTests.fs" />
3434
<Compile Include="NavigateToSearchServiceTests.fs" />
35+
<Compile Include="NavigateToSearchWhileLoadingTests.fs" />
3536
<Compile Include="CopilotContextProviderTests.fs" />
3637
<Compile Include="CodeFixes\CodeFixTestFramework.fs" />
3738
<Compile Include="CodeFixes\AddInstanceMemberParameterTests.fs" />
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
/// Navigate To runs a search of its own while the solution is still loading, and nothing it searches
4+
/// then has its compilation options yet.
5+
module FSharp.Editor.Tests.NavigateToSearchWhileLoadingTests
6+
7+
open System
8+
open System.Collections.Immutable
9+
open System.Threading
10+
open System.Threading.Tasks
11+
12+
open Xunit
13+
14+
open Microsoft.CodeAnalysis
15+
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.NavigateTo
16+
17+
open FSharp.Editor.Tests.Helpers
18+
19+
/// A project the project system has not yet handed its command line options: the state every project
20+
/// of the solution is in until it has loaded.
21+
let private loadingProject name source =
22+
let projectId = ProjectId.CreateNewId()
23+
24+
[ RoslynTestHelpers.CreateDocumentInfo projectId $"C:\\{name}.fs" source ]
25+
|> RoslynTestHelpers.CreateProjectInfo projectId $"C:\\{name}.fsproj"
26+
27+
let private loadingSolution source =
28+
let project = loadingProject "test" source
29+
let solution = RoslynTestHelpers.CreateSolution [ project ]
30+
project.Id, solution, solution.Projects |> Seq.exactlyOne
31+
32+
/// One export provider per test: the navigable items are cached in a shared one.
33+
let private searchServices () =
34+
let service: IFSharpNavigateToSearchService =
35+
MefHelpers.createExportProvider().GetExportedValue()
36+
37+
service, service :?> IFSharpAdvancedNavigateToSearchService
38+
39+
let private namesFound (results: ImmutableArray<FSharpNavigateToSearchResult>) = results |> Seq.map _.Name |> Seq.toList
40+
41+
/// The loading search as the searcher drives it: results and project completions arrive through callbacks.
42+
/// Returns the names found and how many times a project was reported complete.
43+
let private searchWhileLoading
44+
(service: IFSharpNavigateToSearchService, advanced: IFSharpAdvancedNavigateToSearchService)
45+
(projects: Project list)
46+
pattern
47+
=
48+
task {
49+
let found = ResizeArray()
50+
let completed = ref 0
51+
52+
do!
53+
advanced.SearchCachedDocumentsAsync(
54+
(List.head projects).Solution,
55+
ImmutableArray.CreateRange projects,
56+
ImmutableArray.Empty,
57+
pattern,
58+
service.KindsProvided,
59+
null,
60+
(fun results ->
61+
lock found (fun () -> found.AddRange results)
62+
Task.CompletedTask),
63+
(fun () ->
64+
Interlocked.Increment &completed.contents |> ignore
65+
Task.CompletedTask),
66+
CancellationToken.None
67+
)
68+
69+
return found |> Seq.map _.Name |> Seq.toList, completed.Value
70+
}
71+
72+
[<Fact>]
73+
let ``the loading search finds what the search that waits for the options cannot`` () : Task =
74+
task {
75+
let _, _, project =
76+
loadingSolution "module Sample =\n let declaredWhileLoading = 1\n"
77+
78+
let (service, _) as services = searchServices ()
79+
80+
let searchAccurately () =
81+
service.SearchProjectAsync(project, ImmutableArray.Empty, "declaredWhileLoading", service.KindsProvided, CancellationToken.None)
82+
:> Task
83+
84+
let! _ = Assert.ThrowsAnyAsync<OperationCanceledException>(fun () -> searchAccurately ())
85+
86+
let! names, completed = searchWhileLoading services [ project ] "declaredWhileLoading"
87+
88+
Assert.Equal<string list>([ "declaredWhileLoading" ], names)
89+
Assert.Equal(1, completed)
90+
91+
// What the loading search left in the cache must not be handed to the accurate search: it was
92+
// read without the project's defines.
93+
let! _ = Assert.ThrowsAnyAsync<OperationCanceledException>(fun () -> searchAccurately ())
94+
()
95+
}
96+
97+
[<Fact>]
98+
let ``the loading search reads a file under the wrong defines and does not keep the answer`` () : Task =
99+
task {
100+
let projectId, solution, project =
101+
loadingSolution "#if FOO\nlet fooOnly = 1\n#endif\n"
102+
103+
let (service, _) as services = searchServices ()
104+
105+
let! namesWhileLoading, _ = searchWhileLoading services [ project ] "fooOnly"
106+
Assert.Equal<string list>([], namesWhileLoading)
107+
108+
{ RoslynTestHelpers.DefaultProjectOptions with
109+
OtherOptions = [| "--define:FOO" |]
110+
}
111+
|> RoslynTestHelpers.SetProjectOptions projectId solution
112+
113+
let! found = service.SearchProjectAsync(project, ImmutableArray.Empty, "fooOnly", service.KindsProvided, CancellationToken.None)
114+
115+
Assert.Equal<string list>([ "fooOnly" ], namesFound found)
116+
}
117+
118+
[<Fact>]
119+
let ``every project is reported complete once, whether or not anything is found in it`` () : Task =
120+
task {
121+
let solution =
122+
RoslynTestHelpers.CreateSolution
123+
[
124+
loadingProject "first" "let found = 1\n"
125+
loadingProject "second" "let other = 2\n"
126+
]
127+
128+
let! names, completed = searchWhileLoading (searchServices ()) (solution.Projects |> Seq.toList) "found"
129+
130+
Assert.Equal<string list>([ "found" ], names)
131+
Assert.Equal(2, completed)
132+
}

0 commit comments

Comments
 (0)