Skip to content

Commit de83036

Browse files
committed
internal/frontend: redirect on "std/" queries
If a search query is "std/P", where P is a a stdlib package, redirect to P without searching. Also, update the help page on search to mention this, and to correctly reflect the existing behavior of always doing a search for queries without a slash. For golang/go#71452. Change-Id: Idf5c09ee3e81b4c6efa44c2d76470e6f496dcbcd Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/644676 kokoro-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 7301061 commit de83036

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

internal/frontend/search.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ func newSearchResult(r *internal.SearchResult, searchSymbols bool, pr *message.P
344344
// If the user types an existing package path into the search bar, we will
345345
// redirect the user to the details page. Standard library packages that only
346346
// contain one element (such as fmt, errors, etc.) will not redirect, to allow
347-
// users to search by those terms.
347+
// users to search by those terms. However, if the query begins with "std/" and
348+
// then contains a stdlib package, even a one-element one, then it will be redirected.
348349
//
349350
// If the user types a name that is in the form of a Go vulnerability ID, we will
350351
// redirect to the page for that ID (whether or not it exists).
@@ -360,6 +361,10 @@ func searchRequestRedirectPath(ctx context.Context, ds internal.DataSource, quer
360361
if !strings.Contains(requestedPath, "/") || mode == searchModeVuln {
361362
return ""
362363
}
364+
if path, ok := strings.CutPrefix(requestedPath, "std/"); ok && stdlib.Contains(path) {
365+
requestedPath = path
366+
}
367+
363368
_, err := ds.GetUnitMeta(ctx, requestedPath, internal.UnknownModulePath, version.Latest)
364369
if err != nil {
365370
if !errors.Is(err, derrors.NotFound) {

internal/frontend/search_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,12 @@ func TestSearchRequestRedirectPath(t *testing.T) {
533533
{"module", "golang.org/x/tools", "/golang.org/x/tools", ""},
534534
{"directory", "golang.org/x/tools/internal", "/golang.org/x/tools/internal", ""},
535535
{"package", "golang.org/x/tools/internal/lsp", "/golang.org/x/tools/internal/lsp", ""},
536-
{"stdlib package does not redirect", "errors", "", ""},
536+
{"stdlib package does not redirect", "fmt", "", ""},
537537
{"stdlib package does redirect", "cmd/go", "/cmd/go", ""},
538538
{"stdlib directory does redirect", "cmd/go/internal", "/cmd/go/internal", ""},
539539
{"std does not redirect", "std", "", ""},
540+
{"std/stdlib-package does redirect", "std/fmt", "/fmt", ""},
541+
{"std/other-package does not redirect", "std/golang.org/x/tools", "", ""},
540542
{"non-existent path does not redirect", "github.com/non-existent", "", ""},
541543
{"trim URL scheme from query", "https://golang.org/x/tools", "/golang.org/x/tools", ""},
542544
{"Go vuln redirects", "GO-1969-0720", "/vuln/GO-1969-0720?q", ""},

static/frontend/search-help/search-help.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
<p>You can search for packages in which the search text occurs in the package name, package path, synopsis, or README.</p>
1818
<p>Results are grouped by module, displaying the most relevant package in each module.</p>
1919
<p>You can also search for a package by its full or partial import path.</p>
20-
<p>If the package path you specified is complete enough, matching a full package import path, you will be brought directly to the details page for the latest version of that package.</p>
20+
<p>If the package path you specified contains a slash and matches a full package import path,
21+
you will be brought directly to the details page for the latest version of that package.
22+
As a special case, a standard library package preceded by "std/" will also redirect to the package.
23+
For example, "path" conducts a search for "path", but "std/path" redirects to the "path" package.
24+
</p>
25+
2126
<h2>Searching by symbol</h2>
2227
<p>You can also search for a symbol by name across all packages. A symbol is a constant, variable, function, type, field, or method.</p>
2328
<p>Searching by symbol will return a list of packages containing the symbol you specify. You can search by the following:</p>
Loading
Loading

0 commit comments

Comments
 (0)