Skip to content

Commit ade7853

Browse files
committed
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 <[email protected]> Reviewed-by: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent cfaff8d commit ade7853

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

go/packages/packages.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717
"os"
1818
"sync"
1919

20-
"golang.org/x/tools/go/gcexportdata"
2120
"path/filepath"
2221
"strings"
22+
23+
"golang.org/x/tools/go/gcexportdata"
2324
)
2425

2526
// A LoadMode specifies the amount of detail to return when loading packages.
@@ -296,11 +297,6 @@ func (ld *loader) load(patterns ...string) ([]*Package, error) {
296297
// See if the extra process invocation can be avoided.
297298
list, err := listfunc(rawCfg, patterns...)
298299
if _, ok := err.(GoTooOldError); ok {
299-
if ld.Config.Mode >= LoadTypes {
300-
// Upgrade to LoadAllSyntax because we can't depend on the existance
301-
// of export data. We can remove this once iancottrell's cl is in.
302-
ld.Config.Mode = LoadAllSyntax
303-
}
304300
listfunc = golistPackagesFallback
305301
list, err = listfunc(rawCfg, patterns...)
306302
}
@@ -358,8 +354,7 @@ func (ld *loader) loadFrom(list ...*rawPackage) ([]*Package, error) {
358354
GoFiles: pkg.GoFiles,
359355
OtherFiles: pkg.OtherFiles,
360356
},
361-
// TODO: should needsrc also be true if pkg.Export == ""
362-
needsrc: ld.Mode >= LoadAllSyntax,
357+
needsrc: ld.Mode >= LoadAllSyntax || pkg.Export == "",
363358
}
364359
ld.pkgs[lpkg.ID] = lpkg
365360
if !pkg.DepOnly {

go/packages/packages_test.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,12 @@ func TestTypeCheckError(t *testing.T) {
533533
wantTypes bool
534534
wantSyntax bool
535535
wantIllTyped bool
536-
wantErrs []string
537536
}{
538-
{"a", true, true, true, nil},
539-
{"b", true, true, true, nil},
540-
{"c", true, true, true, []string{"could not import d (no export data file)"}},
541-
{"d", false, false, true, nil}, // missing export data
542-
{"e", false, false, false, nil}, // type info not requested (despite type error)
537+
{"a", true, true, true},
538+
{"b", true, true, true},
539+
{"c", true, true, true},
540+
{"d", false, false, true}, // missing export data
541+
{"e", false, false, false}, // type info not requested (despite type error)
543542
} {
544543
if usesOldGolist && test.id == "c" || test.id == "d" || test.id == "e" {
545544
// Behavior is different for old golist because it upgrades to wholeProgram.
@@ -568,9 +567,6 @@ func TestTypeCheckError(t *testing.T) {
568567
if p.IllTyped != test.wantIllTyped {
569568
t.Errorf("IllTyped was %t for %s", p.IllTyped, test.id)
570569
}
571-
if errs := errorMessages(p.Errors); !reflect.DeepEqual(errs, test.wantErrs) {
572-
t.Errorf("in package %s, got errors %s, want %s", p, errs, test.wantErrs)
573-
}
574570
}
575571

576572
// Check value of constant.

0 commit comments

Comments
 (0)