internal/lsp: don't keep track of closed overlays

getFile still returns a FileHandle, even if the file doesn't exist on
disk. Work-around this by checking if the file exists before adding
the file handle to the map.

Fixes golang/go#38498

Change-Id: Ie02679068b37bf4f3d19966c6cfcf2361086b1de
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242924
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-07-16 01:15:38 -04:00
parent e66011cbf8
commit 43ed94694c
4 changed files with 41 additions and 1 deletions

View File

@ -60,6 +60,10 @@ type fileHandle struct {
err error
}
func (c *Cache) GetFile(ctx context.Context, uri span.URI) (source.FileHandle, error) {
return c.getFile(ctx, uri)
}
func (c *Cache) getFile(ctx context.Context, uri span.URI) (*fileHandle, error) {
var modTime time.Time
if fi, err := os.Stat(uri.Filename()); err == nil {

View File

@ -211,6 +211,7 @@ func TestRmTest38878Close(t *testing.T) {
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) {
@ -1009,3 +1010,32 @@ func main() {
)
})
}
func TestClosingBuffer(t *testing.T) {
const basic = `
-- go.mod --
module mod.com
go 1.14
-- main.go --
package main
func main() {}
`
runner.Run(t, basic, func(t *testing.T, env *Env) {
env.Await(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
)
env.Editor.OpenFileWithContent(env.Ctx, "foo.go", `package main`)
env.Await(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1),
)
env.CloseBuffer("foo.go")
env.Await(
OnceMet(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidClose), 1),
NoLogMatching(protocol.Info, "packages=0"),
),
)
})
}

View File

@ -285,6 +285,9 @@ type Cache interface {
// FileSet returns the shared fileset used by all files in the system.
FileSet() *token.FileSet
// GetFile returns a file handle for the given URI.
GetFile(ctx context.Context, uri span.URI) (FileHandle, error)
// ParseGoHandle returns a ParseGoHandle for the given file handle.
ParseGoHandle(ctx context.Context, fh FileHandle, mode ParseMode) ParseGoHandle
}

View File

@ -237,7 +237,10 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
if snapshot == nil {
return errors.Errorf("no snapshot for %s", uri)
}
fh, err := snapshot.GetFile(ctx, uri)
// Check if the file exists on disk after it has been closed. Calling
// snapshot.GetFile will add it back to the snapshot even if it doesn't
// exist, which will cause problems.
fh, err := snapshot.View().Session().Cache().GetFile(ctx, uri)
if err != nil {
return err
}