Skip to content

Commit

Permalink
Show script option project diagnostics (ionide#1248)
Browse files Browse the repository at this point in the history
* Show script option project diagnostics

* fix tests
  • Loading branch information
TheAngryByrd authored Mar 15, 2024
1 parent 7d2d2ec commit 6bdbbaa
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
20 changes: 3 additions & 17 deletions src/FsAutoComplete.Core/CompilerServiceInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,10 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe
}

member self.GetProjectOptionsFromScript(file: string<LocalPath>, 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
Expand Down
8 changes: 3 additions & 5 deletions src/FsAutoComplete.Core/CompilerServiceInterface.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,7 +24,8 @@ type FSharpCompilerServiceChecker =
(FSharpProjectOptions * FSharpProjectOptions list) option

member GetProjectOptionsFromScript:
file: string<LocalPath> * source: ISourceText * tfm: FSIRefs.TFM -> Async<FSharpProjectOptions>
file: string<LocalPath> * source: ISourceText * tfm: FSIRefs.TFM ->
Async<FSharpProjectOptions * FSharpDiagnostic list>

member ScriptTypecheckRequirementsChanged: IEvent<unit>

Expand Down
36 changes: 27 additions & 9 deletions src/FsAutoComplete/LspServers/AdaptiveServerState.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/FsAutoComplete.Tests.Lsp/CompletionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/FsAutoComplete.Tests.Lsp/Helpers.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ val fsacDiagnostics: file: string -> (IObservable<string * obj> -> IObservable<D
val compilerDiagnostics: file: string -> (IObservable<string * obj> -> IObservable<Diagnostic array>)
val diagnosticsToResult: (IObservable<Diagnostic array> -> IObservable<Result<unit, Diagnostic array>>)
val waitForParseResultsForFile: file: string -> (IObservable<string * obj> -> Async<Result<unit, Diagnostic array>>)
val waitForDiagnosticErrorForFile: file: string -> (IObservable<string * obj> -> Async<Result<unit, Diagnostic array>>)
val waitForFsacDiagnosticsForFile: file: string -> (IObservable<string * obj> -> Async<Result<unit, Diagnostic array>>)

val waitForCompilerDiagnosticsForFile:
Expand Down

0 comments on commit 6bdbbaa

Please sign in to comment.