Skip to content

Commit b35b03d

Browse files
authored
LSP fix file close (#4099)
1 parent 8b05170 commit b35b03d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

private/buf/buflsp/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ func (s *server) DidClose(
337337
ctx context.Context,
338338
params *protocol.DidCloseTextDocumentParams,
339339
) error {
340-
s.fileManager.Close(ctx, params.TextDocument.URI)
340+
if file := s.fileManager.Get(params.TextDocument.URI); file != nil {
341+
file.Close(ctx)
342+
}
341343
return nil
342344
}
343345

private/buf/buflsp/workspace.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ func newWorkspaceManager(lsp *lsp) *workspaceManager {
4848
// LeaseWorkspace attempts to find and lease the workspace for the given URI string. If the
4949
// workspace has not been seen before, a new workspace is created. This may fail.
5050
func (w *workspaceManager) LeaseWorkspace(ctx context.Context, uri protocol.URI) (*workspace, error) {
51-
// Run a cleanup as a lazy job.
52-
defer w.Cleanup(ctx)
51+
defer func() {
52+
// Run a cleanup as a lazy job.
53+
w.Cleanup(ctx)
54+
w.lsp.logger.Debug("workspace: lease workspace", slog.Int("active", len(w.workspaces)))
55+
}()
5356

5457
workspace, err := w.getOrCreateWorkspace(ctx, uri)
5558
if err != nil {
@@ -81,7 +84,7 @@ func (w *workspaceManager) Cleanup(ctx context.Context) {
8184
func (w *workspaceManager) getOrCreateWorkspace(ctx context.Context, uri protocol.URI) (*workspace, error) {
8285
// This looks for a workspace that already has ownership over the URI.
8386
// If a new file is added we will create a new workspace.
84-
// Reusing workspaces is an optimization
87+
// Reusing workspaces is an optimization. Matching is on best-effort.
8588
fileName := uri.Filename()
8689
for _, workspace := range w.workspaces {
8790
if _, ok := workspace.fileNameToFileInfo[fileName]; ok {

0 commit comments

Comments
 (0)