1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:38:33 -06:00

internal/lsp: regtests for removing files outside the editor

When a file with errors is removed outside the editor, sometimes its
errors are cleared by the editor and sometimes they are not. If the file
is still open in the editor gopls does not clear the errors, taking the
editor's version as the truth. Otherwise the errors are cleared.
(This behavior depends on the editor sending gopls a notification that
the workspace changed.)

There seems to be no good way yet to test that gopls takes no action after
receiving the didChangeWatchedFiles notification.

Updates golang/go#38878

Change-Id: Ie418dd786d4c5f827cf0665a31f0f9913f7cfdc0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/235377
Run-TryBot: Peter Weinberger <pjw@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
pjw 2020-05-27 07:32:44 -04:00 committed by Peter Weinberger
parent 6be401e3f7
commit 41453949f3
2 changed files with 50 additions and 1 deletions

View File

@ -5,9 +5,12 @@
package regtest
import (
"context"
"fmt"
"log"
"os"
"testing"
"time"
"golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/fake"
@ -176,6 +179,52 @@ const a = http.MethodGet
})
}
// Tests golang/go#38878: good a.go, bad a_test.go, remove a_test.go but its errors remain
// If the file is open in the editor, this is working as intended
// If the file is not open in the editor, the errors go away
const test38878 = `
-- go.mod --
module foo
-- a.go --
package x
func f() {}
-- a_test.go --
package x
import "testing"
func TestA(t *testing.T) {
f(3)
}
`
func TestRmTest38878Close(t *testing.T) {
runner.Run(t, test38878, func(t *testing.T, env *Env) {
env.OpenFile("a_test.go")
env.Await(DiagnosticAt("a_test.go", 5, 3))
env.CloseBuffer("a_test.go")
env.RemoveFileFromWorkspace("a_test.go")
// diagnostics go away
env.Await(EmptyDiagnostics("a_test.go"))
})
}
func TestRmTest38878(t *testing.T) {
log.SetFlags(log.Lshortfile)
runner.Run(t, test38878, func(t *testing.T, env *Env) {
env.OpenFile("a_test.go")
env.Await(DiagnosticAt("a_test.go", 5, 3))
env.Sandbox.Workdir.RemoveFile(context.Background(), "a_test.go")
// diagnostics remain after giving gopls a chance to do something
// (there is not yet a better way to decide gopls isn't going
// to do anything)
time.Sleep(time.Second)
env.Await(DiagnosticAt("a_test.go", 5, 3))
})
}
// TestNoMod confirms that gopls continues to work when a user adds a go.mod
// file to their workspace.
func TestNoMod(t *testing.T) {

View File

@ -134,7 +134,7 @@ func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.Did
if err != nil {
return err
}
// Clear the diagnostics for any deleted files.
// Clear the diagnostics for any deleted files that are not open in the editor.
for uri := range deletions {
if snapshot := snapshots[uri]; snapshot == nil || snapshot.IsOpen(uri) {
continue