From ade7853a2a5896e5dacdb611fdec9a89cb905087 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Tue, 31 Jul 2018 12:36:23 -0400 Subject: [PATCH] go/packages: allow Load to use source if no export data is possible We ask the underlying tool to produce export data if needed, but if it cant we should fall back to source rather than just failing. Change-Id: I376d41b4c8ad4d3c1aef3ca4ab4e3d0466b83b56 Reviewed-on: https://go-review.googlesource.com/126875 Run-TryBot: Ian Cottrell Reviewed-by: Michael Matloob TryBot-Result: Gobot Gobot --- go/packages/packages.go | 11 +++-------- go/packages/packages_test.go | 14 +++++--------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/go/packages/packages.go b/go/packages/packages.go index 9c786490da..a1ceb8b9c2 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -17,9 +17,10 @@ import ( "os" "sync" - "golang.org/x/tools/go/gcexportdata" "path/filepath" "strings" + + "golang.org/x/tools/go/gcexportdata" ) // A LoadMode specifies the amount of detail to return when loading packages. @@ -296,11 +297,6 @@ func (ld *loader) load(patterns ...string) ([]*Package, error) { // See if the extra process invocation can be avoided. list, err := listfunc(rawCfg, patterns...) if _, ok := err.(GoTooOldError); ok { - if ld.Config.Mode >= LoadTypes { - // Upgrade to LoadAllSyntax because we can't depend on the existance - // of export data. We can remove this once iancottrell's cl is in. - ld.Config.Mode = LoadAllSyntax - } listfunc = golistPackagesFallback list, err = listfunc(rawCfg, patterns...) } @@ -358,8 +354,7 @@ func (ld *loader) loadFrom(list ...*rawPackage) ([]*Package, error) { GoFiles: pkg.GoFiles, OtherFiles: pkg.OtherFiles, }, - // TODO: should needsrc also be true if pkg.Export == "" - needsrc: ld.Mode >= LoadAllSyntax, + needsrc: ld.Mode >= LoadAllSyntax || pkg.Export == "", } ld.pkgs[lpkg.ID] = lpkg if !pkg.DepOnly { diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 66953e6853..0d4780e740 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -533,13 +533,12 @@ func TestTypeCheckError(t *testing.T) { wantTypes bool wantSyntax bool wantIllTyped bool - wantErrs []string }{ - {"a", true, true, true, nil}, - {"b", true, true, true, nil}, - {"c", true, true, true, []string{"could not import d (no export data file)"}}, - {"d", false, false, true, nil}, // missing export data - {"e", false, false, false, nil}, // type info not requested (despite type error) + {"a", true, true, true}, + {"b", true, true, true}, + {"c", true, true, true}, + {"d", false, false, true}, // missing export data + {"e", false, false, false}, // type info not requested (despite type error) } { if usesOldGolist && test.id == "c" || test.id == "d" || test.id == "e" { // Behavior is different for old golist because it upgrades to wholeProgram. @@ -568,9 +567,6 @@ func TestTypeCheckError(t *testing.T) { if p.IllTyped != test.wantIllTyped { t.Errorf("IllTyped was %t for %s", p.IllTyped, test.id) } - if errs := errorMessages(p.Errors); !reflect.DeepEqual(errs, test.wantErrs) { - t.Errorf("in package %s, got errors %s, want %s", p, errs, test.wantErrs) - } } // Check value of constant.