1
0
mirror of https://github.com/golang/go synced 2024-11-18 22:55:23 -07:00

internal/lsp: warn user in case of a multi-file ad-hoc package

This change surfaces a warning to the user if they might be coding in a
multi-file ad-hoc package.

Updates golang/go#36416

Change-Id: Ifaa15a0777ea97e62c1477fb33911636b13e073e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/213458
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-01-06 17:31:24 -05:00
parent f270e23f6a
commit 675cf513f4

View File

@ -7,6 +7,7 @@ package source
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis"
"golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/protocol"
@ -66,6 +67,14 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, fh FileHandle, withAnal
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
// If we have a package with a single file and errors about "undeclared" symbols,
// we may have an ad-hoc package with multiple files. Show a warning message.
// TODO(golang/go#36416): Remove this when golang.org/cl/202277 is merged.
if warningMsg == "" && len(pkg.CompiledGoFiles()) == 1 && hasUndeclaredErrors(pkg) {
if warningMsg, err = checkCommonErrors(ctx, snapshot.View(), fh.Identity().URI); err != nil {
log.Error(ctx, "error checking common errors", err, telemetry.File.Of(fh.Identity().URI))
}
}
// Prepare the reports we will send for the files in this package. // Prepare the reports we will send for the files in this package.
reports := make(map[FileIdentity][]Diagnostic) reports := make(map[FileIdentity][]Diagnostic)
for _, fh := range pkg.CompiledGoFiles() { for _, fh := range pkg.CompiledGoFiles() {
@ -255,3 +264,17 @@ func onlyDeletions(fixes []SuggestedFix) bool {
} }
return true return true
} }
// hasUndeclaredErrors returns true if a package has a type error
// about an undeclared symbol.
func hasUndeclaredErrors(pkg Package) bool {
for _, err := range pkg.GetErrors() {
if err.Kind != TypeError {
continue
}
if strings.Contains(err.Message, "undeclared name:") {
return true
}
}
return false
}