go/packages/golist: pass in -compiled to go list

This is mostly straightforward, except that go list -e -compiled
will now return errors if a package can't build. This is a bug.
We need to skip the errors test until that's fixed.

For now, don't try to run go list with no arguments because it will
fail. So when all arguments are contains, we will check for empty
patterns and skip running go list.

Change-Id: I7c92b1bf9448e5dbea22f9ade9fb9429945e3719
Reviewed-on: https://go-review.googlesource.com/127339
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Michael Matloob 2018-08-01 13:40:35 -04:00
parent 78b3e71765
commit 4a42e0a488
4 changed files with 73 additions and 50 deletions

View File

@ -57,17 +57,28 @@ func LoadRaw(ctx context.Context, cfg *raw.Config, patterns ...string) ([]string
patterns = restPatterns patterns = restPatterns
} }
listfunc := golistPackages // TODO(matloob): Remove the definition of listfunc and just use golistPackages once go1.12 is released.
// TODO(matloob): Patterns may now be empty, if it was solely comprised of contains: patterns. var listfunc func(ctx context.Context, cfg *raw.Config, words ...string) ([]string, []*raw.Package, error)
// See if the extra process invocation can be avoided. listfunc = func(ctx context.Context, cfg *raw.Config, words ...string) ([]string, []*raw.Package, error) {
roots, pkgs, err := listfunc(ctx, cfg, patterns...) roots, pkgs, err := golistPackages(ctx, cfg, patterns...)
if _, ok := err.(goTooOldError); ok { if _, ok := err.(goTooOldError); ok {
listfunc = golistPackagesFallback listfunc = golistPackagesFallback
roots, pkgs, err = listfunc(ctx, cfg, patterns...) roots, pkgs, err = listfunc(ctx, cfg, patterns...)
} }
listfunc = golistPackages
return roots, pkgs, err
}
roots, pkgs, err := []string(nil), []*raw.Package(nil), error(nil)
// TODO(matloob): Patterns may now be empty, if it was solely comprised of contains: patterns.
// See if the extra process invocation can be avoided.
if len(patterns) > 0 {
roots, pkgs, err = listfunc(ctx, cfg, patterns...)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
}
// Run go list for contains: patterns. // Run go list for contains: patterns.
seenPkgs := make(map[string]bool) // for deduplication. different containing queries could produce same packages seenPkgs := make(map[string]bool) // for deduplication. different containing queries could produce same packages
@ -112,6 +123,7 @@ type jsonPackage struct {
Name string Name string
Export string Export string
GoFiles []string GoFiles []string
CompiledGoFiles []string
CFiles []string CFiles []string
CgoFiles []string CgoFiles []string
SFiles []string SFiles []string
@ -233,11 +245,16 @@ func golistPackages(ctx context.Context, cfg *raw.Config, words ...string) ([]st
ID: id, ID: id,
Name: p.Name, Name: p.Name,
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles), GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles),
CompiledGoFiles: absJoin(p.Dir, p.CompiledGoFiles),
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles), OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
PkgPath: pkgpath, PkgPath: pkgpath,
Imports: imports, Imports: imports,
Export: export, Export: export,
} }
// TODO(matloob): Temporary hack since CompiledGoFiles isn't always set.
if len(pkg.CompiledGoFiles) == 0 {
pkg.CompiledGoFiles = pkg.GoFiles
}
if !p.DepOnly { if !p.DepOnly {
roots = append(roots, pkg.ID) roots = append(roots, pkg.ID)
} }
@ -262,7 +279,7 @@ func absJoin(dir string, fileses ...[]string) (res []string) {
func golistargs(cfg *raw.Config, words []string) []string { func golistargs(cfg *raw.Config, words []string) []string {
fullargs := []string{ fullargs := []string{
"list", "-e", "-json", "list", "-e", "-json", "-compiled",
fmt.Sprintf("-test=%t", cfg.Tests), fmt.Sprintf("-test=%t", cfg.Tests),
fmt.Sprintf("-export=%t", cfg.Export), fmt.Sprintf("-export=%t", cfg.Export),
fmt.Sprintf("-deps=%t", cfg.Deps), fmt.Sprintf("-deps=%t", cfg.Deps),

View File

@ -68,6 +68,7 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
ID: id, ID: id,
Name: p.Name, Name: p.Name,
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles), GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles),
CompiledGoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles), // TODO(matloob): Use cgo-processed Go files instead of p.GoFiles
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles), OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
PkgPath: pkgpath, PkgPath: pkgpath,
Imports: importMap(p.Imports), Imports: importMap(p.Imports),
@ -82,6 +83,7 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
ID: testID, ID: testID,
Name: p.Name, Name: p.Name,
GoFiles: absJoin(p.Dir, p.GoFiles, p.TestGoFiles, p.CgoFiles), GoFiles: absJoin(p.Dir, p.GoFiles, p.TestGoFiles, p.CgoFiles),
CompiledGoFiles: absJoin(p.Dir, p.GoFiles, p.TestGoFiles, p.CgoFiles), // TODO(matloob): Use cgo-processed Go files instead of p.GoFiles
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles), OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
PkgPath: pkgpath, PkgPath: pkgpath,
Imports: importMap(append(p.Imports, p.TestImports...)), Imports: importMap(append(p.Imports, p.TestImports...)),
@ -102,6 +104,7 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
ID: xtestID, ID: xtestID,
Name: p.Name + "_test", Name: p.Name + "_test",
GoFiles: absJoin(p.Dir, p.XTestGoFiles), GoFiles: absJoin(p.Dir, p.XTestGoFiles),
CompiledGoFiles: absJoin(p.Dir, p.XTestGoFiles),
PkgPath: pkgpath, PkgPath: pkgpath,
Imports: importMap(p.XTestImports), Imports: importMap(p.XTestImports),
}) })

View File

@ -464,7 +464,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
lpkg.Errors = append(lpkg.Errors, err) lpkg.Errors = append(lpkg.Errors, err)
} }
files, errs := ld.parseFiles(lpkg.GoFiles) files, errs := ld.parseFiles(lpkg.raw.CompiledGoFiles)
for _, err := range errs { for _, err := range errs {
appendError(err) appendError(err)
} }

View File

@ -671,6 +671,9 @@ func TestWholeProgramOverlay(t *testing.T) {
} }
func TestWholeProgramImportErrors(t *testing.T) { func TestWholeProgramImportErrors(t *testing.T) {
// TODO(matloob): Remove this once go list -e -compiled is fixed. See golang.org/issue/26755
t.Skip("go list -compiled -e fails with non-zero exit status for empty packages")
if usesOldGolist { if usesOldGolist {
t.Skip("not yet supported in pre-Go 1.10.4 golist fallback implementation") t.Skip("not yet supported in pre-Go 1.10.4 golist fallback implementation")
} }