SublimeLinter-golint icon indicating copy to clipboard operation
SublimeLinter-golint copied to clipboard

Error column is wrong when non-ASCII characters come earlier on the line (golint counts bytes)

Open dpc00 opened this issue 2 months ago • 1 comments

The error marker is in the wrong place when a non-ASCII character comes earlier on the line: golint reports the column as a UTF-8 byte offset, and the linter passes it on as a character column.

Reproduce (Sublime Text 4215, SublimeLinter, SublimeLinter-golint, golint from golang.org/x/lint):

package main

var x_y = 1

func main() {
	_ = "ééé"; var my_var = 2
	_ = my_var
}

golint a.go prints:

a.go:3:5: don't use underscores in Go names; var x_y should be xY
a.go:6:20: don't use underscores in Go names; var my_var should be myVar

On line 6, my_var starts at the 17th character (1-based), but golint says column 20: each é is 2 bytes in UTF-8, so the three of them add 3.

Actual: line 3 (all ASCII before the name) is marked on x_y, which is right. On line 6 the mark is three characters to the right, on the var at the end of my_var, not on my_var. The more bytes beyond one per character there are before the problem on the line (accents take 2, CJK 3, emoji 4), the further the mark drifts.

Expected: the mark is on my_var.

Go's token.Position.Column is documented as a byte count, so this is how golint always reports it. The same thing affects the other linters whose tools count bytes, for example SublimeLinter/SublimeLinter-cppcheck#25.

Possible fix: before the column is used, convert it using the line text, i.e. decode the first col - 1 bytes of the line as UTF-8 and use the number of characters. In linter.py that would be an override of split_match that fixes col from self.view (the line is available there) before returning the match. I have not changed any code here.

dpc00 avatar Oct 01 '26 16:10 dpc00

The same thing happens in SublimeLinter-golangcilint (that repository has issues disabled, so I am noting it here, since it is the same fix for another Go tool).

golangci-lint 2.14.0 also reports byte columns. For

package main

import "os"

func main() {
	s := "é😀é"; os.Remove(s)
	x := 1
	x = 2
	println(x)
}

it prints main.go:6:28: Error return value of os.Remove is not checked (errcheck). In characters the call is at column 23; 28 is the byte offset (é is 2 bytes, 😀 is 4). In Sublime the marker for that line ends up at the end of the line instead of on the call. The wrapper has no column_unit set, so the core treats the number as a character column.

I tried adding column_unit = 'utf8' to the GolangCILint class on a local copy (core 4.30 supports it), and the marker is then on the ( of the call, as it should be. So the same one-line change fixes that wrapper.

dpc00 avatar Oct 01 '26 18:10 dpc00