diff --git a/src/FsAutoComplete.Core/CompilerServiceInterface.fs b/src/FsAutoComplete.Core/CompilerServiceInterface.fs index 4fa32b589..6835368fa 100644 --- a/src/FsAutoComplete.Core/CompilerServiceInterface.fs +++ b/src/FsAutoComplete.Core/CompilerServiceInterface.fs @@ -211,24 +211,10 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe } member self.GetProjectOptionsFromScript(file: string, source, tfm) = - async { - let! (projOptions, errors) = - match tfm with - | FSIRefs.TFM.NetFx -> self.GetNetFxScriptOptions(file, source) - | FSIRefs.TFM.NetCore -> self.GetNetCoreScriptOptions(file, source) - - match errors with - | [] -> () - | errs -> - optsLogger.info ( - Log.setLogLevel LogLevel.Error - >> Log.setMessage "Resolved {opts} with {errors}" - >> Log.addContextDestructured "opts" projOptions - >> Log.addContextDestructured "errors" errs - ) + match tfm with + | FSIRefs.TFM.NetFx -> self.GetNetFxScriptOptions(file, source) + | FSIRefs.TFM.NetCore -> self.GetNetCoreScriptOptions(file, source) - return projOptions - } member __.ScriptTypecheckRequirementsChanged = scriptTypecheckRequirementsChanged.Publish diff --git a/src/FsAutoComplete.Core/CompilerServiceInterface.fsi b/src/FsAutoComplete.Core/CompilerServiceInterface.fsi index c7db49506..f332a9a27 100644 --- a/src/FsAutoComplete.Core/CompilerServiceInterface.fsi +++ b/src/FsAutoComplete.Core/CompilerServiceInterface.fsi @@ -4,14 +4,11 @@ open System.IO open FSharp.Compiler.CodeAnalysis open Utils open FSharp.Compiler.Text -open FsAutoComplete.Logging open Ionide.ProjInfo.ProjectSystem open FSharp.UMX open FSharp.Compiler.EditorServices open FSharp.Compiler.Symbols -open Microsoft.Extensions.Caching.Memory -open System -open FsToolkit.ErrorHandling +open FSharp.Compiler.Diagnostics type Version = int @@ -27,7 +24,8 @@ type FSharpCompilerServiceChecker = (FSharpProjectOptions * FSharpProjectOptions list) option member GetProjectOptionsFromScript: - file: string * source: ISourceText * tfm: FSIRefs.TFM -> Async + file: string * source: ISourceText * tfm: FSIRefs.TFM -> + Async member ScriptTypecheckRequirementsChanged: IEvent diff --git a/src/FsAutoComplete/LspServers/AdaptiveServerState.fs b/src/FsAutoComplete/LspServers/AdaptiveServerState.fs index b34bde74a..2cfb64369 100644 --- a/src/FsAutoComplete/LspServers/AdaptiveServerState.fs +++ b/src/FsAutoComplete/LspServers/AdaptiveServerState.fs @@ -1186,22 +1186,40 @@ type AdaptiveState(lspClient: FSharpLspClient, sourceTextFactory: ISourceTextFac and! tfmConfig = tfmConfig let! projs = - asyncOption { + asyncResult { let cts = getOpenFileTokenOrDefault filePath use linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ctok, cts) - let! opts = - checker.GetProjectOptionsFromScript(filePath, file.Source, tfmConfig) - |> Async.withCancellation linkedCts.Token + try + let! (opts, errors) = + checker.GetProjectOptionsFromScript(filePath, file.Source, tfmConfig) + |> Async.withCancellation linkedCts.Token + + opts |> scriptFileProjectOptions.Trigger + let diags = errors |> Array.ofList |> Array.map fcsErrorToDiagnostic + + diagnosticCollections.SetFor( + Path.LocalPathToUri filePath, + "F# Script Project Options", + file.Version, + diags + ) - opts |> scriptFileProjectOptions.Trigger + return + { FSharpProjectOptions = opts + LanguageVersion = LanguageVersionShim.fromFSharpProjectOptions opts } + |> List.singleton + with e -> + logger.error ( + Log.setMessage "Error getting project options for {filePath}" + >> Log.addContextDestructured "filePath" filePath + >> Log.addExn e + ) - return - { FSharpProjectOptions = opts - LanguageVersion = LanguageVersionShim.fromFSharpProjectOptions opts } + return! Error $"Error getting project options for {filePath} - {e.Message}" } - return file, (Option.toList projs |> Ok) + return file, projs else let! projs = sourceFileToProjectOptions diff --git a/test/FsAutoComplete.Tests.Lsp/CompletionTests.fs b/test/FsAutoComplete.Tests.Lsp/CompletionTests.fs index b519a34ac..5fd468981 100644 --- a/test/FsAutoComplete.Tests.Lsp/CompletionTests.fs +++ b/test/FsAutoComplete.Tests.Lsp/CompletionTests.fs @@ -21,7 +21,7 @@ let tests state = do! server.TextDocumentDidOpen tdop let! _diagnostics = - waitForParseResultsForFile "Script.fsx" events + waitForDiagnosticErrorForFile "Script.fsx" events |> AsyncResult.bimap (fun _ -> failtest "Should have had errors") (fun e -> e) return (server, path) @@ -743,7 +743,7 @@ let autoOpenTests state = do! server.TextDocumentDidOpen tdop do! - waitForParseResultsForFile scriptName events + waitForDiagnosticErrorForFile scriptName events |> AsyncResult.bimap (fun _ -> failtest "Should have had errors") id |> Async.Ignore diff --git a/test/FsAutoComplete.Tests.Lsp/Helpers.fs b/test/FsAutoComplete.Tests.Lsp/Helpers.fs index 366a21d30..23a459b20 100644 --- a/test/FsAutoComplete.Tests.Lsp/Helpers.fs +++ b/test/FsAutoComplete.Tests.Lsp/Helpers.fs @@ -689,6 +689,8 @@ let diagnosticsToResult = let waitForParseResultsForFile file = fileDiagnostics file >> diagnosticsToResult >> Async.AwaitObservable +let waitForDiagnosticErrorForFile file = fileDiagnostics file >> Observable.choose (function | [||] -> None | diags -> Some diags) >> diagnosticsToResult >> Async.AwaitObservable + let waitForFsacDiagnosticsForFile file = fsacDiagnostics file >> diagnosticsToResult >> Async.AwaitObservable let waitForCompilerDiagnosticsForFile file = compilerDiagnostics file >> diagnosticsToResult >> Async.AwaitObservable diff --git a/test/FsAutoComplete.Tests.Lsp/Helpers.fsi b/test/FsAutoComplete.Tests.Lsp/Helpers.fsi index 1d022ef8f..7dc01c165 100644 --- a/test/FsAutoComplete.Tests.Lsp/Helpers.fsi +++ b/test/FsAutoComplete.Tests.Lsp/Helpers.fsi @@ -111,6 +111,7 @@ val fsacDiagnostics: file: string -> (IObservable -> IObservable (IObservable -> IObservable) val diagnosticsToResult: (IObservable -> IObservable>) val waitForParseResultsForFile: file: string -> (IObservable -> Async>) +val waitForDiagnosticErrorForFile: file: string -> (IObservable -> Async>) val waitForFsacDiagnosticsForFile: file: string -> (IObservable -> Async>) val waitForCompilerDiagnosticsForFile: