Skip to content

Commit 230cc3b

Browse files
committed
Avoid returning negative positions
According to the LSP spec, the line and character must both be unsigned integers. The spec also specifically calls out that `-1` is not supported: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position With some code structures, it is possible to produce an error such as the following from `golangci-lint run --out-format=json`: ```json { "FromLinter": "typecheck", "Text": ": # github.com/my/package [github.com/my/package.test]\n./main.go:31:2: undefined: asdf", "Severity": "", "SourceLines": [ "package main" ], "Replacement": null, "Pos": { "Filename": "main.go", "Offset": 0, "Line": 1, "Column": 0 }, "ExpectNoLint": false, "ExpectedNoLintLinter": "" } ``` This ultimately does result in some issues with tooling compatibility, for example: folke/trouble.nvim#224 (comment) By preventing the number from dropping below zero, this class of error should no longer be present.
1 parent ed10182 commit 230cc3b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

handler.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,14 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
9191

9292
d := Diagnostic{
9393
Range: Range{
94-
Start: Position{Line: issue.Pos.Line - 1, Character: issue.Pos.Column - 1},
95-
End: Position{Line: issue.Pos.Line - 1, Character: issue.Pos.Column - 1},
94+
Start: Position{
95+
Line: max(issue.Pos.Line-1, 0),
96+
Character: max(issue.Pos.Column-1, 0),
97+
},
98+
End: Position{
99+
Line: max(issue.Pos.Line-1, 0),
100+
Character: max(issue.Pos.Column-1, 0),
101+
},
96102
},
97103
Severity: issue.DiagSeverity(),
98104
Source: &issue.FromLinter,
@@ -104,6 +110,13 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
104110
return diagnostics, nil
105111
}
106112

113+
func max(a, b int) int {
114+
if a > b {
115+
return a
116+
}
117+
return b
118+
}
119+
107120
func (h *langHandler) diagnosticMessage(issue *Issue) string {
108121
if h.noLinterName {
109122
return issue.Text

0 commit comments

Comments
 (0)