safe-ts lsp: a language server, with a VS Code extension #17

Merged
sionzee merged 5 commits from lsp-server into main 2026-11-04 21:29:48 +01:00
Owner

What

A SAFE-TS language server, safe-ts lsp, plus a minimal VS Code extension that starts it. Until now the editor only got SAFE-TS through the oxlint plugin, which can't run on .vue files and sees one file at a time, read from disk.

Server (crates/safe_ts_cli/src/lsp.rs, stdio)

  • Diagnostics for open .ts/.tsx/.mts/.vue files. They come on open, on save, and after a change (150 ms debounce, at most 1 s while typing). They use the same checks as safe-ts check (Run::diagnostics_for), so failures are followed into other modules.
  • Unsaved buffers count: Project::set_overlay makes every read of an open file use the buffer, including when another file imports it.
  • Dependents: Project::check_file_with_dependencies returns the files a check read. When one of them changes (an edit, open, close, save, or a watched-file event for a file that isn't open), the open files that read it are checked again. The server registers file watchers when the client supports dynamic registration. A change to package.json/tsconfig*.json/safe-ts.json, or a file being created or deleted, rebuilds the Project, because config and resolver results are cached.
  • Quick fixes from fixes.rs (the LocatedDiagnostic::fixes) are returned as quickfix code actions.
  • Hover on a call, via the new Project::call_failures. It shows thrown non-PropagateError failures with their path, PropagateError kinds (rejects when async), a Result return type, and library calls that a feature pack or automatic mode knows about.
  • Severities follow recommended.json: warn there is a warning here. Diagnostics link to their rule page. A checker panic is caught and logged; it doesn't kill the server.

Why lsp-server + lsp-types: these are rust-analyzer's crates, maintained (lsp-server 0.10.0, July 2026). The loop is synchronous over crossbeam channels. The checker is synchronous and fast, so tower-lsp's async/tokio stack would add a runtime for nothing, and debouncing is just recv_deadline. (tower-lsp itself hasn't had a release since 2023; its maintained fork is tower-lsp-server.)

VS Code extension (packages/vscode-safe-ts, plain JS, vscode-languageclient 10.1.2, not published)

  • Where the server comes from (after the rebase onto #12's package layout). For installed users, the CLI ships in the platform packages: each @evobug/safe-ts-native-<platform> carries bin/safe-ts (bin/safe-ts.exe on Windows) next to safe-ts.node, so npm installs exactly the matching one, version-locked to the addon, and @evobug/safe-ts-transform / the oxlint plugin bring it along. @evobug/safe-ts-native itself stays a loader. In the repository, npm run build (scripts/copy-addon.mjs) still copies the CLI to packages/safe-ts-native/bin/ (gitignored), next to the local safe-ts.node.
  • The extension looks for it (server-path.js, no vscode import, so it can be tested outside VS Code) in: safeTs.path; then, in each workspace folder and its parents, node_modules/@evobug/safe-ts-native/bin/ (repository build), node_modules/@evobug/safe-ts-native-<platform>/bin/ (npm), and the same under node_modules/.pnpm/node_modules/ (pnpm); then safe-ts on PATH. The platform id is worked out like the loader's (linux-x64-gnu, win32-x64-msvc, ...).
  • Release pipeline: build-native.mjs builds safe_ts_cli with the addon, stage-native.mjs stages both (--check requires both), build-packages.mjs lists bin in the platform packages' files. copy-addon.mjs copies the CLI only when it was built, because the bench, pages and release workflows build only safe_ts_napi before calling it; the release job's platform packages get their CLI from build-native.mjs. verify-packages.mjs checks the CLI is in the host tarball and executable after install, runs safe-ts check and an LSP initialize/shutdown with it, and checks the extension's lookup resolves to it. With the CLI, the Linux x64 platform package packs to 3.9 MB (the CLI is 5.8 MB unpacked, the addon 4.6 MB).
  • .vue files are matched by pattern, so it works without the Vue extension.
  • There is a SAFE-TS: Restart language server command, and changing safeTs.path restarts the server too.

Docs: a new docs/guide/editor.md (in the sidebar after Adopting), plus updates to quick-start, adoption (setup), vue/setup.md (table and text), the vue/index.md "Where it runs" table, the index.md "Where it runs" section and playground caption, docs/README.md, and the README (layout, editor, quick fixes, Vue, tests).

Which editors were tried

  • VS Code 1.144.1, Linux x64 build, on WSL2 with WSLg. I launched it with @vscode/test-electron (scratch harness in /tmp, not committed) and --disable-extensions, so the Vue extension was not installed. I ran it two ways: as a development extension (--extensionDevelopmentPath), and installed from a VSIX built exactly as the docs describe. Both passed the same script:
    • two no-null problems shown, and "Replace null with undefined" applied as a code action
    • typing the rest of the fix (unsaved) clears them
    • App.vue (languageId plaintext) gets unhandled-failure on its template line
    • main.ts gets the cross-file failure
    • the hover shows load() can fail: throws … load → JSON.parse() at helper.ts:3:10
    • an unsaved edit to helper.ts clears main.ts
  • Not tried: VS Code with the Vue extension, VS Code on Windows or macOS, Neovim, Helix, Zed, JetBrains. The docs say so.

Rebased onto main at 1fe2d1d

Rebased onto main at 1fe2d1d (after #2, #3, #4, #5, #6, #8, #9, #10, #11, #12, #13, #16, #23). What had to change:

  • Conflicts in lib.rs (the export list, with #10's TransformOptions) and the README (#16's bench paragraph and this PR's editor paragraph, both kept).
  • #5: CallSite::callee is now a Callee enum; the hover label uses call.name (the callee as written), as the diagnostics do. #2's resolver filter and #5's call-following apply to the LSP unchanged: analyze is still the only place files are read, so overlays cover the new paths too.
  • #11: the hover called every failure of a project function a throw, and decided whether a PropagateError kind rejects by the callee's async keyword. It now uses the failure chain's throws/rejects and each kind's own rejects (new CallFailure::Rejects { error: Option<_>, path }, Propagates { rejects }). New stdio test hover_tells_rejections_from_throws; it fails on the previous code ("propagates Gone" for a rejection).
  • #12: the binary location above (separate commit).
  • #16: the first CI bench run on the rebased branch failed: bench.yml runs cargo build -p safe_ts_napi && node scripts/copy-addon.mjs, and this PR's copy-addon.mjs required the CLI. Fixed in copy-addon.mjs (last commit). Locally, npm run bench:check on this branch passes all ratios; the two new entries (no-accumulating-copy, stable-shape bench blocks) come from main and have no baseline there either, which the check doesn't fail on.

Tests

After the rebase:

  • cargo nextest run --workspace: 111 passed, 0 failed.
  • crates/safe_ts_cli/tests/lsp.rs (4 tests, including the new hover one) run 10 times in a row: 4/4 each time.
  • npm run test:std: 130 passed, 0 failed. npm run test:vite: passed. npm run typecheck: passed. npm run test:docs: passed (docs typecheck, wasm, VitePress build, no dead links).
  • node scripts/verify-packages.mjs: all checks passed, including the new CLI checks (ok: @evobug/safe-ts-native-linux-x64-gnu/bin/safe-ts runs check and lsp, and the VS Code extension finds it).
  • By hand: the packed tarballs installed with pnpm 12.16.1 (overrides to the local tarballs): the lookup found node_modules/.pnpm/node_modules/@evobug/safe-ts-native-linux-x64-gnu/bin/safe-ts, and it ran. cargo xwin build --release --locked -p safe_ts_napi -p safe_ts_cli --target x86_64-pc-windows-msvc built; staged with stage-native.mjs win32-x64-msvc <dll> <exe>, the Windows safe-ts.exe (run through WSL interop) answered an LSP initialize and shutdown.
  • npm run bench:check: passed locally (29 ratios). node scripts/copy-addon.mjs with and without target/release/safe-ts: copies the CLI, or skips it with a note.

Before the rebase (unchanged tests, on 0e01fb3): the stdio tests drove initialize/diagnostics, a code action applied with an incremental didChange, full-text change and didClose, a .vue file, and the cross-file case (failure path, hover, unsaved fix in the imported buffer, close going back to disk, didChangeWatchedFiles), plus unit tests for UTF-16 positions, URIs and preset severities. The VS Code runs described above were done before the rebase; I didn't repeat them in VS Code after it.

Left out / uncertain

  • Only open files get diagnostics; the whole program is still safe-ts check.
  • safe-ts check's warnings (pack version range, noUncheckedIndexedAccess off) aren't shown.
  • Checks run on the message thread. That's fine at the speeds the checker has here; a very large first check would delay other requests until it's done. $/cancelRequest is ignored.
  • Messages show paths relative to the first workspace folder.
  • In VS Code, the first code-action request after opening a file was sometimes cancelled by VS Code itself (the retry succeeded). I couldn't tell from here whether that is the test harness or the client.
  • vsce can't package from inside the npm workspace, so the documented VSIX steps copy the folder out first.
  • Binary location: the cross-compiled macOS and linux-arm64 CLIs weren't built here (no zig on this machine); only Linux x64 (native) and Windows x64 (cargo-xwin) were. The release workflow builds them the same way as the addon, but it hasn't run with this change. There is no npx safe-ts shim: the CLI is found by path (the extension, or node_modules/@evobug/safe-ts-native-<platform>/bin/safe-ts by hand). Yarn PnP isn't handled; there, set safeTs.path.

🤖 Generated with Claude Code

## What A SAFE-TS language server, `safe-ts lsp`, plus a minimal VS Code extension that starts it. Until now the editor only got SAFE-TS through the oxlint plugin, which can't run on `.vue` files and sees one file at a time, read from disk. **Server** (`crates/safe_ts_cli/src/lsp.rs`, stdio) - Diagnostics for open `.ts`/`.tsx`/`.mts`/`.vue` files. They come on open, on save, and after a change (150 ms debounce, at most 1 s while typing). They use the same checks as `safe-ts check` (`Run::diagnostics_for`), so failures are followed into other modules. - Unsaved buffers count: `Project::set_overlay` makes every read of an open file use the buffer, including when another file imports it. - Dependents: `Project::check_file_with_dependencies` returns the files a check read. When one of them changes (an edit, open, close, save, or a watched-file event for a file that isn't open), the open files that read it are checked again. The server registers file watchers when the client supports dynamic registration. A change to `package.json`/`tsconfig*.json`/`safe-ts.json`, or a file being created or deleted, rebuilds the `Project`, because config and resolver results are cached. - Quick fixes from `fixes.rs` (the `LocatedDiagnostic::fixes`) are returned as `quickfix` code actions. - Hover on a call, via the new `Project::call_failures`. It shows thrown non-`PropagateError` failures with their path, `PropagateError` kinds (rejects when async), a `Result` return type, and library calls that a feature pack or automatic mode knows about. - Severities follow `recommended.json`: `warn` there is a warning here. Diagnostics link to their rule page. A checker panic is caught and logged; it doesn't kill the server. **Why lsp-server + lsp-types:** these are rust-analyzer's crates, maintained (lsp-server 0.10.0, July 2026). The loop is synchronous over crossbeam channels. The checker is synchronous and fast, so tower-lsp's async/tokio stack would add a runtime for nothing, and debouncing is just `recv_deadline`. (tower-lsp itself hasn't had a release since 2023; its maintained fork is tower-lsp-server.) **VS Code extension** (`packages/vscode-safe-ts`, plain JS, `vscode-languageclient` 10.1.2, not published) - **Where the server comes from (after the rebase onto #12's package layout).** For installed users, the CLI ships in the platform packages: each `@evobug/safe-ts-native-<platform>` carries `bin/safe-ts` (`bin/safe-ts.exe` on Windows) next to `safe-ts.node`, so npm installs exactly the matching one, version-locked to the addon, and `@evobug/safe-ts-transform` / the oxlint plugin bring it along. `@evobug/safe-ts-native` itself stays a loader. In the repository, `npm run build` (`scripts/copy-addon.mjs`) still copies the CLI to `packages/safe-ts-native/bin/` (gitignored), next to the local `safe-ts.node`. - The extension looks for it (`server-path.js`, no `vscode` import, so it can be tested outside VS Code) in: `safeTs.path`; then, in each workspace folder and its parents, `node_modules/@evobug/safe-ts-native/bin/` (repository build), `node_modules/@evobug/safe-ts-native-<platform>/bin/` (npm), and the same under `node_modules/.pnpm/node_modules/` (pnpm); then `safe-ts` on `PATH`. The platform id is worked out like the loader's (`linux-x64-gnu`, `win32-x64-msvc`, ...). - Release pipeline: `build-native.mjs` builds `safe_ts_cli` with the addon, `stage-native.mjs` stages both (`--check` requires both), `build-packages.mjs` lists `bin` in the platform packages' `files`. `copy-addon.mjs` copies the CLI only when it was built, because the bench, pages and release workflows build only `safe_ts_napi` before calling it; the release job's platform packages get their CLI from `build-native.mjs`. `verify-packages.mjs` checks the CLI is in the host tarball and executable after install, runs `safe-ts check` and an LSP initialize/shutdown with it, and checks the extension's lookup resolves to it. With the CLI, the Linux x64 platform package packs to 3.9 MB (the CLI is 5.8 MB unpacked, the addon 4.6 MB). - `.vue` files are matched by pattern, so it works without the Vue extension. - There is a `SAFE-TS: Restart language server` command, and changing `safeTs.path` restarts the server too. **Docs:** a new `docs/guide/editor.md` (in the sidebar after Adopting), plus updates to quick-start, adoption (setup), `vue/setup.md` (table and text), the `vue/index.md` "Where it runs" table, the `index.md` "Where it runs" section and playground caption, `docs/README.md`, and the README (layout, editor, quick fixes, Vue, tests). ## Which editors were tried - **VS Code 1.144.1, Linux x64 build, on WSL2 with WSLg.** I launched it with `@vscode/test-electron` (scratch harness in /tmp, not committed) and `--disable-extensions`, so the Vue extension was not installed. I ran it two ways: as a development extension (`--extensionDevelopmentPath`), and installed from a VSIX built exactly as the docs describe. Both passed the same script: - two `no-null` problems shown, and "Replace null with undefined" applied as a code action - typing the rest of the fix (unsaved) clears them - `App.vue` (languageId `plaintext`) gets `unhandled-failure` on its template line - `main.ts` gets the cross-file failure - the hover shows `load() can fail: throws … load → JSON.parse() at helper.ts:3:10` - an unsaved edit to `helper.ts` clears `main.ts` - Not tried: VS Code with the Vue extension, VS Code on Windows or macOS, Neovim, Helix, Zed, JetBrains. The docs say so. ## Rebased onto main at 1fe2d1d Rebased onto `main` at 1fe2d1d (after #2, #3, #4, #5, #6, #8, #9, #10, #11, #12, #13, #16, #23). What had to change: - Conflicts in `lib.rs` (the export list, with #10's `TransformOptions`) and the README (#16's bench paragraph and this PR's editor paragraph, both kept). - #5: `CallSite::callee` is now a `Callee` enum; the hover label uses `call.name` (the callee as written), as the diagnostics do. #2's resolver filter and #5's call-following apply to the LSP unchanged: `analyze` is still the only place files are read, so overlays cover the new paths too. - #11: the hover called every failure of a project function a throw, and decided whether a `PropagateError` kind rejects by the callee's `async` keyword. It now uses the failure chain's throws/rejects and each kind's own `rejects` (new `CallFailure::Rejects { error: Option<_>, path }`, `Propagates { rejects }`). New stdio test `hover_tells_rejections_from_throws`; it fails on the previous code ("propagates `Gone`" for a rejection). - #12: the binary location above (separate commit). - #16: the first CI bench run on the rebased branch failed: `bench.yml` runs `cargo build -p safe_ts_napi && node scripts/copy-addon.mjs`, and this PR's `copy-addon.mjs` required the CLI. Fixed in `copy-addon.mjs` (last commit). Locally, `npm run bench:check` on this branch passes all ratios; the two `new` entries (no-accumulating-copy, stable-shape bench blocks) come from main and have no baseline there either, which the check doesn't fail on. ## Tests After the rebase: - `cargo nextest run --workspace`: 111 passed, 0 failed. - `crates/safe_ts_cli/tests/lsp.rs` (4 tests, including the new hover one) run 10 times in a row: 4/4 each time. - `npm run test:std`: 130 passed, 0 failed. `npm run test:vite`: passed. `npm run typecheck`: passed. `npm run test:docs`: passed (docs typecheck, wasm, VitePress build, no dead links). - `node scripts/verify-packages.mjs`: all checks passed, including the new CLI checks (`ok: @evobug/safe-ts-native-linux-x64-gnu/bin/safe-ts runs check and lsp, and the VS Code extension finds it`). - By hand: the packed tarballs installed with pnpm 12.16.1 (overrides to the local tarballs): the lookup found `node_modules/.pnpm/node_modules/@evobug/safe-ts-native-linux-x64-gnu/bin/safe-ts`, and it ran. `cargo xwin build --release --locked -p safe_ts_napi -p safe_ts_cli --target x86_64-pc-windows-msvc` built; staged with `stage-native.mjs win32-x64-msvc <dll> <exe>`, the Windows `safe-ts.exe` (run through WSL interop) answered an LSP initialize and shutdown. - `npm run bench:check`: passed locally (29 ratios). `node scripts/copy-addon.mjs` with and without `target/release/safe-ts`: copies the CLI, or skips it with a note. Before the rebase (unchanged tests, on 0e01fb3): the stdio tests drove initialize/diagnostics, a code action applied with an incremental `didChange`, full-text change and `didClose`, a `.vue` file, and the cross-file case (failure path, hover, unsaved fix in the imported buffer, close going back to disk, `didChangeWatchedFiles`), plus unit tests for UTF-16 positions, URIs and preset severities. The VS Code runs described above were done before the rebase; I didn't repeat them in VS Code after it. ## Left out / uncertain - Only open files get diagnostics; the whole program is still `safe-ts check`. - `safe-ts check`'s warnings (pack version range, `noUncheckedIndexedAccess` off) aren't shown. - Checks run on the message thread. That's fine at the speeds the checker has here; a very large first check would delay other requests until it's done. `$/cancelRequest` is ignored. - Messages show paths relative to the first workspace folder. - In VS Code, the first code-action request after opening a file was sometimes cancelled by VS Code itself (the retry succeeded). I couldn't tell from here whether that is the test harness or the client. - vsce can't package from inside the npm workspace, so the documented VSIX steps copy the folder out first. - Binary location: the cross-compiled macOS and linux-arm64 CLIs weren't built here (no zig on this machine); only Linux x64 (native) and Windows x64 (cargo-xwin) were. The release workflow builds them the same way as the addon, but it hasn't run with this change. There is no `npx safe-ts` shim: the CLI is found by path (the extension, or `node_modules/@evobug/safe-ts-native-<platform>/bin/safe-ts` by hand). Yarn PnP isn't handled; there, set `safeTs.path`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sionzee force-pushed lsp-server from ef8861ff9f to 57b61533cb
Some checks failed
Benchmarks / bench (pull_request) Failing after 1m52s
2026-11-03 23:26:35 +01:00
Compare
copy-addon: copy the CLI only when it was built
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m29s
515b4172b0
The bench and pages workflows build only safe_ts_napi before `node scripts/copy-addon.mjs`,
and since this PR it required target/release/safe-ts, so the bench job failed. The CLI is
copied when it exists, and a line says when it isn't. `npm run build` builds both, as before.

The release workflow no longer needs to build the host CLI: the platform packages get theirs
from build-native.mjs, and stage-native.mjs --check still requires them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge main into lsp-server
Some checks failed
Benchmarks / bench (pull_request) Failing after 4s
d5476d7b4e
Resolve conflicts with the PRs merged before this one: both sides kept
(rule lists, std exports, package checks, CLI usage, README).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch lsp-server 2026-11-04 21:29:48 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!17
No description provided.