diff options
| author | Michael Matloob <matloob@golang.org> | 2024-01-04 14:02:03 -0500 |
|---|---|---|
| committer | Michael Matloob <matloob@golang.org> | 2024-01-08 21:04:35 +0000 |
| commit | 8b25f917fd1efcced509d19470c6efa46556bdaa (patch) | |
| tree | cb58025c2be9df50910331e6974fcafde325d793 /cmd/pkgsite | |
| parent | a0bbdd60d911497f44b795042acaf959d146beb4 (diff) | |
| download | go-x-pkgsite-8b25f917fd1efcced509d19470c6efa46556bdaa.tar.xz | |
cmd/pkgsite: fix case where we don't have runtime.GOROOT()
If pkgsite is built with -trimpath, runtime.GOROOT() returns an empty
string, so we don't know where the local go stdlib is. In that case,
don't try to serve a local stdlib and instead fall back to the
stdlibZipModuleGetter. Also fix two issues that show up when using the
stdlibZipModuleGetter. First, we try to prefetch fetch.LocalVersion of
the standard library when the server starts. Instead, prefetch latest
which will have the correct behavior with both the stdlib zips and
local stdlib. Second, when we're trying to fetch the stdlib using git
make sure that we're on another branch than the one we're trying to
fetch into because git won't let us fetch into the branch we're on.
Fixes golang/go#64903
Change-Id: I9dfb22d50b7738080490ce2682e0cf187c16d2d1
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/554195
TryBot-Bypass: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'cmd/pkgsite')
| -rw-r--r-- | cmd/pkgsite/main.go | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go index 7e6bd12e..f9eae158 100644 --- a/cmd/pkgsite/main.go +++ b/cmd/pkgsite/main.go @@ -384,13 +384,15 @@ func buildGetters(ctx context.Context, cfg getterConfig) ([]fetch.ModuleGetter, if cfg.useLocalStdlib { goRepo := *goRepoPath if goRepo == "" { - goRepo = runtime.GOROOT() + goRepo = getGOROOT() } - mg, err := fetch.NewGoPackagesStdlibModuleGetter(ctx, goRepo) - if err != nil { - log.Errorf(ctx, "loading packages from stdlib: %v", err) - } else { - getters = append(getters, mg) + if goRepo != "" { // if goRepo == "" we didn't get a *goRepoPath and couldn't find GOROOT. Fall back to the zip files. + mg, err := fetch.NewGoPackagesStdlibModuleGetter(ctx, goRepo) + if err != nil { + log.Errorf(ctx, "loading packages from stdlib: %v", err) + } else { + getters = append(getters, mg) + } } } @@ -404,6 +406,17 @@ func buildGetters(ctx context.Context, cfg getterConfig) ([]fetch.ModuleGetter, return getters, nil } +func getGOROOT() string { + if rg := runtime.GOROOT(); rg != "" { + return rg + } + b, err := exec.Command("go", "env", "GOROOT").Output() + if err != nil { + return "" + } + return strings.TrimSpace(string(b)) +} + func newServer(getters []fetch.ModuleGetter, localModules []frontend.LocalModule, prox *proxy.Client) (*frontend.Server, error) { lds := fetchdatasource.Options{ Getters: getters, @@ -424,7 +437,7 @@ func newServer(getters []fetch.ModuleGetter, localModules []frontend.LocalModule for _, lm := range localModules { go lds.GetUnitMeta(context.Background(), "", lm.ModulePath, fetch.LocalVersion) } - go lds.GetUnitMeta(context.Background(), "", "std", fetch.LocalVersion) + go lds.GetUnitMeta(context.Background(), "", "std", "latest") server, err := frontend.NewServer(frontend.ServerConfig{ DataSourceGetter: func(context.Context) internal.DataSource { return lds }, |
