Skip to content

Commit 4afd482

Browse files
dfinkelmatloob
authored andcommitted
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: golang#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>
1 parent 509d5f6 commit 4afd482

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

src/cmd/go/internal/doc/doc.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,16 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
212212
mod, err := runCmd(append(os.Environ(), "GOWORK=off"), "go", "list", "-m")
213213
if err == nil && mod != "" && mod != "command-line-arguments" {
214214
// If there's a module, go to the module's doc page.
215-
return doPkgsite(mod)
215+
return doPkgsite(mod, "")
216216
}
217217
gowork, err := runCmd(nil, "go", "env", "GOWORK")
218218
if err == nil && gowork != "" {
219219
// Outside a module, but in a workspace, go to the home page
220220
// with links to each of the modules' pages.
221-
return doPkgsite("")
221+
return doPkgsite("", "")
222222
}
223223
// Outside a module or workspace, go to the documentation for the standard library.
224-
return doPkgsite("std")
224+
return doPkgsite("std", "")
225225
}
226226

227227
// 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) {
282282
}
283283
if found {
284284
if serveHTTP {
285-
path, err := objectPath(userPath, pkg, symbol, method)
285+
path, fragment, err := objectPath(userPath, pkg, symbol, method)
286286
if err != nil {
287287
return err
288288
}
289-
return doPkgsite(path)
289+
return doPkgsite(path, fragment)
290290
}
291291
return nil
292292
}
@@ -305,7 +305,8 @@ func runCmd(env []string, cmdline ...string) (string, error) {
305305
return strings.TrimSpace(stdout.String()), nil
306306
}
307307

308-
func objectPath(userPath string, pkg *Package, symbol, method string) (string, error) {
308+
// returns a path followed by a fragment (or an error)
309+
func objectPath(userPath string, pkg *Package, symbol, method string) (string, string, error) {
309310
var err error
310311
path := pkg.build.ImportPath
311312
if path == "." {
@@ -314,18 +315,15 @@ func objectPath(userPath string, pkg *Package, symbol, method string) (string, e
314315
// go list to get the import path.
315316
path, err = runCmd(nil, "go", "list", userPath)
316317
if err != nil {
317-
return "", err
318+
return "", "", err
318319
}
319320
}
320321

321322
object := symbol
322323
if symbol != "" && method != "" {
323324
object = symbol + "." + method
324325
}
325-
if object != "" {
326-
path = path + "#" + object
327-
}
328-
return path, nil
326+
return path, object, nil
329327
}
330328

331329
// failMessage creates a nicely formatted error message when there is no result to show.

src/cmd/go/internal/doc/pkgsite.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func pickUnusedPort() (int, error) {
3434
return port, nil
3535
}
3636

37-
func doPkgsite(urlPath string) error {
37+
func doPkgsite(urlPath, fragment string) error {
3838
port, err := pickUnusedPort()
3939
if err != nil {
4040
return fmt.Errorf("failed to find port for documentation server: %v", err)
@@ -44,6 +44,9 @@ func doPkgsite(urlPath string) error {
4444
if err != nil {
4545
return fmt.Errorf("internal error: failed to construct url: %v", err)
4646
}
47+
if fragment != "" {
48+
path += "#" + fragment
49+
}
4750

4851
// Turn off the default signal handler for SIGINT (and SIGQUIT on Unix)
4952
// and instead wait for the child process to handle the signal and

src/cmd/go/internal/doc/pkgsite_bootstrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
package doc
1010

11-
func doPkgsite(string) error { return nil }
11+
func doPkgsite(string, string) error { return nil }

0 commit comments

Comments
 (0)