You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added a test case to golang.org/x/tools/go/ast/astutil's imports_test.go for adding an import of "fmt" to the following file:
package main
import "bufio"
import "bytes"
import "errors"
What did you expect to see?
Test case passes with the following result:
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
)
What did you see instead?
$ go test golang.org/x/tools/go/ast/astutil
--- FAIL: TestAddImport (0.00s)
panic: runtime error: slice bounds out of range [recovered]
panic: runtime error: slice bounds out of range
goroutine 34 [running]:
panic(0x146740, 0xc42000a100)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
testing.tRunner.func1(0xc4200a6180)
/usr/local/go/src/testing/testing.go:579 +0x25d
panic(0x146740, 0xc42000a100)
/usr/local/go/src/runtime/panic.go:458 +0x243
golang.org/x/tools/go/ast/astutil.AddNamedImport(0xc42007c540, 0xc42012c600, 0x0, 0x0, 0x16d656, 0x3, 0x0)
/Users/rhys/work/src/golang.org/x/tools/go/ast/astutil/imports.go:167 +0x99c
golang.org/x/tools/go/ast/astutil.TestAddImport(0xc4200a6180)
/Users/rhys/work/src/golang.org/x/tools/go/ast/astutil/imports_test.go:540 +0x1f5
testing.tRunner(0xc4200a6180, 0x182ee0)
/usr/local/go/src/testing/testing.go:610 +0x81
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:646 +0x2ec
FAIL golang.org/x/tools/go/ast/astutil 0.012s
I can work around this by adding a boring AST node after the import section (such as var _ int). In that case the output includes the imports I'd expect, though they're oddly not all in a single block import. This may give a hint as to the cause of #17212... Here's a passing test case:
{
name: `issue nnnnn several single-import lines`,
pkg: "fmt",
in: `package main
import "bufio"
import "bytes"
import "errors"
var _ int
`,
out: `package main
import (
"bufio"
"bytes"
"fmt"
)
import "errors"
var _ int
`,
},
Removing the var _ int line leads to the panic.
The text was updated successfully, but these errors were encountered:
I've got a fix for this: the range loop at the end of AddNamedImport is buggy, since it modifies the slice as it ranges over it. I'm going to delay mailing a CL to see if I can fix #17212 at the same time.
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?golang/tools@3f4088e
What operating system and processor architecture are you using (
go env
)?What did you do?
I added a test case to golang.org/x/tools/go/ast/astutil's imports_test.go for adding an import of
"fmt"
to the following file:What did you expect to see?
Test case passes with the following result:
What did you see instead?
I can work around this by adding a boring AST node after the import section (such as
var _ int
). In that case the output includes the imports I'd expect, though they're oddly not all in a single block import. This may give a hint as to the cause of #17212... Here's a passing test case:Removing the
var _ int
line leads to the panic.The text was updated successfully, but these errors were encountered: