cmd/go/internal/doc: pass URL fragments separately with -http

Plumb URL fragments separately when invoking pkgsite so the `#` doesn't
get %-encoded into the path.

Fixes: #75088
Change-Id: I33814fc6a192dff3e4f3d0b9d81205056dddd438
Reviewed-on: https://go-review.googlesource.com/c/go/+/697435
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Ian Alexander <jitsu@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
David Finkel 2025-08-19 13:37:18 -04:00 committed by Michael Matloob
parent 509d5f647f
commit 4afd482812
3 changed files with 14 additions and 13 deletions

View file

@ -212,16 +212,16 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
mod, err := runCmd(append(os.Environ(), "GOWORK=off"), "go", "list", "-m") mod, err := runCmd(append(os.Environ(), "GOWORK=off"), "go", "list", "-m")
if err == nil && mod != "" && mod != "command-line-arguments" { if err == nil && mod != "" && mod != "command-line-arguments" {
// If there's a module, go to the module's doc page. // If there's a module, go to the module's doc page.
return doPkgsite(mod) return doPkgsite(mod, "")
} }
gowork, err := runCmd(nil, "go", "env", "GOWORK") gowork, err := runCmd(nil, "go", "env", "GOWORK")
if err == nil && gowork != "" { if err == nil && gowork != "" {
// Outside a module, but in a workspace, go to the home page // Outside a module, but in a workspace, go to the home page
// with links to each of the modules' pages. // with links to each of the modules' pages.
return doPkgsite("") return doPkgsite("", "")
} }
// Outside a module or workspace, go to the documentation for the standard library. // Outside a module or workspace, go to the documentation for the standard library.
return doPkgsite("std") return doPkgsite("std", "")
} }
// If args are provided, we need to figure out which page to open on the pkgsite // If args are provided, we need to figure out which page to open on the pkgsite
@ -282,11 +282,11 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
} }
if found { if found {
if serveHTTP { if serveHTTP {
path, err := objectPath(userPath, pkg, symbol, method) path, fragment, err := objectPath(userPath, pkg, symbol, method)
if err != nil { if err != nil {
return err return err
} }
return doPkgsite(path) return doPkgsite(path, fragment)
} }
return nil return nil
} }
@ -305,7 +305,8 @@ func runCmd(env []string, cmdline ...string) (string, error) {
return strings.TrimSpace(stdout.String()), nil return strings.TrimSpace(stdout.String()), nil
} }
func objectPath(userPath string, pkg *Package, symbol, method string) (string, error) { // returns a path followed by a fragment (or an error)
func objectPath(userPath string, pkg *Package, symbol, method string) (string, string, error) {
var err error var err error
path := pkg.build.ImportPath path := pkg.build.ImportPath
if path == "." { if path == "." {
@ -314,7 +315,7 @@ func objectPath(userPath string, pkg *Package, symbol, method string) (string, e
// go list to get the import path. // go list to get the import path.
path, err = runCmd(nil, "go", "list", userPath) path, err = runCmd(nil, "go", "list", userPath)
if err != nil { if err != nil {
return "", err return "", "", err
} }
} }
@ -322,10 +323,7 @@ func objectPath(userPath string, pkg *Package, symbol, method string) (string, e
if symbol != "" && method != "" { if symbol != "" && method != "" {
object = symbol + "." + method object = symbol + "." + method
} }
if object != "" { return path, object, nil
path = path + "#" + object
}
return path, nil
} }
// failMessage creates a nicely formatted error message when there is no result to show. // failMessage creates a nicely formatted error message when there is no result to show.

View file

@ -34,7 +34,7 @@ func pickUnusedPort() (int, error) {
return port, nil return port, nil
} }
func doPkgsite(urlPath string) error { func doPkgsite(urlPath, fragment string) error {
port, err := pickUnusedPort() port, err := pickUnusedPort()
if err != nil { if err != nil {
return fmt.Errorf("failed to find port for documentation server: %v", err) return fmt.Errorf("failed to find port for documentation server: %v", err)
@ -44,6 +44,9 @@ func doPkgsite(urlPath string) error {
if err != nil { if err != nil {
return fmt.Errorf("internal error: failed to construct url: %v", err) return fmt.Errorf("internal error: failed to construct url: %v", err)
} }
if fragment != "" {
path += "#" + fragment
}
// Turn off the default signal handler for SIGINT (and SIGQUIT on Unix) // Turn off the default signal handler for SIGINT (and SIGQUIT on Unix)
// and instead wait for the child process to handle the signal and // and instead wait for the child process to handle the signal and

View file

@ -8,4 +8,4 @@
package doc package doc
func doPkgsite(string) error { return nil } func doPkgsite(string, string) error { return nil }