From 537d06c362073e8c95164d0d4709059603cfdb02 Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Thu, 20 Dec 2018 16:09:19 -0500 Subject: [PATCH] imports: use LoadFiles everywhere Since we don't really need type info, and want everything to be fast, use LoadFiles and do parsing manually. It would be nice if there were a mode for that. Change-Id: I33f8a85ffb87a70048c4775058bd0813cf677061 Reviewed-on: https://go-review.googlesource.com/c/155478 Run-TryBot: Heschi Kreinick Reviewed-by: Ian Cottrell --- imports/fix.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/imports/fix.go b/imports/fix.go index 8267f087..69124092 100644 --- a/imports/fix.go +++ b/imports/fix.go @@ -580,7 +580,7 @@ func scanGoPackages(refs map[string]map[string]bool) ([]*pkg, error) { loadQueries = append(loadQueries, "name="+pkgName) } sort.Strings(loadQueries) - cfg := newPackagesConfig(packages.LoadTypes) + cfg := newPackagesConfig(packages.LoadFiles) goPackages, err := packages.Load(cfg, loadQueries...) if err != nil { return nil, err @@ -846,19 +846,29 @@ func VendorlessPath(ipath string) string { // loadExports returns the set of exported symbols in the package at dir. // It returns nil on error or if the package name in dir does not match expectPackage. func loadExports(ctx context.Context, expectPackage string, pkg *pkg) (map[string]bool, error) { + if Debug { + log.Printf("loading exports in dir %s (seeking package %s)", pkg.dir, expectPackage) + } if pkg.goPackage != nil { exports := map[string]bool{} - for _, name := range pkg.goPackage.Types.Scope().Names() { - if ast.IsExported(name) { - exports[name] = true + fset := token.NewFileSet() + for _, fname := range pkg.goPackage.CompiledGoFiles { + f, err := parser.ParseFile(fset, fname, nil, 0) + if err != nil { + if Debug { + log.Printf("Parsing %s: %v", fname, err) + } + return nil, err + } + for name := range f.Scope.Objects { + if ast.IsExported(name) { + exports[name] = true + } } } return exports, nil } - if Debug { - log.Printf("loading exports in dir %s (seeking package %s)", pkg.dir, expectPackage) - } exports := make(map[string]bool) buildCtx := build.Default