go/packages: suppress go list errors when ad-hoc package doesn't exist

Updates golang/go#29280

Change-Id: Ie5a5dc1fef8f3d989b3a5fffb6c2ca66e97c143a
Reviewed-on: https://go-review.googlesource.com/c/154517
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Michael Matloob 2018-12-17 13:26:00 -05:00
parent 57eff0d8ac
commit ae5b881676
2 changed files with 31 additions and 1 deletions

View File

@ -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)
}
}
@ -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 {

View File

@ -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 {