зеркало из https://github.com/golang/tools.git
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:
Родитель
8445f4f065
Коммит
22683886a9
|
@ -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.
|
// Tests golang/go#38267.
|
||||||
|
|
|
@ -254,8 +254,9 @@ func findWorkspaceModules(ctx context.Context, root span.URI, options *source.Op
|
||||||
if !options.ExperimentalWorkspaceModule {
|
if !options.ExperimentalWorkspaceModule {
|
||||||
path := filepath.Join(root.Filename(), "go.mod")
|
path := filepath.Join(root.Filename(), "go.mod")
|
||||||
if info, _ := os.Stat(path); info != nil {
|
if info, _ := os.Stat(path); info != nil {
|
||||||
m := newModule(ctx, span.URIFromPath(path))
|
if m := getViewModule(ctx, root, span.URIFromPath(path), options); m != nil {
|
||||||
modules[m.rootURI] = m
|
modules[m.rootURI] = m
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return modules, nil
|
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.
|
// We're only interested in go.mod files.
|
||||||
if filepath.Base(path) == "go.mod" {
|
if filepath.Base(path) == "go.mod" {
|
||||||
m := newModule(ctx, span.URIFromPath(path))
|
if m := getViewModule(ctx, root, span.URIFromPath(path), options); m != nil {
|
||||||
modules[m.rootURI] = m
|
modules[m.rootURI] = m
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
@ -1077,8 +1077,11 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Ve
|
||||||
rootURI := span.URIFromPath(filepath.Dir(withoutURI.Filename()))
|
rootURI := span.URIFromPath(filepath.Dir(withoutURI.Filename()))
|
||||||
if currentMod {
|
if currentMod {
|
||||||
if _, ok := result.modules[rootURI]; !ok {
|
if _, ok := result.modules[rootURI]; !ok {
|
||||||
result.modules[rootURI] = newModule(ctx, currentFH.URI())
|
if m := getViewModule(ctx, s.view.rootURI, currentFH.URI(), s.view.options); m != nil {
|
||||||
shouldReinitializeView = true
|
result.modules[m.rootURI] = m
|
||||||
|
shouldReinitializeView = true
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} else if originalMod {
|
} else if originalMod {
|
||||||
// Similarly, we need to retry the IWL if a go.mod in the workspace
|
// 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
|
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()))
|
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))
|
sumURI := span.URIFromPath(sumFilename(modURI))
|
||||||
if info, _ := os.Stat(sumURI.Filename()); info == nil {
|
if info, _ := os.Stat(sumURI.Filename()); info == nil {
|
||||||
sumURI = ""
|
sumURI = ""
|
||||||
|
|
Загрузка…
Ссылка в новой задаче