Skip to content

Commit 74cea6e

Browse files
committed
internal/imports: ignore some line directives
This is a minimal fix for the reported panic. The existing code used FileSet.Position() where it should have used FileSet.PositionFor(, false). gopls does this in lots of places; each use of Position() needs to be thought about. x/tools/go/ast/astutil/import.go:273,274 is another example. Fixes: golang/go#51916 Change-Id: I42de6affca2c97c09efb7974a9e44322556b3ffd Reviewed-on: https://go-review.googlesource.com/c/tools/+/398395 Run-TryBot: Peter Weinberger <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> Trust: Peter Weinberger <[email protected]>
1 parent 37acb39 commit 74cea6e

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

internal/imports/fix_test.go

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ func (t *goimportTest) assertProcessEquals(module, file string, contents []byte,
17191719
t.Fatalf("Process() = %v", err)
17201720
}
17211721
if string(buf) != want {
1722-
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
1722+
t.Errorf("Got:\n'%s'\nWant:\n'%s'", buf, want) // 's show empty lines
17231723
}
17241724
}
17251725

@@ -1746,8 +1746,100 @@ const Y = bar.X
17461746
}.processTest(t, "foo.com", "test/t.go", nil, nil, want)
17471747
}
17481748

1749+
func TestPanicAstutils(t *testing.T) {
1750+
t.Skip("panic in ast/astutil/imports.go, should be PostionFor(,false) at lines 273, 274, at least")
1751+
const input = `package main
1752+
//line mah.go:600
1753+
1754+
import (
1755+
"foo.com/a.thing"
1756+
"foo.com/surprise"
1757+
"foo.com/v1"
1758+
"foo.com/other/v2"
1759+
"foo.com/other/v3"
1760+
)
1761+
`
1762+
1763+
const want = `package main
1764+
1765+
//line mah.go:600
1766+
1767+
import (
1768+
"foo.com/a.thing"
1769+
"foo.com/go-thing"
1770+
gow "foo.com/go-wrong"
1771+
v2 "foo.com/other/v2"
1772+
"foo.com/other/v3"
1773+
bar "foo.com/surprise"
1774+
v1 "foo.com/v1"
1775+
)
1776+
1777+
`
1778+
1779+
testConfig{
1780+
module: packagestest.Module{
1781+
Name: "foo.com",
1782+
Files: fm{
1783+
"test/t.go": input,
1784+
},
1785+
},
1786+
}.processTest(t, "foo.com", "test/t.go", nil, nil, want)
1787+
}
1788+
1789+
// without PositionFor in sortImports this test panics
1790+
func TestPanic51916(t *testing.T) {
1791+
const input = `package main
1792+
//line mah.go:600
1793+
1794+
import (
1795+
"foo.com/a.thing"
1796+
"foo.com/surprise"
1797+
"foo.com/v1"
1798+
"foo.com/other/v2"
1799+
"foo.com/other/v3"
1800+
"foo.com/go-thing"
1801+
"foo.com/go-wrong"
1802+
)
1803+
1804+
var _ = []interface{}{bar.X, v1.Y, a.A, v2.V2, other.V3, thing.Thing, gow.Wrong}`
1805+
1806+
const want = `package main
1807+
1808+
//line mah.go:600
1809+
1810+
import (
1811+
"foo.com/a.thing"
1812+
"foo.com/go-thing"
1813+
gow "foo.com/go-wrong"
1814+
v2 "foo.com/other/v2"
1815+
"foo.com/other/v3"
1816+
bar "foo.com/surprise"
1817+
v1 "foo.com/v1"
1818+
)
1819+
1820+
var _ = []interface{}{bar.X, v1.Y, a.A, v2.V2, other.V3, thing.Thing, gow.Wrong}
1821+
`
1822+
1823+
testConfig{
1824+
module: packagestest.Module{
1825+
Name: "foo.com",
1826+
Files: fm{
1827+
"a.thing/a.go": "package a \n const A = 1",
1828+
"surprise/x.go": "package bar \n const X = 1",
1829+
"v1/x.go": "package v1 \n const Y = 1",
1830+
"other/v2/y.go": "package v2 \n const V2 = 1",
1831+
"other/v3/z.go": "package other \n const V3 = 1",
1832+
"go-thing/b.go": "package thing \n const Thing = 1",
1833+
"go-wrong/b.go": "package gow \n const Wrong = 1",
1834+
"test/t.go": input,
1835+
},
1836+
},
1837+
}.processTest(t, "foo.com", "test/t.go", nil, nil, want)
1838+
}
1839+
17491840
// Tests that an existing import with badly mismatched path/name has its name
17501841
// correctly added. See #28645 and #29041.
1842+
// and check that //line directives are ignored (#51916)
17511843
func TestAddNameToMismatchedImport(t *testing.T) {
17521844
const input = `package main
17531845

internal/imports/sortimports.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func sortImports(localPrefix string, fset *token.FileSet, f *ast.File) {
5151
// Deduping can leave a blank line before the rparen; clean that up.
5252
if len(d.Specs) > 0 {
5353
lastSpec := d.Specs[len(d.Specs)-1]
54-
lastLine := fset.Position(lastSpec.Pos()).Line
55-
if rParenLine := fset.Position(d.Rparen).Line; rParenLine > lastLine+1 {
54+
lastLine := fset.PositionFor(lastSpec.Pos(), false).Line
55+
if rParenLine := fset.PositionFor(d.Rparen, false).Line; rParenLine > lastLine+1 {
5656
fset.File(d.Rparen).MergeLine(rParenLine - 1)
5757
}
5858
}

0 commit comments

Comments
 (0)