Skip to content

[red-knot] Fix playground crashes when diagnostics are stale - #17165

Merged
MichaReiser merged 1 commit into
mainfrom
micha/playground-fix-stale-diagnostics
Apr 3, 2025
Merged

MichaReiser merged 1 commit into
mainfrom
micha/playground-fix-stale-diagnostics

Conversation

@MichaReiser

Copy link
Copy Markdown
Member

Summary

Fixes a crash in the playground where it crashed with an "index out of bounds" error in the Diagnostic::to_range call
after deleting content at the end of the file.

The root cause was that the playground uses useDeferred to avoid too frequent checkFile calls (to get a smoother UX).
However, this has the problem that the rendered diagnostics can be stable (from before the last change).
Rendering the diagnostics can then fail because the toRange call queries the latest content and not the content
from when the diagnostics were created.

The fix is "easy" in the sense that we now eagerly perform the toRange calls. This way, it doesn't matter
when the diagnostics are stale for a few ms.

This problem can only be observed on examples where Red Knot is "slow" (takes more than ~16ms to check) because
only then does useDeferred "debounce" the check calls.

@MichaReiser MichaReiser added playground An issue or PR specific to the Ruff and/or ty playground ty The ty type checker labels Apr 3, 2025
@MichaReiser
MichaReiser requested a review from Copilot April 3, 2025 08:13
@MichaReiser
MichaReiser force-pushed the micha/playground-fix-stale-diagnostics branch from 508f6a9 to da3a092 Compare April 3, 2025 08:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a crash in the playground caused by stale diagnostics by eagerly converting diagnostic information.

  • Replace method calls with property accesses for diagnostic fields.
  • Rename the file open callback from onOpenFile to onFileOpened across the code.
  • Eagerly serialize diagnostics in Chrome.tsx for improved error handling.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
playground/knot/src/Editor/Editor.tsx Updated diagnostic and file open callback handling.
playground/knot/src/Editor/Diagnostics.tsx Changed diagnostic property accesses and removed workspace use.
playground/knot/src/Editor/Chrome.tsx Renamed callback prop and updated diagnostic conversion logic.

Comment thread playground/knot/src/Editor/Chrome.tsx Resolved
Comment thread playground/knot/src/Editor/Chrome.tsx Resolved
Comment thread playground/knot/src/Editor/Chrome.tsx Resolved
@MichaReiser

Copy link
Copy Markdown
Member Author

Copilot oversimplifies things here. We still need to call the WASM methods and that requires using id() etc.

@MichaReiser
MichaReiser merged commit 66355a6 into main Apr 3, 2025
@MichaReiser
MichaReiser deleted the micha/playground-fix-stale-diagnostics branch April 3, 2025 08:18
dcreager added a commit that referenced this pull request Apr 3, 2025
* origin/main:
  [red-knot] Fix more [redundant-cast] false positives (#17170)
  [red-knot] Three-argument type-calls take 'str' as the first argument (#17168)
  Control flow: `return` and `raise` (#17121)
  Bump 0.11.3 (#17173)
  [red-knot] Improve `Debug` implementation for `semantic_index::SymbolTable` (#17172)
  [red-knot] Fix `str(…)` calls (#17163)
  [red-knot] visibility_constraint analysis for match cases (#17077)
  [red-knot] Fix playground crashes when diagnostics are stale (#17165)
dcreager added a commit that referenced this pull request Apr 3, 2025
* origin/main: (82 commits)
  [red-knot] Fix more [redundant-cast] false positives (#17170)
  [red-knot] Three-argument type-calls take 'str' as the first argument (#17168)
  Control flow: `return` and `raise` (#17121)
  Bump 0.11.3 (#17173)
  [red-knot] Improve `Debug` implementation for `semantic_index::SymbolTable` (#17172)
  [red-knot] Fix `str(…)` calls (#17163)
  [red-knot] visibility_constraint analysis for match cases (#17077)
  [red-knot] Fix playground crashes when diagnostics are stale (#17165)
  [red-knot] Callable types are disjoint from literals (#17160)
  [red-knot] Fix inference for `pow` between two literal integers (#17161)
  [red-knot] Add GitHub PR annotations when mdtests fail in CI (#17150)
  [red-knot] Fix equivalence of differently ordered unions that contain `Callable` types (#17145)
  [red-knot] Add initial set of tests for unreachable code (#17159)
  [`airflow`] Move `AIR302` to `AIR301` and `AIR303` to `AIR302` (#17151)
  ruff_db: simplify lifetimes on `DiagnosticDisplay`
  [red-knot] Detect division-by-zero in unions and intersections (#17157)
  [`airflow`] Add autofix infrastructure to `AIR302` name checks (#16965)
  [`flake8-bandit`] Mark `str` and `list[str]` literals as trusted input (`S603`) (#17136)
  [`airflow`] Add autofix for `AIR302` attribute checks (#16977)
  [`airflow`] Extend `AIR302` with additional symbols (#17085)
  ...
maxmynter pushed a commit to maxmynter/ruff that referenced this pull request Apr 3, 2025
…sh#17165)

## Summary

Fixes a crash in the playground where it crashed with an "index out of
bounds" error in the `Diagnostic::to_range` call
after deleting content at the end of the file. 

The root cause was that the playground uses `useDeferred` to avoid too
frequent `checkFile` calls (to get a smoother UX).
However, this has the problem that the rendered `diagnostics` can be
stable (from before the last change).
Rendering the diagnostics can then fail because the `toRange` call
queries the latest content and not the content
from when the diagnostics were created.

The fix is "easy" in the sense that we now eagerly perform the `toRange`
calls. This way, it doesn't matter
when the diagnostics are stale for a few ms. 

This problem can only be observed on examples where Red Knot is "slow"
(takes more than ~16ms to check) because
only then does `useDeferred` "debounce" the `check` calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

playground An issue or PR specific to the Ruff and/or ty playground ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants