Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit 8458922

Browse files
committed
gddo-server: account for vendor dirs in pkg.go.dev redirect
Pkg.go.dev does not support documentation for vendor directories. When showing the corresponding pkg.go.dev link in the header or when automatically redirecting, change the destination to be the pkg.go.dev homepage instead of a vendor link that will 404. Fixes golang/go#38671 Change-Id: Ic7d221d3cc7fef352f5c7097a667fb8dd2ad0340 Reviewed-on: https://go-review.googlesource.com/c/gddo/+/233839 Run-TryBot: Andrew Bonventre <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent b32b9ce commit 8458922

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

gddo-server/assets/templates/importers.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ <h3>Packages that import {{$.pdoc.Name}}</h3>
77
{{end}}
88

99
{{define "PkgGoDevLink"}}
10-
<a href="https://pkg.go.dev{{if .pdoc.ImportPath}}/{{.pdoc.ImportPath}}{{end}}?tab=importedby">pkg.go.dev{{if .pdoc.ImportPath}}/{{.pdoc.ImportPath}}?tab=importedby{{end}}</a>
10+
<a href="https://pkg.go.dev{{if .pdoc.ImportPath}}{{if notVendorPath .pdoc.ImportPath}}/{{.pdoc.ImportPath}}?tab=importedby{{end}}{{end}}">pkg.go.dev{{if .pdoc.ImportPath}}{{if notVendorPath .pdoc.ImportPath}}/{{.pdoc.ImportPath}}?tab=importedby{{end}}{{end}}</a>
1111
{{end}}

gddo-server/assets/templates/imports.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ <h3>Packages imported by {{.pdoc.Name}}</h3>
77
{{end}}
88

99
{{define "PkgGoDevLink"}}
10-
<a href="https://pkg.go.dev{{if .pdoc.ImportPath}}/{{.pdoc.ImportPath}}{{end}}?tab=imports">pkg.go.dev{{if .pdoc.ImportPath}}/{{.pdoc.ImportPath}}?tab=imports{{end}}</a>
10+
<a href="https://pkg.go.dev{{if .pdoc.ImportPath}}{{if notVendorPath .pdoc.ImportPath}}/{{.pdoc.ImportPath}}?tab=imports{{end}}{{end}}">pkg.go.dev{{if .pdoc.ImportPath}}{{if notVendorPath .pdoc.ImportPath}}/{{.pdoc.ImportPath}}?tab=imports{{end}}{{end}}</a>
1111
{{end}}

gddo-server/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,12 @@ func pkgGoDevURL(godocURL *url.URL) *url.URL {
11711171
u := &url.URL{Scheme: "https", Host: pkgGoDevHost}
11721172
q := url.Values{"utm_source": []string{"godoc"}}
11731173

1174+
if strings.Contains(godocURL.Path, "/vendor/") || strings.HasSuffix(godocURL.Path, "/vendor") {
1175+
u.Path = "/"
1176+
u.RawQuery = q.Encode()
1177+
return u
1178+
}
1179+
11741180
switch godocURL.Path {
11751181
case "/-/go":
11761182
u.Path = "/std"

gddo-server/main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ func TestGodoc(t *testing.T) {
151151
from: "https://godoc.org/cloud.google.com/go/storage?importers",
152152
to: "https://pkg.go.dev/cloud.google.com/go/storage?tab=importedby&utm_source=godoc",
153153
},
154+
{
155+
from: "https://godoc.org/golang.org/x/vgo/vendor/cmd/go/internal/modfile",
156+
to: "https://pkg.go.dev/?utm_source=godoc",
157+
},
158+
{
159+
from: "https://godoc.org/golang.org/x/vgo/vendor",
160+
to: "https://pkg.go.dev/?utm_source=godoc",
161+
},
154162
}
155163

156164
for _, tc := range testCases {

gddo-server/template.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ func joinTemplateDir(base string, files []string) []string {
521521

522522
const pkgGoDevLinkTmpl = `
523523
{{define "PkgGoDevLink"}}
524-
<a href="https://pkg.go.dev{{if .pdoc.ImportPath}}/{{.pdoc.ImportPath}}{{end}}">pkg.go.dev{{if .pdoc.ImportPath}}/{{.pdoc.ImportPath}}{{end}}</a>
524+
<a href="https://pkg.go.dev{{if .pdoc.ImportPath}}{{if notVendorPath .pdoc.ImportPath}}/{{.pdoc.ImportPath}}{{end}}{{end}}">pkg.go.dev{{if .pdoc.ImportPath}}{{if notVendorPath .pdoc.ImportPath}}/{{.pdoc.ImportPath}}{{end}}{{end}}</a>
525525
{{end}}
526526
`
527527

@@ -559,6 +559,7 @@ func parseTemplates(dir string, cb *httputil.CacheBusters, v *viper.Viper) (temp
559559
"relativePath": relativePathFn,
560560
"sidebarEnabled": func() bool { return v.GetBool(ConfigSidebar) },
561561
"staticPath": func(p string) string { return cb.AppendQueryParam(p, "v") },
562+
"notVendorPath": func(p string) bool { return !strings.Contains(p, "/vendor") },
562563
}
563564
for _, set := range htmlSets {
564565
templateName := set[0]

0 commit comments

Comments
 (0)