Fix diagnostic race condition when switching Python interpreters
Summary: When switching Python interpreters via workspace/didChangeConfiguration, the LSP would sometimes show stale diagnostics because of a race condition in how config invalidation and diagnostic publishing interacted.
The issue was that invalidate_config_and_validate_in_memory() would:
- Invalidate configs and mark modules as dirty
- Commit the transaction with updated configs
- Send RecheckFinished event to trigger diagnostic publishing
However, the RecheckFinished event handler could run and publish diagnostics before modules had actually re-run with the new config, resulting in diagnostics that still showed errors from the old interpreter's site-packages.
The fix is to publish diagnostics immediately after committing the config invalidation transaction, using a fresh transaction that reflects the newly committed state. This ensures diagnostics always use the updated configs.
Additionally, extracted the diagnostic publishing logic into a reusable publish_diagnostics_for_handles() method to avoid code duplication.
This likely helps with https://github.com/facebook/pyrefly/issues/1667
Differential Revision: D87848835
@kinto0 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D87848835.