diff --git a/tests/benchmarks/CompilerServiceBenchmarks/Program.fs b/tests/benchmarks/CompilerServiceBenchmarks/Program.fs index d17e10031a6..ced6ad6fcd9 100644 --- a/tests/benchmarks/CompilerServiceBenchmarks/Program.fs +++ b/tests/benchmarks/CompilerServiceBenchmarks/Program.fs @@ -2,14 +2,10 @@ open System.IO open System.Text open System.Threading.Tasks -open FSharp.Compiler.ErrorLogger open FSharp.Compiler.CodeAnalysis open FSharp.Compiler.Diagnostics -open FSharp.Compiler.Syntax -open FSharp.Compiler.Symbols open FSharp.Compiler.EditorServices open FSharp.Compiler.Text -open FSharp.Compiler.AbstractIL open FSharp.Compiler.AbstractIL.IL open FSharp.Compiler.AbstractIL.ILBinaryReader open Microsoft.CodeAnalysis.Text diff --git a/tests/service/Common.fs b/tests/service/Common.fs index 812dceecfe6..410faa45c3d 100644 --- a/tests/service/Common.fs +++ b/tests/service/Common.fs @@ -6,6 +6,7 @@ open System.Diagnostics open System.IO open System.Collections.Generic open System.Collections.Immutable +open System.Threading open System.Threading.Tasks open FSharp.Compiler.CodeAnalysis open FSharp.Compiler.IO diff --git a/vsintegration/src/FSharp.Editor/Common/Extensions.fs b/vsintegration/src/FSharp.Editor/Common/Extensions.fs index 801174a567d..6315654f56a 100644 --- a/vsintegration/src/FSharp.Editor/Common/Extensions.fs +++ b/vsintegration/src/FSharp.Editor/Common/Extensions.fs @@ -6,6 +6,7 @@ module internal Microsoft.VisualStudio.FSharp.Editor.Extensions open System open System.IO open System.Collections.Immutable +open System.Threading open System.Threading.Tasks open Microsoft.CodeAnalysis @@ -293,14 +294,17 @@ module Exception = |> String.concat " ---> " type Async with - static member RunImmediate (computation: Async<'T>, ?cancellationToken ) = - let cancellationToken = defaultArg cancellationToken Async.DefaultCancellationToken - let ts = TaskCompletionSource<'T>() - let task = ts.Task - Async.StartWithContinuations( - computation, - (fun k -> ts.SetResult k), - (fun exn -> ts.SetException exn), - (fun _ -> ts.SetCanceled()), - cancellationToken) - task.Result + static member RunImmediateExceptOnUI (computation: Async<'T>, ?cancellationToken ) = + match SynchronizationContext.Current with + | null -> + let cancellationToken = defaultArg cancellationToken Async.DefaultCancellationToken + let ts = TaskCompletionSource<'T>() + let task = ts.Task + Async.StartWithContinuations( + computation, + (fun k -> ts.SetResult k), + (fun exn -> ts.SetException exn), + (fun _ -> ts.SetCanceled()), + cancellationToken) + task.Result + | _ -> Async.RunSynchronously(computation, ?cancellationToken=cancellationToken) diff --git a/vsintegration/src/FSharp.Editor/Formatting/IndentationService.fs b/vsintegration/src/FSharp.Editor/Formatting/IndentationService.fs index 23f42e10ee0..1a87ac9d89d 100644 --- a/vsintegration/src/FSharp.Editor/Formatting/IndentationService.fs +++ b/vsintegration/src/FSharp.Editor/Formatting/IndentationService.fs @@ -107,4 +107,4 @@ type internal FSharpIndentationService match indent with | None -> Nullable() | Some(indentation) -> Nullable(FSharpIndentationResult(sourceText.Lines.[lineNumber].Start, indentation)) - } |> (fun c -> Async.RunImmediate(c,cancellationToken=cancellationToken)) + } |> (fun c -> Async.RunImmediateExceptOnUI(c,cancellationToken=cancellationToken)) diff --git a/vsintegration/src/FSharp.Editor/LanguageService/WorkspaceExtensions.fs b/vsintegration/src/FSharp.Editor/LanguageService/WorkspaceExtensions.fs index 6b685dbe32d..f012f65ed44 100644 --- a/vsintegration/src/FSharp.Editor/LanguageService/WorkspaceExtensions.fs +++ b/vsintegration/src/FSharp.Editor/LanguageService/WorkspaceExtensions.fs @@ -203,7 +203,7 @@ type Document with let workspaceService = this.Project.Solution.GetFSharpWorkspaceService() let parsingOptions, _, _ = workspaceService.FSharpProjectOptionsManager.TryGetOptionsForDocumentOrProject(this, CancellationToken.None, nameof(this.SetFSharpProjectOptionsForTesting)) - |> Async.RunImmediate + |> Async.RunImmediateExceptOnUI |> Option.get ProjectCache.Projects.Add(this.Project, (workspaceService.Checker, workspaceService.FSharpProjectOptionsManager, parsingOptions, projectOptions)) diff --git a/vsintegration/src/FSharp.Editor/Navigation/GoToDefinition.fs b/vsintegration/src/FSharp.Editor/Navigation/GoToDefinition.fs index b6df356276a..2b4ab7b6af2 100644 --- a/vsintegration/src/FSharp.Editor/Navigation/GoToDefinition.fs +++ b/vsintegration/src/FSharp.Editor/Navigation/GoToDefinition.fs @@ -473,7 +473,7 @@ type internal GoToDefinition(metadataAsSource: FSharpMetadataAsSourceService) = } let span = - match Async.RunImmediate(goToAsync, cancellationToken = cancellationToken) with + match Async.RunImmediateExceptOnUI(goToAsync, cancellationToken = cancellationToken) with | Some span -> span | _ -> TextSpan()