Skip to content

Commit dea6492

Browse files
authored
Workaround for #15972 (#15982)
1 parent e6a5c75 commit dea6492

File tree

2 files changed

+94
-9
lines changed

2 files changed

+94
-9
lines changed

vsintegration/src/FSharp.Editor/Diagnostics/DocumentDiagnosticAnalyzer.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ type internal FSharpDocumentDiagnosticAnalyzer [<ImportingConstructor>] () =
7676

7777
let! parseResults = document.GetFSharpParseResultsAsync("GetDiagnostics")
7878

79+
// Old logic, rollback once https://github.com/dotnet/fsharp/issues/15972 is fixed (likely on Roslyn side, since we're returning diagnostics, but they're not getting to VS).
80+
(*
7981
match diagnosticType with
8082
| DiagnosticsType.Syntax ->
8183
for diagnostic in parseResults.Diagnostics do
@@ -88,6 +90,23 @@ type internal FSharpDocumentDiagnosticAnalyzer [<ImportingConstructor>] () =
8890
errors.Add(diagnostic) |> ignore
8991
9092
errors.ExceptWith(parseResults.Diagnostics)
93+
*)
94+
95+
// TODO: see comment above, this is a workaround for issue we have in current VS/Roslyn
96+
match diagnosticType with
97+
| DiagnosticsType.Syntax ->
98+
for diagnostic in parseResults.Diagnostics do
99+
errors.Add(diagnostic) |> ignore
100+
101+
// We always add syntactic, and do not exclude them when semantic is requested
102+
| DiagnosticsType.Semantic ->
103+
for diagnostic in parseResults.Diagnostics do
104+
errors.Add(diagnostic) |> ignore
105+
106+
let! _, checkResults = document.GetFSharpParseAndCheckResultsAsync("GetDiagnostics")
107+
108+
for diagnostic in checkResults.Diagnostics do
109+
errors.Add(diagnostic) |> ignore
91110

92111
if errors.Count = 0 then
93112
return ImmutableArray.Empty

vsintegration/tests/FSharp.Editor.Tests/DocumentDiagnosticAnalyzerTests.fs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,72 @@ type DocumentDiagnosticAnalyzerTests() =
9191
actualError.Location.SourceSpan.End
9292
|> Assert.shouldBeEqualWith expectedEnd "Error end positions should match"
9393

94+
member private this.VerifyDiagnosticBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE
95+
(
96+
fileContents: string,
97+
expectedMessage: string,
98+
expectedSeverity: DiagnosticSeverity
99+
) =
100+
// TODO: once workaround (https://github.com/dotnet/fsharp/pull/15982) will not be needed, this should be reverted back to normal method (see PR)
101+
let errors =
102+
getDiagnostics fileContents
103+
|> Seq.filter (fun e -> e.Severity = expectedSeverity)
104+
|> Seq.toArray
105+
106+
errors.Length
107+
|> Assert.shouldBeEqualWith 2 "There should be two errors generated"
108+
109+
let actualError = errors.[0]
110+
Assert.Equal(expectedSeverity, actualError.Severity)
111+
112+
actualError.GetMessage()
113+
|> Assert.shouldBeEqualWith expectedMessage "Error messages should match"
114+
115+
let expectedStart = fileContents.IndexOf(startMarker) + startMarker.Length
116+
117+
actualError.Location.SourceSpan.Start
118+
|> Assert.shouldBeEqualWith expectedStart "Error start positions should match"
119+
120+
let expectedEnd = fileContents.IndexOf(endMarker)
121+
122+
actualError.Location.SourceSpan.End
123+
|> Assert.shouldBeEqualWith expectedEnd "Error end positions should match"
124+
125+
member private this.VerifyErrorBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(fileContents: string, expectedMessage: string) =
126+
// TODO: once workaround (https://github.com/dotnet/fsharp/pull/15982) will not be needed, this should be reverted back to normal method (see PR)
127+
this.VerifyDiagnosticBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(fileContents, expectedMessage, DiagnosticSeverity.Error)
128+
129+
member private this.VerifyErrorAtMarker_HACK_PLEASE_REFER_TO_COMMENT_INSIDE
130+
(
131+
fileContents: string,
132+
expectedMarker: string,
133+
?expectedMessage: string
134+
) =
135+
let errors =
136+
getDiagnostics fileContents
137+
|> Seq.filter (fun e -> e.Severity = DiagnosticSeverity.Error)
138+
|> Seq.toArray
139+
140+
errors.Length
141+
|> Assert.shouldBeEqualWith 2 "There should be exactly two errors generated"
142+
143+
let actualError = errors.[0]
144+
145+
if expectedMessage.IsSome then
146+
actualError.GetMessage()
147+
|> Assert.shouldBeEqualWith expectedMessage.Value "Error messages should match"
148+
149+
Assert.Equal(DiagnosticSeverity.Error, actualError.Severity)
150+
let expectedStart = fileContents.IndexOf(expectedMarker)
151+
152+
actualError.Location.SourceSpan.Start
153+
|> Assert.shouldBeEqualWith expectedStart "Error start positions should match"
154+
155+
let expectedEnd = expectedStart + expectedMarker.Length
156+
157+
actualError.Location.SourceSpan.End
158+
|> Assert.shouldBeEqualWith expectedEnd "Error end positions should match"
159+
94160
member private this.VerifyErrorBetweenMarkers(fileContents: string, expectedMessage: string) =
95161
this.VerifyDiagnosticBetweenMarkers(fileContents, expectedMessage, DiagnosticSeverity.Error)
96162

