]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/doc: pass URL fragments separately with -http
authorDavid Finkel <davidf@vimeo.com>
Tue, 19 Aug 2025 17:37:18 +0000 (13:37 -0400)
committerMichael Matloob <matloob@google.com>
Wed, 20 Aug 2025 18:19:58 +0000 (11:19 -0700)
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>
src/cmd/go/internal/doc/doc.go
src/cmd/go/internal/doc/pkgsite.go
src/cmd/go/internal/doc/pkgsite_bootstrap.go

index 37501065fed63580a4b902fb72d13a646bd21ed4..fceeaf101efd62292551dd4f64e62116105b0a0f 100644 (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")
                        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.
index 6769536ca5dab9c0296f1ca8e70fb2d21de48748..06289ac4fc9a8a69d01eea063e8457a167b25c13 100644 (file)
@@ -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
index c909d6184a0496c67cd4c5ce18f75de4f8966142..3c9f546957e5a02dd78b588555cb4aa793b46453 100644 (file)
@@ -8,4 +8,4 @@
 
 package doc
 
-func doPkgsite(string) error { return nil }
+func doPkgsite(string, string) error { return nil }