diff --git a/imports/fix_test.go b/imports/fix_test.go index e2873008c6a..654d65596dd 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -888,6 +888,33 @@ import "fmt" func main() { _ = fmt.Println } +`, + }, + + { + name: "issue #23709", + in: `package main + +import ( + "math" // fun +) + +func main() { + x := math.MaxInt64 + fmt.Println(strings.Join(",", []string{"hi"}), x) +}`, + out: `package main + +import ( + "fmt" + "math" // fun + "strings" +) + +func main() { + x := math.MaxInt64 + fmt.Println(strings.Join(",", []string{"hi"}), x) +} `, }, } @@ -1643,8 +1670,8 @@ func TestImportPathToNameGoPathParse(t *testing.T) { func TestIgnoreConfiguration(t *testing.T) { testConfig{ gopathFiles: map[string]string{ - ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming - "example.net/pkg/pkg.go": "package pkg\nconst X = 1", + ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming + "example.net/pkg/pkg.go": "package pkg\nconst X = 1", "otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1", }, }.test(t, func(t *goimportTest) { diff --git a/imports/sortimports.go b/imports/sortimports.go index 653afc51776..93711565a9b 100644 --- a/imports/sortimports.go +++ b/imports/sortimports.go @@ -167,11 +167,18 @@ func sortSpecs(fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec { } s.Path.ValuePos = pos[i].Start s.EndPos = pos[i].End + nextSpecPos := pos[i].End + for _, g := range importComment[s] { for _, c := range g.List { c.Slash = pos[i].End + nextSpecPos = c.End() } } + if i < len(specs)-1 { + pos[i+1].Start = nextSpecPos + pos[i+1].End = nextSpecPos + } } sort.Sort(byCommentPos(comments))