go.tools/imports: fix fileset mismatch bug

Fixes golang/go#6884

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/43890043
This commit is contained in:
Brad Fitzpatrick 2013-12-18 09:09:37 -08:00
parent 64c6d0410b
commit b6e674b8e7
3 changed files with 24 additions and 4 deletions

View File

@ -45,7 +45,7 @@ func importGroup(importPath string) int {
return 0 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. // refs are a set of possible package references currently unsatisified by imports.
// first key: either base package (e.g. "fmt") or renamed package // first key: either base package (e.g. "fmt") or renamed package
// second key: referenced package symbol (e.g. "Println") // second key: referenced package symbol (e.g. "Println")
@ -196,8 +196,6 @@ func loadPkgIndex() {
wg.Wait() wg.Wait()
} }
var fset = token.NewFileSet()
func loadPkg(wg *sync.WaitGroup, root, pkgrelpath string) { func loadPkg(wg *sync.WaitGroup, root, pkgrelpath string) {
importpath := filepath.ToSlash(pkgrelpath) importpath := filepath.ToSlash(pkgrelpath)
shortName := importPathToName(importpath) 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) fmt.Fprintf(os.Stderr, "could not import %q: %v", dir, err)
return nil return nil
} }
fset := token.NewFileSet()
for _, file := range buildPkg.GoFiles { for _, file := range buildPkg.GoFiles {
f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0) f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0)
if err != nil { if err != nil {

View File

@ -447,6 +447,27 @@ func f() {
_ = fmt.Printf _ = fmt.Printf
_ = snappy.Foo _ = 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")
}
`, `,
}, },
} }

View File

@ -45,7 +45,7 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) {
return nil, err return nil, err
} }
_, err = fixImports(file) _, err = fixImports(fileSet, file)
if err != nil { if err != nil {
return nil, err return nil, err
} }