{"action":"closed","issue":{"url":"https://api.github.com/repos/golang/go/issues/17213","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/17213/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/17213/comments","events_url":"https://api.github.com/repos/golang/go/issues/17213/events","html_url":"https://github.com/golang/go/issues/17213","id":178968931,"number":17213,"title":"x/tools/go/ast/astutil: panic when given multiple single-import lines","user":{"login":"rhysh","id":230685,"avatar_url":"https://avatars.githubusercontent.com/u/230685?v=3","gravatar_id":"","url":"https://api.github.com/users/rhysh","html_url":"https://github.com/rhysh","followers_url":"https://api.github.com/users/rhysh/followers","following_url":"https://api.github.com/users/rhysh/following{/other_user}","gists_url":"https://api.github.com/users/rhysh/gists{/gist_id}","starred_url":"https://api.github.com/users/rhysh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rhysh/subscriptions","organizations_url":"https://api.github.com/users/rhysh/orgs","repos_url":"https://api.github.com/users/rhysh/repos","events_url":"https://api.github.com/users/rhysh/events{/privacy}","received_events_url":"https://api.github.com/users/rhysh/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-09-23T20:41:15Z","updated_at":"2016-09-24T00:48:47Z","closed_at":"2016-09-24T00:48:47Z","body":"Please answer these questions before submitting your issue. Thanks!\r\n\r\n### What version of Go are you using (`go version`)?\r\n\r\n```\r\n$ go version\r\ngo version go1.7.1 darwin/amd64\r\n```\r\n\r\ngolang/tools@3f4088edb48e8a4e3c66a5f8e7b2a78615fcb83f\r\n\r\n### What operating system and processor architecture are you using (`go env`)?\r\n\r\n```\r\n$ go env\r\nGOARCH=\"amd64\"\r\nGOBIN=\"\"\r\nGOEXE=\"\"\r\nGOHOSTARCH=\"amd64\"\r\nGOHOSTOS=\"darwin\"\r\nGOOS=\"darwin\"\r\nGOPATH=\"/Users/rhys/work\"\r\nGORACE=\"\"\r\nGOROOT=\"/usr/local/go\"\r\nGOTOOLDIR=\"/usr/local/go/pkg/tool/darwin_amd64\"\r\nCC=\"clang\"\r\nGOGCCFLAGS=\"-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/49/zmds5zsn75z1283vtzxyfr5hj7yjq4/T/go-build054967312=/tmp/go-build -gno-record-gcc-switches -fno-common\"\r\nCXX=\"clang++\"\r\nCGO_ENABLED=\"1\"\r\n```\r\n\r\n### What did you do?\r\n\r\nI 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:\r\n\r\n```\r\npackage main\r\n\r\nimport \"bufio\"\r\nimport \"bytes\"\r\nimport \"errors\"\r\n```\r\n\r\n\r\n### What did you expect to see?\r\n\r\nTest case passes with the following result:\r\n\r\n```\r\npackage main\r\n\r\nimport (\r\n\t\"bufio\"\r\n\t\"bytes\"\r\n\t\"errors\"\r\n\t\"fmt\"\r\n)\r\n```\r\n\r\n### What did you see instead?\r\n\r\n```\r\n$ go test golang.org/x/tools/go/ast/astutil\r\n--- FAIL: TestAddImport (0.00s)\r\npanic: runtime error: slice bounds out of range [recovered]\r\n\tpanic: runtime error: slice bounds out of range\r\n\r\ngoroutine 34 [running]:\r\npanic(0x146740, 0xc42000a100)\r\n\t/usr/local/go/src/runtime/panic.go:500 +0x1a1\r\ntesting.tRunner.func1(0xc4200a6180)\r\n\t/usr/local/go/src/testing/testing.go:579 +0x25d\r\npanic(0x146740, 0xc42000a100)\r\n\t/usr/local/go/src/runtime/panic.go:458 +0x243\r\ngolang.org/x/tools/go/ast/astutil.AddNamedImport(0xc42007c540, 0xc42012c600, 0x0, 0x0, 0x16d656, 0x3, 0x0)\r\n\t/Users/rhys/work/src/golang.org/x/tools/go/ast/astutil/imports.go:167 +0x99c\r\ngolang.org/x/tools/go/ast/astutil.TestAddImport(0xc4200a6180)\r\n\t/Users/rhys/work/src/golang.org/x/tools/go/ast/astutil/imports_test.go:540 +0x1f5\r\ntesting.tRunner(0xc4200a6180, 0x182ee0)\r\n\t/usr/local/go/src/testing/testing.go:610 +0x81\r\ncreated by testing.(*T).Run\r\n\t/usr/local/go/src/testing/testing.go:646 +0x2ec\r\nFAIL\tgolang.org/x/tools/go/ast/astutil\t0.012s\r\n```\r\n\r\n---\r\n\r\nI 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:\r\n\r\n```\r\n\t{\r\n\t\tname: `issue nnnnn several single-import lines`,\r\n\t\tpkg:  \"fmt\",\r\n\t\tin: `package main\r\n\r\nimport \"bufio\"\r\nimport \"bytes\"\r\nimport \"errors\"\r\n\r\nvar _ int\r\n`,\r\n\t\tout: `package main\r\n\r\nimport (\r\n\t\"bufio\"\r\n\t\"bytes\"\r\n\t\"fmt\"\r\n)\r\n\r\nimport \"errors\"\r\n\r\nvar _ int\r\n`,\r\n\t},\r\n```\r\n\r\nRemoving the `var _ int` line leads to the panic."}}
{"actor":{"display_login":"gopherbot"}}