Repository navigation
fix: 32 diagnostics or more with nested, equal or separate ranges no longer fail with "Comparison method violates its general contract!" - #2328
Conversation
…longer fail with "Comparison method violates its general contract!" The diagnostics of a file are grouped by covered range: each diagnostic joins the first group whose range contains its own, so that the quick fixes of a group are asked for once. They were sorted first, so that a diagnostic comes before those whose range its range contains, with a comparator that returned -1 when the first range contained the second, and 1 otherwise. That isn't an order: it never returned 0, so a range came before itself, two equal ranges each came before the other, and two separate ranges each came after the other. TimSort finds that out once it merges runs, from 32 diagnostics on, and failed with "IllegalArgumentException: Comparison method violates its general contract!". The diagnostics that the language server published weren't shown then: the file kept those it had before, or none, and the same list failed each time it was published. The diagnostics are now sorted by the start of their range, and those with the same start the longest first. That is a total order, in which a range comes before the ranges that it contains, as the grouping needs. The new test publishes 32 diagnostics whose ranges are nested, equal, overlapping, separate and empty. With the old comparator, it fails with the exception of the report. A second test checks that no diagnostic comes after one whose range contains its own. Fixes redhat-developer#2323
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
…ther diagnostics of its list LSP requires a range for each diagnostic, but LSP4J's JSON parser accepts a diagnostic without one, or without the start or the end of its range, and a null diagnostic in the list. Such a diagnostic made the grouping of the diagnostics of an opened file fail with a NullPointerException, so none of the list was shown, as with redhat-developer#2323. A null diagnostic made the cache of a closed file fail too, in List.copyOf, and an error without a range made a closed file a problem file in the Project view. Both now ignore such a diagnostic (LSPDiagnosticUtils.withRange), which can't be placed in the file, as LSP4IJ already ignores a diagnostic whose range doesn't fit the document when it shows it. The others of the list are kept. A list whose diagnostics all have a range is used as it is. Refs redhat-developer#2323
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2328 +/- ##
==========================================
- Coverage 58.63% 58.62% -0.01%
==========================================
Files 849 849
Lines 36704 36713 +9
Branches 7606 7608 +2
==========================================
+ Hits 21520 21524 +4
- Misses 12183 12185 +2
- Partials 3001 3004 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@coderabbitai review |
Action performed
|
Fixes #2323
With 32 diagnostics or more whose ranges are nested, equal or separate,
LSPDiagnosticsForServer.toMapfailed withIllegalArgumentException: Comparison method violates its general contract!, as in the report. A diagnostic without a range made a list fail in the same way, with aNullPointerException. Twocommits, for a rebase merge, for 0.30.2. Both bugs are already in 0.30.1.
Behaviour change
Comparison method violates its general contract!. Its diagnostics weren't shown: the file kept those it had before, or none, and the same list failed each time it came.nulldiagnostic, made the whole list of an opened file fail with aNullPointerException, so none of it was shown. Anulldiagnostic made the list of a closed file fail too, and an error without a range made a closed file a problem file in the Project view.1.
fix:the order of the diagnosticsCause.
toMapsorts the diagnostics, then groups them by covered range: each diagnostic joins the first group whose range contains its own, and a group asks for its quick fixes once. The comparator returned -1 when the first range contained the second, and 1 otherwise. It never returned 0, so it wasn't an order:TimSort checks the order only once it merges runs, from 32 elements on, hence the threshold in the report.
Fix. The diagnostics are sorted by the start of their range, and those with the same start the longest first (
CONTAINING_RANGE_FIRST). It is a total order, and a range that contains another comes first, as the grouping needs: such a range starts no later, and with the same start ends no earlier.The comparator proposed in the issue sorts by severity first, and of the same start the shortest range first. A contained diagnostic would then come before the one that contains it, and start a group of its own, so the two would ask for their quick fixes separately.
Tests.
testManyDiagnosticsWithNestedOverlappingAndEqualRangesAreAllKept:updatewith 32 diagnostics whose ranges are nested, equal, overlapping, separate and emptyIllegalArgumentException: Comparison method violates its general contract!testADiagnosticComesBeforeTheDiagnosticsWhoseRangeItsRangeContains: no diagnostic comes after one whose range contains its own, also for two diagnostics in either orderThe 32 ranges come from a throwaway search with
java.util.Random: seed 10 is one of 3 among the first 35 seeds that broke the old comparator. For the run with the old comparator, the old lambda replacedCONTAINING_RANGE_FIRST.2.
fix:a diagnostic without a rangeLSP requires a range for each diagnostic, but LSP4J's JSON parser accepts one without it, one whose range lacks its
start or end, and a
nulldiagnostic in the list. Every report reachesLSPDiagnosticsForServer.updatefor an openedfile, or
ClosedDocument.updateDiagnosticsfor a closed one, and both now drop such a diagnostic first, withLSPDiagnosticUtils.withRange. A list whose diagnostics all have a range is used as it is.LSPDiagnosticsForServerTest.testADiagnosticWithoutARangeIsIgnoredAndTheOthersAreKept: two diagnostics with ranges, an error without a range, anulland an error without the start of its rangeNullPointerException: Cannot invoke "org.eclipse.lsp4j.Range.getStart()" because the return value of "org.eclipse.lsp4j.Diagnostic.getRange()" is nullClosedDocumentDiagnosticsTest.aDiagnosticWithoutARangeIsIgnoredAndTheOthersAreKeptNullPointerException, inList.copyOfClosedDocumentDiagnosticsTest.aReportWithOnlyDiagnosticsWithoutARangeRemovesTheDiagnosticsOfItsProviderTest runs
Headless, with the counts from the JUnit XMLs:
features.diagnostics,ClosedDocumentDiagnosticsTest,ClosedDocumentProblemTestandLSPLazyCodeActionsTestfeatures.diagnostics, andLSPLazyCodeActionsTest, with the first commitLSPDiagnosticsForServerTest,ClosedDocumentDiagnosticsTest,ClosedDocumentProblemTestandLSPLazyCodeActionsTestThe first CI run, of the first commit alone, failed
LanguageServerProcessNodesTest.testADisposedLanguageServerExpiresItsCannotStartNotificationon Linux, a test of the LSP console that this PR doesn't touch. It races with the start that it makes fail; a fix of
the test follows in a PR of its own.