From b6e674b8e7fc09f9a2c73ebe49de48ed89b5834f Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 18 Dec 2013 09:09:37 -0800 Subject: [PATCH] go.tools/imports: fix fileset mismatch bug Fixes golang/go#6884 R=golang-dev, gri CC=golang-dev https://golang.org/cl/43890043 --- imports/fix.go | 5 ++--- imports/fix_test.go | 21 +++++++++++++++++++++ imports/imports.go | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/imports/fix.go b/imports/fix.go index 12d382a002..908b05ddb5 100644 --- a/imports/fix.go +++ b/imports/fix.go @@ -45,7 +45,7 @@ func importGroup(importPath string) int { return 0 } -func fixImports(f *ast.File) (added []string, err error) { +func fixImports(fset *token.FileSet, f *ast.File) (added []string, err error) { // refs are a set of possible package references currently unsatisified by imports. // first key: either base package (e.g. "fmt") or renamed package // second key: referenced package symbol (e.g. "Println") @@ -196,8 +196,6 @@ func loadPkgIndex() { wg.Wait() } -var fset = token.NewFileSet() - func loadPkg(wg *sync.WaitGroup, root, pkgrelpath string) { importpath := filepath.ToSlash(pkgrelpath) shortName := importPathToName(importpath) @@ -250,6 +248,7 @@ func loadExportsGoPath(dir string) map[string]bool { fmt.Fprintf(os.Stderr, "could not import %q: %v", dir, err) return nil } + fset := token.NewFileSet() for _, file := range buildPkg.GoFiles { f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0) if err != nil { diff --git a/imports/fix_test.go b/imports/fix_test.go index 123637ffe5..27a20b256b 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -447,6 +447,27 @@ func f() { _ = fmt.Printf _ = snappy.Foo } +`, + }, + + // golang.org/issue/6884 + { + name: "issue 6884", + in: `package main + +// A comment +func main() { + fmt.Println("Hello, world") +} +`, + out: `package main + +import "fmt" + +// A comment +func main() { + fmt.Println("Hello, world") +} `, }, } diff --git a/imports/imports.go b/imports/imports.go index 6da3a512d9..a40d075a61 100644 --- a/imports/imports.go +++ b/imports/imports.go @@ -45,7 +45,7 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) { return nil, err } - _, err = fixImports(file) + _, err = fixImports(fileSet, file) if err != nil { return nil, err }