From 91381dc0aef943b19ded2d98203e41b4e6861be2 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Wed, 20 Nov 2019 23:33:35 -0500 Subject: [PATCH] cmd/godoc: don't execute go list -m all when GOMOD is /dev/null When the GOMOD value is the operating system's null device, there isn't a main module. Return an empty build list right away, since running 'go list -m all' in Go 1.14 will cause a "cannot match "all": working directory is not part of a module" error. Fixes golang/go#35690 Updates golang/go#35728 Change-Id: I024ca3b7d774835140ce4a1625133aff6554a533 Reviewed-on: https://go-review.googlesource.com/c/tools/+/208258 Reviewed-by: Bryan C. Mills --- cmd/godoc/godoc_test.go | 2 +- cmd/godoc/main.go | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index 7e44348919..e61432f212 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -473,7 +473,7 @@ func TestNoMainModule(t *testing.T) { cmd.Stderr = &stderr err = cmd.Run() if err != nil { - t.Fatal(err) + t.Fatalf("godoc command failed: %v\nstderr=%q", err, stderr.String()) } if strings.Contains(stderr.String(), "go mod download") { t.Errorf("stderr contains 'go mod download', is that intentional?\nstderr=%q", stderr.String()) diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go index dc8a08159f..77a38ac6f7 100644 --- a/cmd/godoc/main.go +++ b/cmd/godoc/main.go @@ -222,7 +222,7 @@ func main() { fillModuleCache(os.Stderr, goModFile) // Determine modules in the build list. - mods, err := buildList() + mods, err := buildList(goModFile) if err != nil { fmt.Fprintf(os.Stderr, "failed to determine the build list of the main module: %v", err) os.Exit(1) @@ -456,7 +456,12 @@ func fillModuleCache(w io.Writer, goMod string) { // in module mode. // // See https://golang.org/cmd/go/#hdr-The_main_module_and_the_build_list. -func buildList() ([]mod, error) { +func buildList(goMod string) ([]mod, error) { + if goMod == os.DevNull { + // Empty build list. + return nil, nil + } + out, err := exec.Command("go", "list", "-m", "-json", "all").Output() if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) { return nil, fmt.Errorf("go command exited unsuccessfully: %v\n%s", ee.ProcessState.String(), ee.Stderr)