Skip to content

Commit cb42bbe

Browse files
committed
go/packages: add a work around for go list behavior for missing ad-hoc package
If a file in an ad-hoc package doesn't exist, go list should exit 0 and return an dummy package with an error set on it. Since it doesn't do that yet, add a work-around. Updates golang/go#29280 Change-Id: I6019f28ce4770582f274919d1aa35d85a634687e Reviewed-on: https://go-review.googlesource.com/c/tools/+/171018 Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 0fdf0c7 commit cb42bbe

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

go/packages/golist.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,15 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
761761
// the error in the Err section of stdout in case -e option is provided.
762762
// This fix is provided for backwards compatibility.
763763
if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "named files must be .go files") {
764-
output := fmt.Sprintf(`{"ImportPath": "","Incomplete": true,"Error": {"Pos": "","Err": %s}}`,
765-
strconv.Quote(strings.Trim(stderr.String(), "\n")))
764+
output := fmt.Sprintf(`{"ImportPath": "command-line-arguments","Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
765+
strings.Trim(stderr.String(), "\n"))
766+
return bytes.NewBufferString(output), nil
767+
}
768+
769+
// Workaround for #29280: go list -e has incorrect behavior when an ad-hoc package doesn't exist.
770+
if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "no such file or directory") {
771+
output := fmt.Sprintf(`{"ImportPath": "command-line-arguments","Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
772+
strings.Trim(stderr.String(), "\n"))
766773
return bytes.NewBufferString(output), nil
767774
}
768775

go/packages/packages_test.go

+41-24
Original file line numberDiff line numberDiff line change
@@ -313,29 +313,6 @@ func TestLoadAbsolutePath(t *testing.T) {
313313
}
314314
}
315315

316-
func TestReturnErrorWhenUsingNonGoFiles(t *testing.T) {
317-
exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
318-
Name: "golang.org/gopatha",
319-
Files: map[string]interface{}{
320-
"a/a.go": `package a`,
321-
}}, {
322-
Name: "golang.org/gopathb",
323-
Files: map[string]interface{}{
324-
"b/b.c": `package b`,
325-
}}})
326-
defer exported.Cleanup()
327-
config := packages.Config{}
328-
_, err := packages.Load(&config, "a/a.go", "b/b.c")
329-
if err == nil {
330-
t.Fatalf("should have failed with an error")
331-
}
332-
got := err.Error()
333-
want := "named files must be .go files"
334-
if !strings.Contains(got, want) {
335-
t.Fatalf("want error message: %s, got: %s", want, got)
336-
}
337-
}
338-
339316
func TestVendorImports(t *testing.T) {
340317
exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
341318
Name: "golang.org/fake",
@@ -1723,7 +1700,47 @@ func testErrorMissingFile(t *testing.T, exporter packagestest.Exporter) {
17231700
if err != nil {
17241701
t.Fatal(err)
17251702
}
1726-
t.Log(pkgs)
1703+
if len(pkgs) == 0 && runtime.GOOS == "windows" {
1704+
t.Skip("Issue #31344: the ad-hoc command-line-arguments package isn't created on windows")
1705+
}
1706+
if len(pkgs) != 1 || pkgs[0].PkgPath != "command-line-arguments" {
1707+
t.Fatalf("packages.Load: want [command-line-arguments], got %v", pkgs)
1708+
}
1709+
if len(pkgs[0].Errors) == 0 {
1710+
t.Errorf("result of Load: want package with errors, got none: %+v", pkgs[0])
1711+
}
1712+
}
1713+
1714+
func TestReturnErrorWhenUsingNonGoFiles(t *testing.T) {
1715+
packagestest.TestAll(t, testReturnErrorWhenUsingNonGoFiles)
1716+
}
1717+
func testReturnErrorWhenUsingNonGoFiles(t *testing.T, exporter packagestest.Exporter) {
1718+
exported := packagestest.Export(t, exporter, []packagestest.Module{{
1719+
Name: "golang.org/gopatha",
1720+
Files: map[string]interface{}{
1721+
"a/a.go": `package a`,
1722+
}}, {
1723+
Name: "golang.org/gopathb",
1724+
Files: map[string]interface{}{
1725+
"b/b.c": `package b`,
1726+
}}})
1727+
defer exported.Cleanup()
1728+
config := packages.Config{}
1729+
pkgs, err := packages.Load(&config, "a/a.go", "b/b.c")
1730+
if err != nil {
1731+
t.Fatal(err)
1732+
}
1733+
if len(pkgs) != 1 || pkgs[0].PkgPath != "command-line-arguments" {
1734+
t.Fatalf("packages.Load: want [command-line-arguments], got %v", pkgs)
1735+
}
1736+
if len(pkgs[0].Errors) != 1 {
1737+
t.Fatalf("result of Load: want package with one error, got: %+v", pkgs[0])
1738+
}
1739+
got := pkgs[0].Errors[0].Error()
1740+
want := "named files must be .go files"
1741+
if !strings.Contains(got, want) {
1742+
t.Fatalf("want error message: %s, got: %s", want, got)
1743+
}
17271744
}
17281745

17291746
func errorMessages(errors []packages.Error) []string {

0 commit comments

Comments
 (0)