1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:44:43 -07:00

internal/lsp: improve handling of non-Go folders

CL 244117 introduced a bug when modFile == os.DevNull: v.root is left
uninitialized, resulting in a view that appears to own all files. Fixing
that exposes a problem where opening a folder with no Go files and
GO111MODULE=on shows a popup. Skip the popup when no Go files are found.

Change-Id: I7f8b2d6fd2f954af64c3a65156ff44c649f3a5b2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/248620
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-08-14 16:22:27 -04:00
parent d00afeaade
commit 118ac038d7
5 changed files with 43 additions and 7 deletions

View File

@ -170,6 +170,7 @@ func (s *Session) createView(ctx context.Context, name string, folder span.URI,
cancel: cancel,
name: name,
folder: folder,
root: folder,
filesByURI: make(map[span.URI]*fileBase),
filesByBase: make(map[string][]*fileBase),
}

View File

@ -734,7 +734,6 @@ func (v *View) setBuildInformation(ctx context.Context, folder span.URI, options
v.sumURI = span.URIFromPath(sumFilename)
}
v.root = v.folder
if options.ExpandWorkspaceToModule && v.modURI != "" {
v.root = span.URIFromPath(filepath.Dir(v.modURI.Filename()))
}

View File

@ -8,6 +8,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
@ -317,7 +318,20 @@ func toProtocolDiagnostics(diagnostics []*source.Diagnostic) []protocol.Diagnost
func (s *Server) handleFatalErrors(ctx context.Context, snapshot source.Snapshot, modErr, loadErr error) bool {
modURI := snapshot.View().ModFile()
// We currently only have workarounds for errors associated with modules.
// If the folder has no Go code in it, we shouldn't spam the user with a warning.
var hasGo bool
_ = filepath.Walk(snapshot.View().Folder().Filename(), func(path string, info os.FileInfo, err error) error {
if !strings.HasSuffix(info.Name(), ".go") {
return nil
}
hasGo = true
return errors.New("done")
})
if !hasGo {
return true
}
// All other workarounds are for errors associated with modules.
if modURI == "" {
return false
}

View File

@ -541,14 +541,36 @@ func f() {
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1),
NoDiagnostics("a.go"),
),
EmptyShowMessage(""),
NoShowMessage(),
)
// introduce an error, expect no Show Message
env.RegexpReplace("a.go", "func", "fun")
env.Await(env.DiagnosticAtRegexp("a.go", "fun"), EmptyShowMessage(""))
env.Await(env.DiagnosticAtRegexp("a.go", "fun"), NoShowMessage())
})
}
func TestNonGoFolder(t *testing.T) {
const files = `
-- hello.txt --
hi mom
`
for _, go111module := range []string{"on", "off", ""} {
t.Run(fmt.Sprintf("GO111MODULE_%v", go111module), func(t *testing.T) {
withOptions(WithEditorConfig(fake.EditorConfig{
Env: map[string]string{"GO111MODULE": go111module},
})).run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("hello.txt")
env.Await(
OnceMet(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1),
NoShowMessage(),
),
)
})
})
}
}
// Tests golang/go#38602.
func TestNonexistentFileDiagnostics_Issue38602(t *testing.T) {
const collision = `

View File

@ -375,11 +375,11 @@ func NoOutstandingWork() SimpleExpectation {
}
}
// EmptyShowMessage asserts that the editor has not received a ShowMessage.
func EmptyShowMessage(title string) SimpleExpectation {
// NoShowMessage asserts that the editor has not received a ShowMessage.
func NoShowMessage() SimpleExpectation {
check := func(s State) (Verdict, interface{}) {
if len(s.showMessage) == 0 {
return Met, title
return Met, "no ShowMessage"
}
return Unmeetable, nil
}