Skip to content

Commit 82f79ed

Browse files
committed
cmd/pkgsite: more changes to use local stdlib
This cl makes more changes to enable using the local stdlib. It always uses the local stdlib from cmd/pkgsite. It also changes code that processes and shows versions to expect and pass through v0.0.0, the fake version we use when we show local packages. For #57742 Change-Id: Ic407e58e9658b0703dbc6df47f40bbe1f1bd2b2a Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/517915 kokoro-CI: kokoro <[email protected]> Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 17236d3 commit 82f79ed

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

cmd/pkgsite/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func main() {
110110
flag.BoolVar(&serverCfg.useCache, "cache", false, "fetch from the module cache")
111111
flag.StringVar(&serverCfg.cacheDir, "cachedir", "", "module cache directory (defaults to `go env GOMODCACHE`)")
112112
flag.BoolVar(&serverCfg.useListedMods, "list", true, "for each path, serve all modules in build list")
113+
serverCfg.useLocalStdlib = true
113114

114115
flag.Usage = func() {
115116
out := flag.CommandLine.Output()
@@ -380,11 +381,6 @@ func buildGetters(ctx context.Context, cfg getterConfig) ([]fetch.ModuleGetter,
380381
getters = append(getters, g)
381382
}
382383

383-
// Add a proxy
384-
if cfg.proxy != nil {
385-
getters = append(getters, fetch.NewProxyModuleGetter(cfg.proxy, source.NewClient(time.Second)))
386-
}
387-
388384
if cfg.useLocalStdlib {
389385
goRepo := *goRepoPath
390386
if goRepo == "" {
@@ -398,6 +394,11 @@ func buildGetters(ctx context.Context, cfg getterConfig) ([]fetch.ModuleGetter,
398394
}
399395
}
400396

397+
// Add a proxy
398+
if cfg.proxy != nil {
399+
getters = append(getters, fetch.NewProxyModuleGetter(cfg.proxy, source.NewClient(time.Second)))
400+
}
401+
401402
getters = append(getters, fetch.NewStdlibZipModuleGetter())
402403

403404
return getters, nil

cmd/pkgsite/main_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ package a
5656

5757
cfg := func(modifyDefault func(*serverConfig)) serverConfig {
5858
c := serverConfig{
59-
paths: []string{localModule},
60-
gopathMode: false,
61-
useListedMods: true,
62-
useLocalStdlib: true,
63-
useCache: true,
64-
cacheDir: cacheDir,
65-
proxy: prox,
59+
paths: []string{localModule},
60+
gopathMode: false,
61+
useListedMods: true,
62+
useCache: true,
63+
cacheDir: cacheDir,
64+
proxy: prox,
6665
}
6766
if modifyDefault != nil {
6867
modifyDefault(&c)

internal/fetch/fetch.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,10 @@ func processModuleContents(ctx context.Context, modulePath, resolvedVersion, req
187187
defer span.End()
188188

189189
v := resolvedVersion
190-
if modulePath == stdlib.ModulePath && stdlib.SupportedBranches[requestedVersion] {
191-
v = requestedVersion
190+
if _, ok := mg.(*stdlibZipModuleGetter); ok {
191+
if modulePath == stdlib.ModulePath && stdlib.SupportedBranches[requestedVersion] {
192+
v = requestedVersion
193+
}
192194
}
193195
sourceInfo, err := mg.SourceInfo(ctx, modulePath, v)
194196
if err != nil {

internal/fetchdatasource/fetchdatasource.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"golang.org/x/pkgsite/internal/fetch"
2424
"golang.org/x/pkgsite/internal/log"
2525
"golang.org/x/pkgsite/internal/proxy"
26-
"golang.org/x/pkgsite/internal/stdlib"
2726
"golang.org/x/pkgsite/internal/version"
2827
)
2928

@@ -166,15 +165,6 @@ func (ds *FetchDataSource) fetch(ctx context.Context, modulePath, version string
166165
} else {
167166
m.RemoveNonRedistributableData()
168167
}
169-
// There is special handling in FetchModule for the standard library,
170-
// that bypasses the getter g. Don't record g as having fetch std.
171-
//
172-
// TODO(rfindley): it would be cleaner if the standard library could be
173-
// its own module getter. This could also allow the go/packages getter to
174-
// serve existing on-disk content for std. See also golang/go#58923.
175-
if modulePath == stdlib.ModulePath {
176-
g = nil
177-
}
178168
return m, g, nil
179169
}
180170
if !errors.Is(fr.Error, derrors.NotFound) {
@@ -187,7 +177,7 @@ func (ds *FetchDataSource) fetch(ctx context.Context, modulePath, version string
187177
func (ds *FetchDataSource) populateUnitSubdirectories(u *internal.Unit, m *internal.Module) {
188178
p := u.Path + "/"
189179
for _, u2 := range m.Units {
190-
if strings.HasPrefix(u2.Path, p) {
180+
if strings.HasPrefix(u2.Path, p) || u.Path == "std" {
191181
var syn string
192182
if len(u2.Documentation) > 0 {
193183
syn = u2.Documentation[0].Synopsis

internal/frontend/urlinfo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"golang.org/x/pkgsite/internal"
1616
"golang.org/x/pkgsite/internal/derrors"
1717
"golang.org/x/pkgsite/internal/experiment"
18+
"golang.org/x/pkgsite/internal/fetch"
1819
"golang.org/x/pkgsite/internal/log"
1920
"golang.org/x/pkgsite/internal/stdlib"
2021
"golang.org/x/pkgsite/internal/version"
@@ -155,6 +156,11 @@ func parseStdLibURLPath(urlPath string) (_ *urlPathInfo, err error) {
155156
tag = strings.TrimSuffix(tag, "/")
156157
info.requestedVersion = stdlib.VersionForTag(tag)
157158
if info.requestedVersion == "" {
159+
if tag == fetch.LocalVersion {
160+
// Special case: 0.0.0 is the version for a local stdlib
161+
info.requestedVersion = fetch.LocalVersion
162+
return info, nil
163+
}
158164
return nil, &userError{
159165
err: fmt.Errorf("invalid Go tag for url: %q", urlPath),
160166
userMessage: fmt.Sprintf("%q is not a valid tag for the standard library", tag),

internal/frontend/versions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"golang.org/x/mod/semver"
1616
"golang.org/x/pkgsite/internal"
17+
"golang.org/x/pkgsite/internal/fetch"
1718
"golang.org/x/pkgsite/internal/log"
1819
"golang.org/x/pkgsite/internal/stdlib"
1920
"golang.org/x/pkgsite/internal/version"
@@ -370,7 +371,7 @@ func pseudoVersionRev(v string) string {
370371

371372
// displayVersion returns the version string, formatted for display.
372373
func displayVersion(modulePath, requestedVersion, resolvedVersion string) string {
373-
if modulePath == stdlib.ModulePath {
374+
if modulePath == stdlib.ModulePath && resolvedVersion != fetch.LocalVersion {
374375
if stdlib.SupportedBranches[requestedVersion] ||
375376
(strings.HasPrefix(resolvedVersion, "v0.0.0") && resolvedVersion != "v0.0.0") { // Plain v0.0.0 is from the go packages module getter
376377
commit := strings.Split(resolvedVersion, "-")[2]
@@ -394,7 +395,7 @@ func displayVersion(modulePath, requestedVersion, resolvedVersion string) string
394395
// a link to this site.
395396
// See TestLinkVersion for examples.
396397
func linkVersion(modulePath, requestedVersion, resolvedVersion string) string {
397-
if modulePath == stdlib.ModulePath {
398+
if modulePath == stdlib.ModulePath && resolvedVersion != fetch.LocalVersion {
398399
if strings.HasPrefix(resolvedVersion, "go") {
399400
return resolvedVersion // already a go version
400401
}

0 commit comments

Comments
 (0)