@@ -99,7 +165,7 @@ type DocumentDiagnosticAnalyzerTests() =
99165

100166
[<Fact>]
101167
member public this.Error_Expression_IllegalIntegerLiteral() =
102-
this.VerifyErrorBetweenMarkers(
168+
this.VerifyErrorBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
103169
fileContents =
104170
"""
105171
let _ = 1
@@ -110,7 +176,7 @@ let a = 0.1(*start*).(*end*)0
110176

111177
[<Fact>]
112178
member public this.Error_Expression_IncompleteDefine() =
113-
this.VerifyErrorBetweenMarkers(
179+
this.VerifyErrorBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
114180
fileContents =
115181
"""
116182
let a = (*start*);(*end*)
@@ -120,7 +186,7 @@ let a = (*start*);(*end*)
120186

121187
[<Fact>]
122188
member public this.Error_Expression_KeywordAsValue() =
123-
this.VerifyErrorBetweenMarkers(
189+
this.VerifyErrorBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
124190
fileContents =
125191
"""
126192
let b =
@@ -255,7 +321,7 @@ let f () =
255321

256322
[<Fact>]
257323
member public this.Error_Identifer_IllegalFloatPointLiteral() =
258-
this.VerifyErrorBetweenMarkers(
324+
this.VerifyErrorBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
259325
fileContents =
260326
"""
261327
let x: float = 1.2(*start*).(*end*)3
@@ -368,7 +434,7 @@ async { if true then return 1 } |> ignore
368434

369435
[<Fact>]
370436
member public this.ExtraEndif() =
371-
this.VerifyErrorBetweenMarkers(
437+
this.VerifyErrorBetweenMarkers_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
372438
fileContents =
373439
"""
374440
#if UNDEFINED
@@ -383,7 +449,7 @@ async { if true then return 1 } |> ignore
383449

384450
[<Fact>]
385451
member public this.Squiggles_HashNotFirstSymbol_If() =
386-
this.VerifyErrorAtMarker(
452+
this.VerifyErrorAtMarker_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
387453
fileContents =
388454
"""
389455
(*comment*) #if UNDEFINED
@@ -398,7 +464,7 @@ async { if true then return 1 } |> ignore
398464

399465
[<Fact>]
400466
member public this.Squiggles_HashNotFirstSymbol_Endif() =
401-
this.VerifyErrorAtMarker(
467+
this.VerifyErrorAtMarker_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
402468
fileContents =
403469
"""
404470
#if DEBUG
@@ -413,7 +479,7 @@ async { if true then return 1 } |> ignore
413479

414480
[<Fact>]
415481
member public this.Squiggles_HashIfWithMultilineComment() =
416-
this.VerifyErrorAtMarker(
482+
this.VerifyErrorAtMarker_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
417483
fileContents =
418484
"""
419485
#if DEBUG (*comment*)
@@ -425,7 +491,7 @@ async { if true then return 1 } |> ignore
425491

426492
[<Fact>]
427493
member public this.Squiggles_HashIfWithUnexpected() =
428-
this.VerifyErrorAtMarker(
494+
this.VerifyErrorAtMarker_HACK_PLEASE_REFER_TO_COMMENT_INSIDE(
429495
fileContents =
430496
"""
431497
#if DEBUG TEST

0 commit comments

Comments
 (0)