Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/godoc: add versioning for links to std packages #94

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions internal/godoc/dochtml/dochtml.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/stdlib"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -442,15 +444,21 @@ func buildNoteHeaders(notes map[string][]*doc.Note) map[string]noteHeader {
}

// versionedPkgPath transforms package paths to contain the same version as the
// current module if the package belongs to the module. As a special case,
// versionedPkgPath will not add versions to standard library packages.
// current module if the package belongs to the module.
func versionedPkgPath(pkgPath string, modInfo *ModuleInfo) string {
if modInfo != nil && modInfo.ModulePath == stdlib.ModulePath {
tag, err := stdlib.TagForVersion(modInfo.ResolvedVersion)
if err != nil {
log.Errorf(context.TODO(), "goTagForVersion(%q): %v", modInfo.ResolvedVersion, err)
return pkgPath
}
return fmt.Sprintf("%s@%s", pkgPath, tag)
}

if modInfo == nil || !modInfo.ModulePackages[pkgPath] {
return pkgPath
}
// We don't need to do anything special here for standard library packages
// since pkgPath will never contain the "std/" module prefix, and
// modInfo.ModulePackages contains this prefix for standard library packages.

innerPkgPath := pkgPath[len(modInfo.ModulePath):]
return fmt.Sprintf("%s@%s%s", modInfo.ModulePath, modInfo.ResolvedVersion, innerPkgPath)
}
12 changes: 6 additions & 6 deletions internal/godoc/dochtml/dochtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,24 @@ func TestVersionedPkgPath(t *testing.T) {
want string
}{
{
name: "builtin package is not versioned",
name: "builtin package is versioned",
pkgPath: "builtin",
modInfo: &ModuleInfo{
ModulePath: "std",
ResolvedVersion: "v1.14.4",
ResolvedVersion: "v1.14",
ModulePackages: map[string]bool{"std/builtin": true, "std/net/http": true},
},
want: "builtin",
want: "builtin@go1.14",
},
{
name: "std packages are not versioned",
name: "std packages are versioned",
pkgPath: "net/http",
modInfo: &ModuleInfo{
ModulePath: "std",
ResolvedVersion: "v1.14.4",
ResolvedVersion: "v1.23.0",
ModulePackages: map[string]bool{"std/builtin": true, "std/net/http": true},
},
want: "net/http",
want: "net/http@go1.23.0",
},
{
name: "imports from other modules are not versioned",
Expand Down