Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiagnosticCollection.set([Uri, [Diagnostic[]][]) is not reliable #15585

Closed
DustinCampbell opened this issue Nov 16, 2016 · 5 comments
Closed
Assignees
Labels
api bug Issue identified by VS Code Team member as probable bug verified Verification succeeded
Milestone

Comments

@DustinCampbell
Copy link
Member

The documentation for DiagnosticCollection.set([Uri, Diagnostic[]][]) states the following:

Diagnostics of multiple tuples of the same uri will be merged, e.g [[file1, [d1]], [file1, [d2]]] is equivalent to [[file1, [d1, d2]]]. If a diagnostics item is undefined as in [file1, undefined] all previous but not subsequent diagnostics are removed.

However, this doesn't work in practice because the entries end up being sorted by uri, but the sort is not stable. Here's the code that does the sort. This can result in entries being reordered so that this...

[file1, undefined]
[file1, [diagnostic1, diagnostic2]]

...becomes this...

[file1, [diagnostic1, diagnostic2]]
[file1, undefined]

That results in diagnostics being cleared when they aren't supposed to be.

@DustinCampbell
Copy link
Member Author

In the C# extension, I tried sorting entries using the same code as VS Code and could clearly see the impact of not having a stable sort.

Here are the entries before sorting:

image

Here they are after sorting:

image

Note that several elements are reversed. These elements have the same uri.

@jrieken jrieken added the bug Issue identified by VS Code Team member as probable bug label Nov 17, 2016
@jrieken jrieken added this to the November 2016 milestone Nov 17, 2016
@jrieken
Copy link
Member

jrieken commented Nov 17, 2016

Awesome find and analysis. @DustinCampbell should have commit rights

@jrieken
Copy link
Member

jrieken commented Nov 17, 2016

So, that's what you get when you rely on un-specified behaviour... It seems that v8 is especially mean because it uses stable-sorting for small arrays and unstable-sorting for larger arrays.

From here

Small arrays are sorted using a simpler algorithm which happens to be stable.
You need a larger array to trigger the quicksort algorithm which is non-stable.

@DustinCampbell
Copy link
Member Author

Nice fix!

@chrmarti
Copy link
Contributor

Verified by code review and running the test with the fix reverted.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api bug Issue identified by VS Code Team member as probable bug verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

4 participants