# Invalid diagnostic location after text edit

**URL:** <https://forums.swift.org/t/invalid-diagnostic-location-after-text-edit/54761>\
**Category:** SourceKit-LSP\
**Created:** [January 20, 2022, 10:59pm UTC](https://forums.swift.org/t/invalid-diagnostic-location-after-text-edit/54761 "2022-01-20T22:59:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![krzyzanowskim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krzyzanowskim/32/1282_2.png) [@krzyzanowskim](https://forums.swift.org/u/krzyzanowskim)\
**Post date:** [January 20, 2022, 10:59pm UTC](https://forums.swift.org/t/invalid-diagnostic-location-after-text-edit/54761/1 "2022-01-20T22:59:40Z")

</div>

I'm not sure where I should report this issue, so here we go. It looks like the content of `textDocument/publishDiagnostics` that follows the `textDocument/didChange` is invalid.

What happens is as follows:

1. I start with a diagnostic at the last line
2. Backward delete a line

now what happens is `sourcekit-lsp` reports 2 notifications, one after another. First, behave like if buffer didn't update yet, and diagnostic is still at line 12, then right after that, there's another notification where this diagnostic is properly updated and located at line 11. It acts as it reports the intermediate state somehow. I confirmed this behavior with the current `main` (`ef270137fe8ea03a032914f44a9ffd0e3fb11496`) and binary from Xcode 13.2.1.

I was able to capture that in VSCode:

![Screen Recording 2022-01-20 at 23.54.06.2022-01-20 23_55_24](https://global.discourse-cdn.com/swift/original/3X/1/1/115e527f712c8b030435c3a2b54c3990526ae874.gif)

 ![Screen Shot 2022-01-20 at 23.16.42](https://global.discourse-cdn.com/swift/original/3X/8/8/8891a56f9f533b2cbf2cb768443a034d713c4c46.jpeg)

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [January 20, 2022, 11:41pm UTC](https://forums.swift.org/t/invalid-diagnostic-location-after-text-edit/54761/2 "2022-01-20T23:41:37Z")

</div>

Looks buggy, thanks for reporting. CC @ahoppen

It's expected that there are two updates, and it's possible that in the first update the semantic diagnostics (e.g. cannot find 'app') are stale, because the first update is purely syntactic, but (a) we shouldn't get duplicate diagnostics like that, and (b) the semantic diagnostics should be range-shifted to the correct location even if we don't know yet if they're applicable (i.e. if your edit fixed the semantic error we wouldn't know that in the first update, but we should at least shift it to the correct location).

---

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [January 21, 2022, 9:53am UTC](https://forums.swift.org/t/invalid-diagnostic-location-after-text-edit/54761/3 "2022-01-21T09:53:33Z")

</div>

Thanks for reporting this @krzyzanowskim. As Ben mentioned, sending two `PublishDiagnostics` notifications is intended but it looks like the syntactic diagnostics report wrong line numbers. Maybe VSCode is getting confused because of this. I’m looking into it.

I also noticed from your screen recording that we only highlight the first character of the unknown identifier. I’m looking into that as well.

---

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [January 21, 2022, 2:37pm UTC](https://forums.swift.org/t/invalid-diagnostic-location-after-text-edit/54761/4 "2022-01-21T14:37:10Z")

</div>

I just submitted [Adjust diagnostic ranges on edits by ahoppen · Pull Request #447 · apple/sourcekit-lsp · GitHub](https://github.com/apple/sourcekit-lsp/pull/447), which should fix the issues.
