Repository navigation
fix: handle empty line codes in ranged discussions - #593
Conversation
GitLab leaves the old or new number of a line code empty when the line has no counterpart on the other side (e.g. "<sha>__44"). parse_line_code returned nil for both numbers in that case and get_line_numbers_for_range crashed with "attempt to perform arithmetic on local 'new_end_line' (a nil value)" while setting diagnostics in the reviewer. Parse empty numbers as nil and fall back to a single-line range.
|
Hi @Enceradeira, are these empty line numbers created by gitlab.nvim or by Gitlab in the browser? I'm asking because maybe this is not the right place to fix this. If the missing line numbers are caused by gitlab.nvim, then we should fix the line code creation. |
|
By the way there is #587 - is it solving the same problem? The author of that other PR never replied to my question, so I didn't bother to merge it :) |
This reverts commit 9384374.
GitLab can return an empty line_code for both ends of a discussion's line_range. parse_line_code then returns nil for both numbers and get_line_numbers_for_range crashed with "attempt to perform arithmetic on local 'new_end_line' (a nil value)" while setting diagnostics in the reviewer. Fall back to a single-line range when a line code can't be parsed, and don't throw on a nil line code.
|
Hi @jakubbortlik, the empty line numbers don't come from gitlab.nvim. The Go server always builds line codes as I checked the failing discussion with range = {
["end"] = { line_code = "", new_line = 26, old_line = 0, type = "new" },
start = { line_code = "", new_line = 26, old_line = 0, type = "new" },
}I'm sure the comment wasn't written in Neovim. It was written by a colleague who doesn't use it, most likely in the GitLab web UI. I could ask whether they used an Emacs plugin, if that's important. I've updated the PR accordingly: Regarding #587: yes, it's the same crash, reached via I'm happy to investigate further if you tell me what you'd like me to look into. |
|
Hi @Enceradeira, thanks for investigating this and updating the PR. If you want to take it a step further you could try to find out if we could use the I've got a vague idea that Gitlab does some updating of the values when a file in a MR is modified and the line numbers change, but I haven't really tried to pin the changes down. So maybe there's an opportunity to not only fix the bug you've encountered, but to also improve the navigation for comments on lines whose line numbers have changed. Maybe to fix this properly, we'd have to also parse the following comments for patterns like |
Move the parse_line_code and get_line_numbers_for_range specs into per-module spec files, nested under the module and function they test.
When GitLab returns empty line codes for a ranged discussion, get_line_numbers_for_range collapsed the range to a single line. Fall back to the range's old_line/new_line instead. These are 0 on the side a line doesn't exist on, so a range spanning deleted and added lines without line codes still falls back to a single line. get_line_numbers_for_range now takes the whole line range instead of the two line codes.
|
Hi @jakubbortlik, I've pushed a commit that takes up your first suggestion: Last week I also looked into tracking comments whose line numbers changed between diff versions, but I couldn't find my way around it as quickly as I'd hoped. I'm happy to try again in a separate PR. In the meantime, as you suggested, I'd be grateful if you could merge this one. That also keeps the fix and the broader improvement in separate PRs. Let me know if you'd like anything done differently. |
|
Hi @Enceradeira, thanks for continuing with the PR. Please run Let's merge this now to fix the current bug. Then I will be grateful if you could investigate a little bit more and see if One think to consider: |
|
By the way, I've now noticed that the missing line codes come from comments created in Gitlab.com itself 😱 |
|
@Enceradeira, thanks for the contribution! |
Opening a file in the reviewer that has a ranged discussion fails in the
DiffviewDiffBufWinEnterhook:GitLab can return an empty
line_codefor both ends of a discussion'sline_range, e.g.:parse_line_codethen returnsnil, nil, andget_line_numbers_for_rangedoes arithmetic on thenilvalues (both the old-SHA and the new-SHA branch). The same crash is reported in #587 viajump_to_reviewer.Changes
get_line_numbers_for_rangefalls back to a single-line range when a line code can't be parsed.parse_line_codeno longer throws on anilline code.tests/spec/line_code_spec.lua, which reproduces the error without the fix.🤖 Generated with Claude Code