internal/lsp: fix go.mod creation without experimental workspace module

We were previously adding modules to the snapshot, even if they weren't
relevant without the workspace module mode. Now, check that the modules
are relevant before adding them.

Change-Id: Ib7600482992d538db2f7451863fee5709a35ffb3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/258719
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Rebecca Stambler 2020-10-01 11:35:17 -04:00
Родитель 8445f4f065
Коммит 22683886a9
3 изменённых файлов: 36 добавлений и 7 удалений

Просмотреть файл

@ -280,6 +280,23 @@ func Hello() {
)
})
})
t.Run("without workspace module", func(t *testing.T) {
withOptions(EditorConfig{
WithoutExperimentalWorkspaceModule: true,
}).run(t, noMod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
if err := env.Sandbox.RunGoCommand(env.Ctx, "", "mod", []string{"init", "mod.com"}); err != nil {
t.Fatal(err)
}
env.Await(
EmptyDiagnostics("main.go"),
env.DiagnosticAtRegexp("bob/bob.go", "x"),
)
})
})
}
// Tests golang/go#38267.

10
internal/lsp/cache/session.go поставляемый
Просмотреть файл

@ -254,8 +254,9 @@ func findWorkspaceModules(ctx context.Context, root span.URI, options *source.Op
if !options.ExperimentalWorkspaceModule {
path := filepath.Join(root.Filename(), "go.mod")
if info, _ := os.Stat(path); info != nil {
m := newModule(ctx, span.URIFromPath(path))
modules[m.rootURI] = m
if m := getViewModule(ctx, root, span.URIFromPath(path), options); m != nil {
modules[m.rootURI] = m
}
}
return modules, nil
}
@ -277,8 +278,9 @@ func findWorkspaceModules(ctx context.Context, root span.URI, options *source.Op
}
// We're only interested in go.mod files.
if filepath.Base(path) == "go.mod" {
m := newModule(ctx, span.URIFromPath(path))
modules[m.rootURI] = m
if m := getViewModule(ctx, root, span.URIFromPath(path), options); m != nil {
modules[m.rootURI] = m
}
}
return nil
})

16
internal/lsp/cache/snapshot.go поставляемый
Просмотреть файл

@ -1077,8 +1077,11 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
rootURI := span.URIFromPath(filepath.Dir(withoutURI.Filename()))
if currentMod {
if _, ok := result.modules[rootURI]; !ok {
result.modules[rootURI] = newModule(ctx, currentFH.URI())
shouldReinitializeView = true
if m := getViewModule(ctx, s.view.rootURI, currentFH.URI(), s.view.options); m != nil {
result.modules[m.rootURI] = m
shouldReinitializeView = true
}
}
} else if originalMod {
// Similarly, we need to retry the IWL if a go.mod in the workspace
@ -1574,8 +1577,15 @@ func (s *snapshot) BuildWorkspaceModFile(ctx context.Context) (*modfile.File, er
return file, nil
}
func newModule(ctx context.Context, modURI span.URI) *moduleRoot {
func getViewModule(ctx context.Context, viewRootURI, modURI span.URI, options *source.Options) *moduleRoot {
rootURI := span.URIFromPath(filepath.Dir(modURI.Filename()))
// If we are not in multi-module mode, check that the affected module is
// in the workspace root.
if !options.ExperimentalWorkspaceModule {
if span.CompareURI(rootURI, viewRootURI) != 0 {
return nil
}
}
sumURI := span.URIFromPath(sumFilename(modURI))
if info, _ := os.Stat(sumURI.Filename()); info == nil {
sumURI = ""