diff --git a/go/ast/astutil/imports.go b/go/ast/astutil/imports.go index 36c536b5..4a77c782 100644 --- a/go/ast/astutil/imports.go +++ b/go/ast/astutil/imports.go @@ -262,7 +262,7 @@ func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (del // There was a blank line immediately preceding the deleted import, // so there's no need to close the hole. // Do nothing. - } else { + } else if line != fset.File(gen.Rparen).LineCount() { // There was no blank line. Close the hole. fset.File(gen.Rparen).MergeLine(line) } diff --git a/go/ast/astutil/imports_test.go b/go/ast/astutil/imports_test.go index cb12a290..21682674 100644 --- a/go/ast/astutil/imports_test.go +++ b/go/ast/astutil/imports_test.go @@ -1297,6 +1297,21 @@ import ( /* comment 2 */ "io" ) +`, + }, + + // Issue 20229: MergeLine panic on weird input + { + name: "import.37", + pkg: "io", + in: `package main +import("_" +"io")`, + out: `package main + +import ( + "_" +) `, }, }