Skip to content

Commit

Permalink
internal/imports: remove the unused ProcessEnv.ModFile field
Browse files Browse the repository at this point in the history
Remove the ProcessEnv.ModFile field, which was never set. Also add
additional comments and superficial cleanup.

Change-Id: I45d63e5039043d01d4280d2d916df6794e437d8a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/559506
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Findley <rfindley@google.com>
  • Loading branch information
findleyr authored and gopherbot committed Jan 31, 2024
1 parent c046c5b commit 0c80ba3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
8 changes: 5 additions & 3 deletions gopls/internal/cache/session.go
Expand Up @@ -201,9 +201,11 @@ func (s *Session) createView(ctx context.Context, def *viewDefinition) (*View, *
}
}
pe = &imports.ProcessEnv{
GocmdRunner: s.gocmdRunner,
BuildFlags: slices.Clone(def.folder.Options.BuildFlags),
ModFlag: "readonly", // processEnv operations should not mutate the modfile
GocmdRunner: s.gocmdRunner,
BuildFlags: slices.Clone(def.folder.Options.BuildFlags),
// TODO(rfindley): an old comment said "processEnv operations should not mutate the modfile"
// But shouldn't we honor the default behavior of mod vendoring?
ModFlag: "readonly",
SkipPathInScan: skipPath,
Env: env,
WorkingDir: def.root.Path(),
Expand Down
11 changes: 10 additions & 1 deletion internal/imports/fix.go
Expand Up @@ -852,7 +852,6 @@ type ProcessEnv struct {

BuildFlags []string
ModFlag string
ModFile string

// SkipPathInScan returns true if the path should be skipped from scans of
// the RootCurrentModule root type. The function argument is a clean,
Expand Down Expand Up @@ -960,6 +959,12 @@ func (e *ProcessEnv) GetResolver() (Resolver, error) {
if err := e.init(); err != nil {
return nil, err
}
// TODO(rfindley): we should only use a gopathResolver here if the working
// directory is actually *in* GOPATH. (I seem to recall an open gopls issue
// for this behavior, but I can't find it).
//
// For gopls, we can optionally explicitly choose a resolver type, since we
// already know the view type.
if len(e.Env["GOMOD"]) == 0 && len(e.Env["GOWORK"]) == 0 {
e.resolver = newGopathResolver(e)
return e.resolver, nil
Expand All @@ -968,6 +973,10 @@ func (e *ProcessEnv) GetResolver() (Resolver, error) {
return e.resolver, nil
}

// buildContext returns the build.Context to use for matching files.
//
// TODO(rfindley): support dynamic GOOS, GOARCH here, when doing cross-platform
// development.
func (e *ProcessEnv) buildContext() (*build.Context, error) {
ctx := build.Default
goenv, err := e.goEnv()
Expand Down
3 changes: 1 addition & 2 deletions internal/imports/mod.go
Expand Up @@ -34,7 +34,7 @@ import (
// - Split scanning the module cache from other ModuleResolver functionality,
// as it is the source of performance woes (and inconsistency).
// - Allow sharing module cache state across multiple ModuleResolvers.
// - Optimize the scan itself, as there is a lot of redundancy statting and
// - Optimize the scan itself, as there is some redundancy statting and
// reading go.mod files.
// - Make it possible to reuse the current state while running a refresh in
// the background.
Expand Down Expand Up @@ -109,7 +109,6 @@ func (r *ModuleResolver) init() error {
inv := gocommand.Invocation{
BuildFlags: r.env.BuildFlags,
ModFlag: r.env.ModFlag,
ModFile: r.env.ModFile,
Env: r.env.env(),
Logf: r.env.Logf,
WorkingDir: r.env.WorkingDir,
Expand Down
6 changes: 5 additions & 1 deletion internal/imports/mod_test.go
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"sync"
"testing"
"time"

"golang.org/x/mod/module"
"golang.org/x/tools/internal/gocommand"
Expand Down Expand Up @@ -1291,14 +1292,17 @@ import (
func BenchmarkScanModCache(b *testing.B) {
env := &ProcessEnv{
GocmdRunner: &gocommand.Runner{},
Logf: b.Logf,
// Uncomment for verbose logging (too verbose to enable by default).
// Logf: b.Logf,
}
exclude := []gopathwalk.RootType{gopathwalk.RootGOROOT}
resolver, err := env.GetResolver()
if err != nil {
b.Fatal(err)
}
start := time.Now()
scanToSlice(resolver, exclude)
b.Logf("warming the mod cache took %v", time.Since(start))
b.ResetTimer()
for i := 0; i < b.N; i++ {
scanToSlice(resolver, exclude)
Expand Down

0 comments on commit 0c80ba3

Please sign in to comment.