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 <bcmills@google.com>
diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go
index 7e44348..e61432f 100644
--- a/cmd/godoc/godoc_test.go
+++ b/cmd/godoc/godoc_test.go
@@ -473,7 +473,7 @@
 	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 dc8a081..77a38ac 100644
--- a/cmd/godoc/main.go
+++ b/cmd/godoc/main.go
@@ -222,7 +222,7 @@
 		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 @@
 // 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)