Skip to content

Commit 573915d

Browse files
committed
gopls/internal/lsp/cache: always init the resolver in runProcessEnvFunc
The current logic populates the process env *after* clearing, but clearing uses fields in the process env. To work around this, clearing is not performed on the first run. Chesterton's fence may apply: I don't really understand this, but don't see how it could be correct: if the clearing requires the environment, then the environment should be populated before clearing. For golang/go#59216 Change-Id: Ia0bc0de10284abf9853158e4f7e60de3d338083d Reviewed-on: https://go-review.googlesource.com/c/tools/+/492679 Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent dd09381 commit 573915d

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

gopls/internal/lsp/cache/imports.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ type importsState struct {
2929
cachedModFileHash source.Hash
3030
cachedBuildFlags []string
3131
cachedDirectoryFilters []string
32-
33-
// runOnce records whether runProcessEnvFunc has been called at least once.
34-
// This is necessary to avoid resetting state before the process env is
35-
// populated.
36-
//
37-
// TODO(rfindley): this shouldn't be necessary.
38-
runOnce bool
3932
}
4033

4134
func (s *importsState) runProcessEnvFunc(ctx context.Context, snapshot *snapshot, fn func(*imports.Options) error) error {
@@ -72,24 +65,19 @@ func (s *importsState) runProcessEnvFunc(ctx context.Context, snapshot *snapshot
7265
// update the processEnv. Clearing caches blocks on any background
7366
// scans.
7467
if changed {
75-
// As a special case, skip cleanup the first time -- we haven't fully
76-
// initialized the environment yet and calling GetResolver will do
77-
// unnecessary work and potentially mess up the go.mod file.
78-
if s.runOnce {
79-
if resolver, err := s.processEnv.GetResolver(); err == nil {
80-
if modResolver, ok := resolver.(*imports.ModuleResolver); ok {
81-
modResolver.ClearForNewMod()
82-
}
68+
if err := populateProcessEnvFromSnapshot(ctx, s.processEnv, snapshot); err != nil {
69+
return err
70+
}
71+
72+
if resolver, err := s.processEnv.GetResolver(); err == nil {
73+
if modResolver, ok := resolver.(*imports.ModuleResolver); ok {
74+
modResolver.ClearForNewMod()
8375
}
8476
}
8577

8678
s.cachedModFileHash = modFileHash
8779
s.cachedBuildFlags = currentBuildFlags
8880
s.cachedDirectoryFilters = currentDirectoryFilters
89-
if err := s.populateProcessEnv(ctx, snapshot); err != nil {
90-
return err
91-
}
92-
s.runOnce = true
9381
}
9482

9583
// Run the user function.
@@ -122,11 +110,10 @@ func (s *importsState) runProcessEnvFunc(ctx context.Context, snapshot *snapshot
122110
return nil
123111
}
124112

125-
// populateProcessEnv sets the dynamically configurable fields for the view's
126-
// process environment. Assumes that the caller is holding the s.view.importsMu.
127-
func (s *importsState) populateProcessEnv(ctx context.Context, snapshot *snapshot) error {
128-
pe := s.processEnv
129-
113+
// populateProcessEnvFromSnapshot sets the dynamically configurable fields for
114+
// the view's process environment. Assumes that the caller is holding the
115+
// importsState mutex.
116+
func populateProcessEnvFromSnapshot(ctx context.Context, pe *imports.ProcessEnv, snapshot *snapshot) error {
130117
if snapshot.view.Options().VerboseOutput {
131118
pe.Logf = func(format string, args ...interface{}) {
132119
event.Log(ctx, fmt.Sprintf(format, args...))

0 commit comments

Comments
 (0)