diff --git a/go/ast/astutil/imports.go b/go/ast/astutil/imports.go index 7d793ffa..2e81f4d3 100644 --- a/go/ast/astutil/imports.go +++ b/go/ast/astutil/imports.go @@ -160,6 +160,9 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added first = gen continue // Don't touch the first one. } + // We now know there is more than one package in this import + // declaration. Ensure that it ends up parenthesized. + first.Lparen = first.Pos() // Move the imports of the other import declaration to the first one. for _, spec := range gen.Specs { spec.(*ast.ImportSpec).Path.ValuePos = first.Pos() diff --git a/go/ast/astutil/imports_test.go b/go/ast/astutil/imports_test.go index c9f80df7..67dd5565 100644 --- a/go/ast/astutil/imports_test.go +++ b/go/ast/astutil/imports_test.go @@ -509,6 +509,42 @@ import "C" // Comment import "C" import "bufio" +`, + }, + { + name: `issue 17212 several single-import lines with shared prefix ending in a slash`, + pkg: "net/http", + in: `package main + +import "bufio" +import "net/url" +`, + out: `package main + +import ( + "bufio" + "net/http" + "net/url" +) +`, + }, + { + name: `issue 17212 block imports lines with shared prefix ending in a slash`, + pkg: "net/http", + in: `package main + +import ( + "bufio" + "net/url" +) +`, + out: `package main + +import ( + "bufio" + "net/http" + "net/url" +) `, }, {