Skip to content

Commit 069435c

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/cache: use 1 not 0 for missing line/col info
This change causes the parser of go list error messages to fill in 1, not 0, for missing line/column information, as those are the correct values for this particular representation (1-based UTF-8). Also, report a bug if we see non-positive arguments to LineCol8Position. Plus, a regression test. Fixes golang/go#67360 Change-Id: I87635b99c8b13056c4816b58106ec4a29a9ceb9e Reviewed-on: https://go-review.googlesource.com/c/tools/+/585555 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent 528484d commit 069435c

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

gopls/internal/cache/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,13 @@ func splitFileLineCol(s string) (file string, line, col8 int) {
433433
// strip col ":%d"
434434
s, n1 := stripColonDigits(s)
435435
if n1 < 0 {
436-
return s, 0, 0 // "filename"
436+
return s, 1, 1 // "filename"
437437
}
438438

439439
// strip line ":%d"
440440
s, n2 := stripColonDigits(s)
441441
if n2 < 0 {
442-
return s, n1, 0 // "filename:line"
442+
return s, n1, 1 // "filename:line"
443443
}
444444

445445
return s, n2, n1 // "filename:line:col"

gopls/internal/cache/errors_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func TestParseErrorMessage(t *testing.T) {
1919
name string
2020
in string
2121
expectedFileName string
22-
expectedLine int
23-
expectedColumn int
22+
expectedLine int // (missing => 1)
23+
expectedColumn int // (missing => 1)
2424
}{
2525
{
2626
name: "from go list output",
@@ -34,7 +34,7 @@ func TestParseErrorMessage(t *testing.T) {
3434
in: "C:\\foo\\bar.go:13: message",
3535
expectedFileName: "bar.go",
3636
expectedLine: 13,
37-
expectedColumn: 0,
37+
expectedColumn: 1,
3838
},
3939
}
4040

gopls/internal/protocol/mapper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import (
7171
"sync"
7272
"unicode/utf8"
7373

74+
"golang.org/x/tools/gopls/internal/util/bug"
7475
"golang.org/x/tools/gopls/internal/util/safetoken"
7576
)
7677

@@ -131,6 +132,14 @@ func (m *Mapper) initLines() {
131132
// LineCol8Position converts a valid line and UTF-8 column number,
132133
// both 1-based, to a protocol (UTF-16) position.
133134
func (m *Mapper) LineCol8Position(line, col8 int) (Position, error) {
135+
// Report a bug for inputs that are invalid for any file content.
136+
if line < 1 {
137+
return Position{}, bug.Errorf("invalid 1-based line number: %d", line)
138+
}
139+
if col8 < 1 {
140+
return Position{}, bug.Errorf("invalid 1-based column number: %d", col8)
141+
}
142+
134143
m.initLines()
135144
line0 := line - 1 // 0-based
136145
if !(0 <= line0 && line0 < len(m.lineStart)) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Regression test for #67360.
2+
3+
This file causes go list to report a "use of internal package
4+
cmd/internal/browser" error. (It is important that this be a real
5+
internal std package.) The line directive caused the position of the
6+
error to lack a column. A bug in the error parser filled in 0, not 1,
7+
for the missing information, and this is an invalid value in the
8+
1-based UTF-8 domain, leading to a panic.
9+
10+
-- flags --
11+
-min_go=go1.21
12+
13+
-- foo.go --
14+
//line foo.go:1
15+
package main //@ diag(re"package", re"internal package.*not allowed")
16+
import _ "cmd/internal/browser" //@ diag(re`"`, re"could not import")

0 commit comments

Comments
 (0)