# project: Persist workspace diagnostics when closing buffer \(\#59875\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/c7b43b20b409b88e3cc359513b3d687fc1c92259)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: c7b43b20b409b88e3cc359513b3d687fc1c92259

Requested commit: c7b43b20b409b88e3cc359513b3d687fc1c92259

Commit: c7b43b20b409b88e3cc359513b3d687fc1c92259

Tree: 88bb17491190cf782494b67015f96576b4e1002c

Author: Xin Zhao

Committer: GitHub

## Message

```
project: Persist workspace diagnostics when closing buffer (#59875)

# Objective

Closes #50739

Many language servers have settings for their diagnostic mode, typically
supporting either "single file" mode or "workspace" mode.

In single file mode, the language server only generates diagnostics for
files that the user has open. When a file is closed, those diagnostics
are cleared. This is the behavior currently implemented in Zed.

However, when a language server runs in workspace mode, we should retain
all diagnostics regardless of whether the corresponding file is open or
closed. As reported in #50739, Zed currently discards these diagnostics
when a buffer is closed, just like in single file mode.

When a buffer is completely closed, Zed triggers a diagnostics update to
perform cleanup:

https://github.com/zed-industries/zed/blob/8372eb1b131ab3b8c0622e930e985f15eb970687/crates/project/src/lsp_store.rs#L4747-L4773
and the Pulled diagnostics are all cleaned, that's why when closing a
buffer, the diagnostic summary disappeared. This process aggressively
clears all `Pulled` diagnostics, causing the diagnostic summary for the
closed file to disappear.

## Solution

Filter the target language servers when updating diagnostics on buffer
close. If a language server has workspace diagnostics enabled, we skip
clearing its diagnostics.

## Testing

Added two new integration tests in
`crates/project/tests/integration/project_tests.rs`:
1.
`test_diagnostic_summaries_cleared_on_buffer_close_without_workspace_diagnostics`:
Verifies that diagnostics are cleared on buffer close when workspace
diagnostics is disabled.
2.
`test_diagnostic_summaries_retained_on_buffer_close_with_workspace_diagnostics`:
Verifies that diagnostics are retained when workspace diagnostics are
enabled. (This test fails on the current `main` branch).

I have also verified this functionality locally. Both local and remote
development environments work as expected.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

## Showcase

<details>
  <summary>Click to view showcase</summary>

My super cool demos here

</details>

---

Release Notes:

- Fixed workspace diagnostics being cleared when closing a buffer.
```

## Parents

- [c28e37687d566d7fe20d959622f0ff172e546acf](https://git.cafe/gitcafe/zed/commit/c28e37687d566d7fe20d959622f0ff172e546acf?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/c7b43b20b409b88e3cc359513b3d687fc1c92259?format=markdown)
