Skip to content

Commit

Permalink
go/packages: suppress go list errors when ad-hoc package doesn't exist
Browse files Browse the repository at this point in the history
Updates golang/go#29280

Change-Id: Ie5a5dc1fef8f3d989b3a5fffb6c2ca66e97c143a
Reviewed-on: https://go-review.googlesource.com/c/154517
Reviewed-by: Ian Cottrell <[email protected]>
  • Loading branch information
matloob committed Dec 17, 2018
1 parent 57eff0d commit ae5b881
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 13 additions & 1 deletion go/packages/golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,10 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
// If that build fails, errors appear on stderr
// (despite the -e flag) and the Export field is blank.
// Do not fail in that case.
if !usesExportData(cfg) {
// The same is true if an ad-hoc package given to go list doesn't exist.
// TODO(matloob): Remove these once we can depend on go list to exit with a zero status with -e even when
// packages don't exist or a build fails.
if !usesExportData(cfg) && !containsGoFile(args) {
return nil, fmt.Errorf("go %v: %s: %s", args, exitErr, stderr)
}
}
Expand All @@ -764,6 +767,15 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
return stdout, nil
}

func containsGoFile(s []string) bool {
for _, f := range s {
if strings.HasSuffix(f, ".go") {
return true
}
}
return false
}

func cmdDebugStr(cfg *Config, args ...string) string {
env := make(map[string]string)
for _, kv := range cfg.Env {
Expand Down
18 changes: 18 additions & 0 deletions go/packages/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,24 @@ func testBasicXTest(t *testing.T, exporter packagestest.Exporter) {
}
}

func TestErrorMissingFile(t *testing.T) { packagestest.TestAll(t, testErrorMissingFile) }
func testErrorMissingFile(t *testing.T, exporter packagestest.Exporter) {
exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "golang.org/fake",
Files: map[string]interface{}{
"a/a_test.go": `package a;`,
}}})
defer exported.Cleanup()

exported.Config.Mode = packages.LoadSyntax
exported.Config.Tests = false
pkgs, err := packages.Load(exported.Config, "missing.go")
if err != nil {
t.Fatal(err)
}
t.Log(pkgs)
}

func errorMessages(errors []packages.Error) []string {
var msgs []string
for _, err := range errors {
Expand Down

0 comments on commit ae5b881

Please sign in to comment.