Skip to content

Commit 2e5f0cf

Browse files
committed
internal/lsp: remove all but one use of the view's modURI field
The view reinitialization logic appears to be broken, and so needs one remaining use of the modURI, which be fixed in a follow-up. Every other use of view.modURI is removed. Updates golang/go#32394 Change-Id: Ic051ed848c30e6981d42a576fb35f40efbeb17a6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/257417 gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> Trust: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent a6f32d1 commit 2e5f0cf

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

gopls/internal/regtest/workspace_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ func Hello() int {
338338
var x int
339339
}
340340
`
341-
run(t, multiModule, func(t *testing.T, env *Env) {
341+
withOptions(
342+
WithProxyFiles(workspaceModuleProxy),
343+
).run(t, multiModule, func(t *testing.T, env *Env) {
342344
env.OpenFile("modb/go.mod")
343345
env.Await(
344346
OnceMet(

internal/lsp/cache/session.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,7 @@ func (s *Session) createView(ctx context.Context, name string, folder span.URI,
227227
modules: modules,
228228
}
229229

230-
// TODO(rstambler): Change this function to work without a snapshot.
231-
// Set the first snapshot's workspace directories. The view's modURI was
232-
// set by setBuildInformation.
233-
var fh source.FileHandle
234-
if v.modURI != "" {
235-
fh, _ = s.GetFile(ctx, v.modURI)
236-
}
237-
v.snapshot.workspaceDirectories = v.snapshot.findWorkspaceDirectories(ctx, fh)
230+
v.snapshot.workspaceDirectories = v.snapshot.findWorkspaceDirectories(ctx)
238231

239232
// Initialize the view without blocking.
240233
initCtx, initCancel := context.WithCancel(xcontext.Detach(ctx))

internal/lsp/cache/snapshot.go

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@ func (s *snapshot) AwaitInitialized(ctx context.Context) {
809809

810810
// reloadWorkspace reloads the metadata for all invalidated workspace packages.
811811
func (s *snapshot) reloadWorkspace(ctx context.Context) error {
812-
// If the view's build configuration is invalid, we cannot reload by package path.
813-
// Just reload the directory instead.
812+
// If the view's build configuration is invalid, we cannot reload by
813+
// package path. Just reload the directory instead.
814814
if !s.view.hasValidBuildConfiguration {
815815
return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
816816
}
@@ -1001,6 +1001,8 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
10011001
result.modules[k] = v
10021002
}
10031003

1004+
var modulesChanged, shouldReinitializeView bool
1005+
10041006
// transitiveIDs keeps track of transitive reverse dependencies.
10051007
// If an ID is present in the map, invalidate its types.
10061008
// If an ID's value is true, invalidate its metadata too.
@@ -1044,6 +1046,8 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
10441046
currentMod := currentExists && currentFH.Kind() == source.Mod
10451047
originalMod := originalFH != nil && originalFH.Kind() == source.Mod
10461048
if currentMod || originalMod {
1049+
modulesChanged = true
1050+
10471051
// If the view's go.mod file's contents have changed, invalidate
10481052
// the metadata for every known package in the snapshot.
10491053
if invalidateMetadata {
@@ -1061,16 +1065,15 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
10611065
rootURI := span.URIFromPath(filepath.Dir(withoutURI.Filename()))
10621066
if currentMod {
10631067
if _, ok := result.modules[rootURI]; !ok {
1064-
m := newModule(ctx, currentFH.URI())
1065-
result.modules[m.rootURI] = m
1066-
result.view.definitelyReinitialize()
1068+
result.modules[rootURI] = newModule(ctx, currentFH.URI())
1069+
shouldReinitializeView = true
10671070
}
10681071
} else if originalMod {
10691072
// Similarly, we need to retry the IWL if a go.mod in the workspace
10701073
// was deleted.
10711074
if _, ok := result.modules[rootURI]; ok {
10721075
delete(result.modules, rootURI)
1073-
result.view.definitelyReinitialize()
1076+
shouldReinitializeView = true
10741077
}
10751078
}
10761079
}
@@ -1091,12 +1094,6 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
10911094
}
10921095
}
10931096
}
1094-
if withoutURI == s.view.modURI {
1095-
// The go.mod's replace directives may have changed. We may
1096-
// need to update our set of workspace directories. Use the new
1097-
// snapshot, as it can be locked without causing issues.
1098-
result.workspaceDirectories = result.findWorkspaceDirectories(ctx, currentFH)
1099-
}
11001097

11011098
// If this is a file we don't yet know about,
11021099
// then we do not yet know what packages it should belong to.
@@ -1147,6 +1144,14 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
11471144
// Make sure to remove the changed file from the unloadable set.
11481145
delete(result.unloadableFiles, withoutURI)
11491146
}
1147+
1148+
// When modules change, we need to recompute their workspace directories,
1149+
// as replace directives may have changed.
1150+
if modulesChanged {
1151+
// Use the new snapshot, as it can be locked without causing issues.
1152+
result.workspaceDirectories = result.findWorkspaceDirectories(ctx)
1153+
}
1154+
11501155
// Copy the package type information.
11511156
for k, v := range s.packages {
11521157
if _, ok := transitiveIDs[k.id]; ok {
@@ -1208,6 +1213,10 @@ copyIDs:
12081213
result.workspacePackages[id] = pkgPath
12091214
}
12101215

1216+
if shouldReinitializeView && s.view.hasValidBuildConfiguration {
1217+
s.view.definitelyReinitialize()
1218+
}
1219+
12111220
// Inherit all of the go.mod-related handles.
12121221
for _, v := range result.modTidyHandles {
12131222
newGen.Inherit(v.handle)
@@ -1309,35 +1318,35 @@ func (s *snapshot) shouldInvalidateMetadata(ctx context.Context, newSnapshot *sn
13091318
//
13101319
// It assumes that the file handle is the view's go.mod file, if it has one.
13111320
// The caller need not be holding the snapshot's mutex, but it might be.
1312-
func (s *snapshot) findWorkspaceDirectories(ctx context.Context, modFH source.FileHandle) map[span.URI]struct{} {
1313-
m := map[span.URI]struct{}{
1314-
s.view.rootURI: {},
1315-
}
1321+
func (s *snapshot) findWorkspaceDirectories(ctx context.Context) map[span.URI]struct{} {
13161322
// If the view does not have a go.mod file, only the root directory
13171323
// is known. In GOPATH mode, we should really watch the entire GOPATH,
13181324
// but that's too expensive.
1319-
modURI := s.view.modURI
1320-
if modURI == "" {
1321-
return m
1322-
}
1323-
if modFH == nil {
1324-
return m
1325-
}
1326-
// Ignore parse errors. An invalid go.mod is not fatal.
1327-
mod, err := s.ParseMod(ctx, modFH)
1328-
if err != nil {
1329-
return m
1325+
dirs := map[span.URI]struct{}{
1326+
s.view.rootURI: {},
13301327
}
1331-
for _, r := range mod.File.Replace {
1332-
// We may be replacing a module with a different version, not a path
1333-
// on disk.
1334-
if r.New.Version != "" {
1328+
for _, m := range s.modules {
1329+
fh, err := s.GetFile(ctx, m.modURI)
1330+
if err != nil {
13351331
continue
13361332
}
1337-
uri := span.URIFromPath(r.New.Path)
1338-
m[uri] = struct{}{}
1333+
// Ignore parse errors. An invalid go.mod is not fatal.
1334+
// TODO(rstambler): Try to preserve existing watched directories as
1335+
// much as possible, otherwise we will thrash when a go.mod is edited.
1336+
mod, err := s.ParseMod(ctx, fh)
1337+
if err != nil {
1338+
continue
1339+
}
1340+
for _, r := range mod.File.Replace {
1341+
// We may be replacing a module with a different version, not a path
1342+
// on disk.
1343+
if r.New.Version != "" {
1344+
continue
1345+
}
1346+
dirs[span.URIFromPath(r.New.Path)] = struct{}{}
1347+
}
13391348
}
1340-
return m
1349+
return dirs
13411350
}
13421351

13431352
func (s *snapshot) BuiltinPackage(ctx context.Context) (*source.BuiltinPackage, error) {

0 commit comments

Comments
 (0)