cmd/pkgsite: ignore indirect modules

When `go list -m` lists an indirect module, there is no GoMod path.
This was confusing the pkgsite command, because it assumed there was one.
Since `go list` doesn't provide enough information to serve indirect modules,
ignore them.

Fixes golang/go#53323

Change-Id: I0ff9658ee286b2f11dabf76d1bf5f65a9c16ed42
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/412058
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go
index d4ccf27..659fcdc 100644
--- a/cmd/pkgsite/main.go
+++ b/cmd/pkgsite/main.go
@@ -38,6 +38,7 @@
 	"bytes"
 	"context"
 	"encoding/json"
+	"errors"
 	"flag"
 	"fmt"
 	"net/http"
@@ -234,7 +235,8 @@
 // listedMod has a subset of the fields written by `go list -m`.
 type listedMod struct {
 	internal.Modver
-	GoMod string // absolute path to go.mod file; in download cache or replaced
+	GoMod    string // absolute path to go.mod file; in download cache or replaced
+	Indirect bool
 }
 
 var listModules = _listModules
@@ -275,6 +277,13 @@
 			return nil, nil, err
 		}
 		for _, lm := range lms {
+			// Ignore indirect modules.
+			if lm.Indirect {
+				continue
+			}
+			if lm.GoMod == "" {
+				return nil, nil, errors.New("empty GoMod: please file a pkgsite bug at https://go.dev/issues/new")
+			}
 			if strings.HasPrefix(lm.GoMod, cacheDir) {
 				cacheMods = append(cacheMods, lm.Modver)
 			} else { // probably the result of a replace directive
diff --git a/cmd/pkgsite/main_test.go b/cmd/pkgsite/main_test.go
index a0dfac3..4dff62b 100644
--- a/cmd/pkgsite/main_test.go
+++ b/cmd/pkgsite/main_test.go
@@ -207,10 +207,17 @@
 			{
 				internal.Modver{Path: "m1", Version: "v1.2.3"},
 				"/dir/cache/download/m1/@v/v1.2.3.mod",
+				false,
 			},
 			{
 				internal.Modver{Path: "m2", Version: "v1.0.0"},
 				"/repos/m2/go.mod",
+				false,
+			},
+			{
+				internal.Modver{Path: "indir", Version: "v2.3.4"},
+				"",
+				true,
 			},
 		}, nil
 	}