ruff/playground
Micha Reiser 66355a6185
[red-knot] Fix playground crashes when diagnostics are stale (#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.
2025-04-03 10:18:36 +02:00
..
api [playground] Use cursor for clickable elements (#16833) 2025-03-18 18:06:00 +01:00
knot [red-knot] Fix playground crashes when diagnostics are stale (#17165) 2025-04-03 10:18:36 +02:00
ruff [playground] Allow selecting the diagnostic message (#17051) 2025-03-28 20:58:05 +00:00
shared [red-knot] Incorporate recent ruff server improvements into red knot's LSP (#17044) 2025-03-28 18:39:18 +00:00
.gitignore Remove all `useEffect` usages (#12659) 2024-08-08 13:16:38 +02:00
.prettierignore Red Knot Playground (#12681) 2025-03-18 17:17:11 +01:00
README.md Red Knot Playground (#12681) 2025-03-18 17:17:11 +01:00
eslint.config.mjs Red Knot Playground (#12681) 2025-03-18 17:17:11 +01:00
package-lock.json Update dependency vite to v6.2.4 (#17104) 2025-04-01 08:29:59 +02:00
package.json Red Knot Playground (#12681) 2025-03-18 17:17:11 +01:00
tsconfig.json Red Knot Playground (#12681) 2025-03-18 17:17:11 +01:00
tsconfig.node.json Red Knot Playground (#12681) 2025-03-18 17:17:11 +01:00

README.md

playground

In-browser playground for Ruff. Available https://play.ruff.rs/.

Getting started

Install the NPM dependencies with npm install, and run, and run the development server with npm start --workspace ruff-playground or npm start --workspace knot-playground. You may need to restart the server after making changes to Ruff or Red Knot to re-build the WASM module.

To run the datastore, which is based on Workers KV, install the Wrangler CLI, then run npx wrangler dev --local from the ./playground/api directory. Note that the datastore is only required to generate shareable URLs for code snippets. The development datastore does not require Cloudflare authentication or login, but in turn only persists data locally.

Architecture

The playground is implemented as a single-page React application powered by Vite, with the editor experience itself powered by Monaco.

The playground stores state in localStorage, but supports persisting code snippets to a persistent datastore based on Workers KV and exposed via a Cloudflare Worker.

The playground design is originally based on Tailwind Play, with additional inspiration from the Biome Playground.

Known issues

Stack overflows

If you see stack overflows in the playground, build the WASM module in release mode: npm run --workspace knot-playground build:wasm.