From 4afd4828129d547719e8e71352c36e5117ddb50e Mon Sep 17 00:00:00 2001 From: David Finkel Date: Tue, 19 Aug 2025 13:37:18 -0400 Subject: [PATCH] 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 Reviewed-by: Ian Alexander Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob --- src/cmd/go/internal/doc/doc.go | 20 +++++++++----------- src/cmd/go/internal/doc/pkgsite.go | 5 ++++- src/cmd/go/internal/doc/pkgsite_bootstrap.go | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cmd/go/internal/doc/doc.go b/src/cmd/go/internal/doc/doc.go index 37501065fe..fceeaf101e 100644 --- a/src/cmd/go/internal/doc/doc.go +++ b/src/cmd/go/internal/doc/doc.go @@ -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") if err == nil && mod != "" && mod != "command-line-arguments" { // 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") if err == nil && gowork != "" { // Outside a module, but in a workspace, go to the home page // 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. - return doPkgsite("std") + return doPkgsite("std", "") } // 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 serveHTTP { - path, err := objectPath(userPath, pkg, symbol, method) + path, fragment, err := objectPath(userPath, pkg, symbol, method) if err != nil { return err } - return doPkgsite(path) + return doPkgsite(path, fragment) } return nil } @@ -305,7 +305,8 @@ func runCmd(env []string, cmdline ...string) (string, error) { 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 path := pkg.build.ImportPath if path == "." { @@ -314,7 +315,7 @@ func objectPath(userPath string, pkg *Package, symbol, method string) (string, e // go list to get the import path. path, err = runCmd(nil, "go", "list", userPath) 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 != "" { object = symbol + "." + method } - if object != "" { - path = path + "#" + object - } - return path, nil + return path, object, nil } // failMessage creates a nicely formatted error message when there is no result to show. diff --git a/src/cmd/go/internal/doc/pkgsite.go b/src/cmd/go/internal/doc/pkgsite.go index 6769536ca5..06289ac4fc 100644 --- a/src/cmd/go/internal/doc/pkgsite.go +++ b/src/cmd/go/internal/doc/pkgsite.go @@ -34,7 +34,7 @@ func pickUnusedPort() (int, error) { return port, nil } -func doPkgsite(urlPath string) error { +func doPkgsite(urlPath, fragment string) error { port, err := pickUnusedPort() if err != nil { return fmt.Errorf("failed to find port for documentation server: %v", err) @@ -44,6 +44,9 @@ func doPkgsite(urlPath string) error { if err != nil { 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) // and instead wait for the child process to handle the signal and diff --git a/src/cmd/go/internal/doc/pkgsite_bootstrap.go b/src/cmd/go/internal/doc/pkgsite_bootstrap.go index c909d6184a..3c9f546957 100644 --- a/src/cmd/go/internal/doc/pkgsite_bootstrap.go +++ b/src/cmd/go/internal/doc/pkgsite_bootstrap.go @@ -8,4 +8,4 @@ package doc -func doPkgsite(string) error { return nil } +func doPkgsite(string, string) error { return nil } -- 2.52.0