Skip to content

Commit 84299a0

Browse files
committed
gopls/internal/lsp/cache: simplify ad-hoc package warning logic
The old logic used to inspect the syntax, on the assumption that metadata was not reloaded after an import deletion. But the logic of metadataChanges clearly shows that it is reloaded. The new logic now inspects only the metadata. There's already a test that the warning is properly dismissed. A follow-up change will strength-reduce ActivePackages to avoid type-checking. Change-Id: I6b010b5b7b011544cbb69189145208101b94de65 Reviewed-on: https://go-review.googlesource.com/c/tools/+/456257 gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
1 parent a3eef25 commit 84299a0

File tree

2 files changed

+21
-35
lines changed

2 files changed

+21
-35
lines changed

gopls/internal/lsp/cache/snapshot.go

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,50 +1440,30 @@ func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
14401440
return loadErr
14411441
}
14421442

1443+
// A portion of this text is expected by TestBrokenWorkspace_OutsideModule.
14431444
const adHocPackagesWarning = `You are outside of a module and outside of $GOPATH/src.
14441445
If you are using modules, please open your editor to a directory in your module.
14451446
If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new.`
14461447

14471448
func shouldShowAdHocPackagesWarning(snapshot source.Snapshot, pkgs []source.Package) string {
1448-
if snapshot.ValidBuildConfiguration() {
1449-
return ""
1450-
}
1451-
for _, pkg := range pkgs {
1452-
if hasMissingDependencies(pkg) {
1453-
return adHocPackagesWarning
1454-
}
1455-
}
1456-
return ""
1457-
}
1449+
if !snapshot.ValidBuildConfiguration() {
1450+
for _, pkg := range pkgs {
1451+
// TODO(adonovan): strength-reduce the parameter to []Metadata.
1452+
m := snapshot.Metadata(pkg.ID())
1453+
if m == nil {
1454+
continue
1455+
}
14581456

1459-
func hasMissingDependencies(pkg source.Package) bool {
1460-
// We don't invalidate metadata for import deletions,
1461-
// so check the package imports via the syntax tree
1462-
// (not via types.Package.Imports, since it contains packages
1463-
// synthesized under the vendoring-hostile assumption that
1464-
// ImportPath equals PackagePath).
1465-
//
1466-
// rfindley says: it looks like this is intending to implement
1467-
// a heuristic "if go list couldn't resolve import paths to
1468-
// packages, then probably you're not in GOPATH or a module".
1469-
// This is used to determine if we need to show a warning diagnostic.
1470-
// It looks like this logic is implementing the heuristic that
1471-
// "even if the metadata has a MissingDep, if the types.Package
1472-
// doesn't need that dep anymore we shouldn't show the warning".
1473-
// But either we're outside of GOPATH/Module, or we're not...
1474-
//
1475-
// If we invalidate the metadata for import deletions (which
1476-
// should be fast) then we can simply return the blank entries
1477-
// in DepsByImpPath.
1478-
for _, f := range pkg.GetSyntax() {
1479-
for _, imp := range f.Imports {
1480-
importPath := source.UnquoteImportPath(imp)
1481-
if _, err := pkg.ResolveImportPath(importPath); err != nil {
1482-
return true
1457+
// A blank entry in DepsByImpPath
1458+
// indicates a missing dependency.
1459+
for _, importID := range m.DepsByImpPath {
1460+
if importID == "" {
1461+
return adHocPackagesWarning
1462+
}
14831463
}
14841464
}
14851465
}
1486-
return false
1466+
return ""
14871467
}
14881468

14891469
func containsCommandLineArguments(pkgs []source.Package) bool {

gopls/internal/regtest/diagnostics/diagnostics_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ func _() {
543543
// Expect a module/GOPATH error if there is an error in the file at startup.
544544
// Tests golang/go#37279.
545545
func TestBrokenWorkspace_OutsideModule(t *testing.T) {
546+
// Versions of the go command before go1.16 do not support
547+
// native overlays and so do not observe the deletion.
548+
testenv.NeedsGo1Point(t, 16)
549+
546550
const noModule = `
547551
-- a.go --
548552
package foo
@@ -556,8 +560,10 @@ func f() {
556560
Run(t, noModule, func(t *testing.T, env *Env) {
557561
env.OpenFile("a.go")
558562
env.Await(
563+
// Expect the adHocPackagesWarning.
559564
OutstandingWork(lsp.WorkspaceLoadFailure, "outside of a module"),
560565
)
566+
// Deleting the import dismisses the warning.
561567
env.RegexpReplace("a.go", `import "mod.com/hello"`, "")
562568
env.Await(
563569
NoOutstandingWork(),

0 commit comments

Comments
 (0)