Commit 03fff363e5 for golang
commit 03fff363e5a12b4e800b1c5cf78977e88d825bcf
Author: Ahmed Mohamed <ahmed.ahmed32005@gmail.com>
Date: Mon Nov 2 19:45:55 2026 +0200
cmd/go: use a rootless loader for go doc's inferred version
When go doc's argument matches a program on $PATH, parseArgs infers its
module path and version from the build info. That load needs a loader with
ForceUseModules and no module root, which parseArgs only builds when the
argument spells a version out with @, so the inferred path reused the
working-directory loader:
$ go doc staticcheck
panic: modload.ForceUseModules must be true
modload.Init returns early once a loader exists, so give the versioned load
its own, as the @ path and buildPkgsite already do.
Fixes #82510
Change-Id: I587ac64d338643d35f06150324830c8daf31a23c
Reviewed-on: https://go-review.googlesource.com/c/go/+/856904
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
diff --git a/src/cmd/go/internal/doc/doc.go b/src/cmd/go/internal/doc/doc.go
index 7c9d99e779..8b25c6691c 100644
--- a/src/cmd/go/internal/doc/doc.go
+++ b/src/cmd/go/internal/doc/doc.go
@@ -507,7 +507,15 @@ func parseArgs(ctx context.Context, flagSet *flag.FlagSet, args []string) (pkg *
if version == "" {
version = v
}
- pkg, err := loadVersioned(ctx, loader, pkgPath, version)
+ // Loading a package at a version needs a rootless loader, the same
+ // as an argument that spells the version out. The loader above was
+ // initialized for the module in the working directory, if any, and
+ // modload.Init does nothing the second time, so this needs its own.
+ vloader := modload.NewLoader()
+ vloader.ForceUseModules = true
+ vloader.RootMode = modload.NoRoot
+ modload.Init(vloader)
+ pkg, err := loadVersioned(ctx, vloader, pkgPath, version)
if err == nil {
return pkg, pkgPath, "", false
}
diff --git a/src/cmd/go/testdata/script/doc_installed_program.txt b/src/cmd/go/testdata/script/doc_installed_program.txt
new file mode 100644
index 0000000000..d36269664d
--- /dev/null
+++ b/src/cmd/go/testdata/script/doc_installed_program.txt
@@ -0,0 +1,34 @@
+# go doc of a name that matches an installed program infers the version from
+# that program's build info, which reaches a versioned load. That load needs a
+# rootless loader, and used to get the one built for the working directory.
+# See issue 82510.
+
+env GOBIN=$WORK/bin
+go install example.com/zqxdoctool
+exists $GOBIN/zqxdoctool$GOEXE
+env PATH=$GOBIN${:}$PATH
+
+# The module is local, so there is no version to fetch and the load fails.
+# It has to fail with an error rather than a panic.
+cd $WORK/elsewhere
+! go doc zqxdoctool
+! stderr 'panic'
+! stderr 'ForceUseModules'
+
+# A name that matches no installed program is unaffected.
+! go doc notaninstalledprogram
+! stderr 'panic'
+
+-- go.mod --
+module example.com/zqxdoctool
+
+go 1.28
+
+-- main.go --
+// Package main is a program used to check go doc's version inference.
+package main
+
+func main() {}
+
+-- $WORK/elsewhere/keep.txt --
+This directory is deliberately outside any module.