Compare commits

..

15 Commits

Author SHA1 Message Date
2da4103cd7 fix: report unavailable LSP servers explicitly 2026-05-31 20:07:17 -04:00
8cfe604de7 fix(daemon): clear diagnostics and reload TypeScript projects on watcher events
Evicting the entire entry on derived watcher events was overly aggressive.
Instead, clear cached diagnostics and send a reloadProjects command to the
TypeScript language server so it picks up workspace changes without losing
state.
2026-05-24 11:57:01 -04:00
071c87d3c1 fix(watcher): derive fallback file patterns 2026-05-20 06:53:35 -04:00
3f3cb4cdbf fix(watcher): close deleted opened documents 2026-05-20 06:38:21 -04:00
14749a6449 fix(watcher): close deleted open documents 2026-05-20 00:22:30 -04:00
62fc80c70f fix(watcher): cap startup wait 2026-05-20 00:09:07 -04:00
b7e421483d fix(watcher): stabilize daemon readiness and tests 2026-05-19 23:58:18 -04:00
0aa44bedc4 fix(watcher): coalesce edge cases, drain on unregister, expose ready()
Addresses review feedback on 7787626:

1. Deleted->Created at the same path is a file replacement, not a no-op.
   Previous coalescing dropped both Created->Deleted and Deleted->Created;
   the latter left the server with no signal to re-read replaced content.
   Now: Deleted->Created collapses to Changed, Created->Changed keeps
   Created (server didn't know the file at all). Extracted coalesce() so
   the matrix is reviewable in one place.

2. setPatterns([]) (server unregistered all watchers) stopped chokidar
   but left pending events + timers intact, so a queued batch could
   still fire after the server stopped caring. Now drains via
   cancelPending() before stopping chokidar.

3. Added ready() returning a promise resolved by chokidar's initial-scan
   'ready' event. Production daemon doesn't need to await it (LSP
   handshake gives chokidar ample wall-time), but tests now use it
   instead of fixed 200ms sleeps - deflakes the suite on slower
   filesystems and addresses the (narrow) startup race where a file
   created during chokidar's initial crawl could be missed.

4. Unit tests replace 11 hardcoded sleeps with watcher.ready(), and add
   coverage for the two coalesce fixes plus the unregister-drains case.
2026-05-19 23:51:32 -04:00
77876264ee feat(watcher): forward FS events as workspace/didChangeWatchedFiles
LSP servers maintain their own workspace index built at initialize time
and rely on the client to push file-system events. Previously the daemon
only synced the single file being queried, so externally created/changed
files (codegen, build scripts, git checkout, the agent's own writes from
the perspective of other open files) left the server's index stale until
manual /lsp-destroy.

Each ClientEntry now lazily owns a WorkspaceWatcher (chokidar + picomatch)
that translates FS events into workspace/didChangeWatchedFiles batches.
Patterns come from the server via client/registerCapability (no
speculative watching). Ignores layer a tiny baseline (.git, .DS_Store)
over the repo's root .gitignore, with a fallback list for non-git
workspaces. Events debounce 50ms quiet / 500ms max wait.

Notable: gopls registers absolute-path globs (/abs/root/**/*.go) rather
than relative ones, so compileWatchers() matches each event against both
relative and absolute path forms. Caught by the integration test; unit
regression test added.

Rollback: PI_LSP_DISABLE_WATCHERS=1 disables all watcher creation.

- src/client.ts: honor register/unregisterCapability for
  workspace/didChangeWatchedFiles; advertise dynamicRegistration;
  expose getFileWatchers/onWatchersChanged/sendNotification
- src/watcher.ts: new WorkspaceWatcher with layered ignores,
  debounce+batch, Created+Deleted coalescing, dual-form glob matching
- src/daemon.ts: per-entry watcher lifecycle, PI_LSP_DISABLE_WATCHERS,
  LSP_DEBUG-gated pattern/event logging
- test/unit/watcher.test.ts: 11 tests against real chokidar + temp dir
- test/integration/watcher-gopls.test.ts: end-to-end against gopls
- AGENTS.md: new "Workspace File Watching" section
- flake.nix: add go (required by gopls integration test)
2026-05-19 23:43:32 -04:00
e143e05758 feat(servers): split vscode-html-language-server and add css, json, bash, sql servers
- Split vscode-html-language-server into separate servers for HTML, CSS,
  and JSON with proper language IDs and file extensions
- Added bash-language-server for shell scripts (.sh, .bash)
- Added sqls for SQL files
- Added timeout wrapper to auto-check diagnostics to prevent blocking pi
2026-05-08 18:51:12 -04:00
46e3cc4ccd feat(config): add per-repo .pi-lsp.json server overrides
Users can now drop a .pi-lsp.json at any ancestor of their working
files to add new LSP servers, override built-in ones, or disable
servers entirely. The nearest config (walking upward) wins.

- New src/config.ts: walks upward for .pi-lsp.json, parses, and
  merges with the built-in registry. Cached per config-file path
  with mtime invalidation. Falls back to built-ins on parse error.
- Merge rules: matching id shallow-merges (user wins); new id
  appends (must include match/command/args/rootMarkers); `disable`
  filters at the end.
- src/root.ts: pickServer() now resolves servers via the per-repo
  registry. Adds findServerById(filePath, id) and re-exports
  getServersForPath() for callers.
- src/daemon.ts: getOrCreateEntry() resolves serverId against the
  filePath's config so spawned servers reflect repo overrides.
- index.ts and cli.ts: replace direct `servers` imports with
  path-aware getServersForPath() lookups.
- Tests: 9 new unit tests covering merge semantics, walk-up
  discovery, mtime invalidation, and graceful fallback.
- Docs: README "Per-Repo Config" section + AGENTS.md updates.
2026-05-07 22:43:41 -04:00
0b23e203f4 build(flake): add gopls and pyright for tests 2026-05-07 22:43:28 -04:00
9e5a0677c8 feat: add svelteserver to LSP server registry 2026-05-07 21:19:46 -04:00
99ce79ac88 fix(lsp): support server workspace configuration 2026-05-05 23:44:21 -04:00
81ab984a86 chore: add oxlint 2026-05-04 07:45:50 -04:00
20 changed files with 1728 additions and 78 deletions

View File

@@ -41,6 +41,40 @@ The daemon tracks opened files per-entry in a `Map<uri, mtimeMs>`. On each reque
A per-entry `serializer` promise chain prevents concurrent syncs from racing. A per-entry `serializer` promise chain prevents concurrent syncs from racing.
### Workspace File Watching
Each `ClientEntry` lazily owns a `WorkspaceWatcher` (`src/watcher.ts`,
chokidar + picomatch) that translates filesystem events into
`workspace/didChangeWatchedFiles` notifications. This keeps the server's
workspace index fresh when files are created/changed/deleted **outside** of
LSP tool calls (build scripts, codegen, `git checkout`, the agent's own
file writes).
Non-obvious bits:
- **Patterns come from the server.** We honor `client/registerCapability`
for `workspace/didChangeWatchedFiles` and store the registrations on the
`LspClient`. **Don't re-stub those handlers**; they look harmless but
break the entire feature. If a server doesn't register, we don't watch.
- **Servers send mixed pattern forms.** Gopls registers absolute-path
globs (`/abs/root/**/*.go`); others send relative (`**/*.ts`) or
`RelativePattern` objects. `compileWatchers()` tries both relative and
absolute matching against each event so we accept all forms.
- **Ignore layering.** Always-ignore baseline (`.git/`, `.DS_Store`) +
root `.gitignore` parsed via the `ignore` package + a small fallback
for non-git workspaces. Nested gitignores aren't supported yet.
- **Startup readiness.** The daemon waits for chokidar's initial scan, capped
at 5s, so first requests don't hang indefinitely on huge workspaces.
- **Debounce.** 50ms quiet period, capped at 500ms max wait so sustained
event streams (branch switches) still flush in bounded time.
- **Watcher and mtime-sync coexist.** When the agent edits a file we'll
emit `didChangeWatchedFiles` *and* the next request's `syncFile` will
send a `didChange`. Servers treat the two as orthogonal (workspace
index vs. editor buffer) and dedupe internally. This matches VS Code.
- **Rollback.** `PI_LSP_DISABLE_WATCHERS=1` short-circuits all watcher
creation — if something goes wrong in a real workspace, this restores
the prior "only the queried file is synced" behavior.
### Extension vs Daemon Responsibilities ### Extension vs Daemon Responsibilities
| Concern | Where | | Concern | Where |
@@ -55,22 +89,43 @@ A per-entry `serializer` promise chain prevents concurrent syncs from racing.
``` ```
index.ts — Extension entry point (tools, commands, auto-check flag) index.ts — Extension entry point (tools, commands, auto-check flag)
server.ts — LSP server registry (gopls, typescript-language-server, pyright) server.ts — Built-in LSP server registry (gopls, typescript-language-server, pyright, ...)
cli.ts — CLI for testing/debugging (daemon-aware or --no-daemon) cli.ts — CLI for testing/debugging (daemon-aware or --no-daemon)
daemon.ts — Entrypoint that starts the daemon process daemon.ts — Entrypoint that starts the daemon process
src/ src/
client.ts — LspClient: spawns a language server, JSON-RPC handshake, file sync client.ts — LspClient: spawns a language server, JSON-RPC handshake, file sync, file-watcher registrations
watcher.ts — WorkspaceWatcher: chokidar + picomatch → workspace/didChangeWatchedFiles batches
commands.ts — CLI command dispatcher (maps command names → LSP methods) commands.ts — CLI command dispatcher (maps command names → LSP methods)
config.ts — Per-repo `.pi-lsp.json` loader: walk-up + merge with built-ins, mtime cache
daemonClient.ts — High-level helpers (daemonRequest, daemonDiagnostics, etc.) daemonClient.ts — High-level helpers (daemonRequest, daemonDiagnostics, etc.)
daemonProtocol.ts — Shared types, socket path, NDJSON send/receive, autospawn logic daemonProtocol.ts — Shared types, socket path, NDJSON send/receive, autospawn logic
root.ts — pickServer(), findRoot(), URI/path conversion root.ts — pickServer(), findServerById(), getServersForPath(), findRoot(), URI/path conversion
types.ts — ServerConfig interface, LspCommand union types.ts — ServerConfig interface, LspCommand union
``` ```
## Adding a Server ### Per-Repo Config (`.pi-lsp.json`)
Edit `server.ts`. Add an entry to the `servers` array: Users can add/override/disable servers without editing `server.ts`. `src/config.ts`
walks upward from a given path to find `.pi-lsp.json`, parses it, and merges
with the built-in `servers` list:
- New `id` → appended (must supply `match`, `command`, `args`, `rootMarkers`).
- Existing `id` → shallow-merged over the built-in (user fields win).
- `disable: []` → filtered out at the end.
Results are cached per config path, invalidated by mtime. `getServersForPath(p)`
is the **single entry point** — don't import the raw `servers` array from
`server.ts` outside `src/config.ts`. The daemon resolves servers at
`getOrCreateEntry()` time via `findServerById(filePath, id)`, so spawned
servers reflect the config of the file being acted on. **Already-running**
entries don't see config changes; users must `/lsp-destroy` to respawn.
## Adding a Server (Built-In)
For servers shipped with pi-lsp, edit `server.ts`. (For per-repo additions,
users should drop a `.pi-lsp.json` at the repo root — see README.) Add an entry
to the `servers` array:
```typescript ```typescript
{ {

View File

@@ -123,6 +123,44 @@ Edit `server.ts`:
} }
``` ```
## Per-Repo Config (`.pi-lsp.json`)
Drop a `.pi-lsp.json` at any ancestor of your working files to add or override
servers for that repo. The nearest config (walking upward) wins.
```json
{
"servers": [
{
"id": "rust-analyzer",
"match": ["rs"],
"command": "rust-analyzer",
"args": [],
"rootMarkers": ["Cargo.toml"],
"languageId": "rust"
},
{ "id": "gopls", "args": ["-remote=auto", "-vv"] }
],
"disable": ["oxlint"]
}
```
**Merge rules:**
- Entry with a built-in `id` → fields shallow-merge over the built-in (user wins).
- Entry with a new `id` → appended; must include `match`, `command`, `args`, `rootMarkers`.
- `disable` → filters out matching ids (built-in or user-defined).
**Reloading after edits:** Config is re-read on mtime change, so new lookups
pick up changes automatically. However, **already-running** language servers in
the daemon keep their original spawn args. If you change `command`, `args`, or
`rootMarkers`, run `/lsp-destroy` (or `pi-lsp daemon stop`) so they respawn
with the new config.
**Security note:** `.pi-lsp.json` controls what binary pi-lsp spawns. Treat it
like `.vscode/settings.json` — don't accept untrusted configs from arbitrary
repos.
## Adding A Command ## Adding A Command
1. Add to the `LspCommand` union in `src/types.ts`. 1. Add to the `LspCommand` union in `src/types.ts`.

8
cli.ts
View File

@@ -2,8 +2,7 @@
import * as path from "node:path"; import * as path from "node:path";
import { startClientForFile } from "./src/client.ts"; import { startClientForFile } from "./src/client.ts";
import { isLspCommand, listCommands, runCommand } from "./src/commands.ts"; import { isLspCommand, listCommands, runCommand } from "./src/commands.ts";
import { pickServer, isServerAvailable } from "./src/root.ts"; import { pickServer, isServerAvailable, getServersForPath } from "./src/root.ts";
import { servers } from "./server.ts";
import { import {
daemonDiagnostics, daemonDiagnostics,
daemonRequest, daemonRequest,
@@ -105,9 +104,10 @@ async function runViaDaemon(
const filePath = path.resolve(fileArg); const filePath = path.resolve(fileArg);
let result: unknown; let result: unknown;
if (cmdArg === "diagnostics") { if (cmdArg === "diagnostics") {
// Pick All Available Servers For Diagnostics // Pick All Available Servers For Diagnostics - Resolves against any
// `.pi-lsp.json` reachable from the file so per-repo overrides apply.
const ext = path.extname(filePath).replace(/^\./, ""); const ext = path.extname(filePath).replace(/^\./, "");
const serverIds = servers const serverIds = getServersForPath(filePath)
.filter((s) => s.match.includes(ext) && isServerAvailable(s)) .filter((s) => s.match.includes(ext) && isServerAvailable(s))
.map((s) => s.id); .map((s) => s.id);
result = await daemonDiagnostics(filePath, serverIds); result = await daemonDiagnostics(filePath, serverIds);

View File

@@ -20,8 +20,14 @@
{ {
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
packages = with pkgs; [ packages = with pkgs; [
oxlint
nodejs_22 nodejs_22
typescript-language-server typescript-language-server
# Tests
go
gopls
pyright
]; ];
}; };
} }

148
index.ts
View File

@@ -12,15 +12,34 @@ import {
daemonRequest, daemonRequest,
daemonStatus, daemonStatus,
} from "./src/daemonClient.ts"; } from "./src/daemonClient.ts";
import { pickServer, isServerAvailable } from "./src/root.ts"; import { pickServer, isServerAvailable, getServersForPath } from "./src/root.ts";
import { servers } from "./server.ts";
import { import {
ServerNotFoundError, ServerNotFoundError,
UnsupportedExtensionError, UnsupportedExtensionError,
} from "./src/types.ts"; } from "./src/types.ts";
type LspUnavailable = { piLspUnavailable: true; message: string };
function lspUnavailable(message: string): LspUnavailable {
return { piLspUnavailable: true, message };
}
function isLspUnavailable(result: unknown): result is LspUnavailable {
return Boolean(
result &&
typeof result === "object" &&
"piLspUnavailable" in result &&
(result as { piLspUnavailable?: unknown }).piLspUnavailable === true,
);
}
function formatUnavailable(result: LspUnavailable): string {
return `(LSP unavailable: ${result.message})`;
}
// Format Hover - Turn an LSP hover response into readable text. // Format Hover - Turn an LSP hover response into readable text.
function formatHover(result: unknown): string { function formatHover(result: unknown): string {
if (isLspUnavailable(result)) return formatUnavailable(result);
if (!result || typeof result !== "object") return "(no hover info)"; if (!result || typeof result !== "object") return "(no hover info)";
const hover = result as { contents?: unknown }; const hover = result as { contents?: unknown };
if (!hover.contents) return "(empty)"; if (!hover.contents) return "(empty)";
@@ -50,6 +69,7 @@ function formatHover(result: unknown): string {
// Format Definition - Turn definition locations into readable text. // Format Definition - Turn definition locations into readable text.
function formatDefinition(result: unknown): string { function formatDefinition(result: unknown): string {
if (isLspUnavailable(result)) return formatUnavailable(result);
if (!result) return "(no definition found)"; if (!result) return "(no definition found)";
const locations = Array.isArray(result) ? result : [result]; const locations = Array.isArray(result) ? result : [result];
if (locations.length === 0) return "(no definition found)"; if (locations.length === 0) return "(no definition found)";
@@ -65,6 +85,7 @@ function formatDefinition(result: unknown): string {
// Format References - Turn reference locations into readable text. // Format References - Turn reference locations into readable text.
function formatReferences(result: unknown): string { function formatReferences(result: unknown): string {
if (isLspUnavailable(result)) return formatUnavailable(result);
if (!result || !Array.isArray(result)) return "(no references found)"; if (!result || !Array.isArray(result)) return "(no references found)";
if (result.length === 0) return "(no references found)"; if (result.length === 0) return "(no references found)";
@@ -86,6 +107,7 @@ function formatReferences(result: unknown): string {
// Format Completions - Turn completion items into readable text. // Format Completions - Turn completion items into readable text.
function formatCompletions(result: unknown): string { function formatCompletions(result: unknown): string {
if (isLspUnavailable(result)) return formatUnavailable(result);
if (!result) return "(no completions)"; if (!result) return "(no completions)";
// Resolve to CompletionItem[] if it's a CompletionList // Resolve to CompletionItem[] if it's a CompletionList
@@ -151,6 +173,7 @@ function formatCompletions(result: unknown): string {
// Format Document Symbols - Turn symbols into a tree outline. // Format Document Symbols - Turn symbols into a tree outline.
function formatDocumentSymbols(result: unknown): string { function formatDocumentSymbols(result: unknown): string {
if (isLspUnavailable(result)) return formatUnavailable(result);
if (!result || !Array.isArray(result)) return "(no symbols)"; if (!result || !Array.isArray(result)) return "(no symbols)";
const symbols = result as any[]; const symbols = result as any[];
if (symbols.length === 0) return "(no symbols)"; if (symbols.length === 0) return "(no symbols)";
@@ -236,6 +259,7 @@ function formatServerDiagnostics(diags: any[], limit: number): string {
// { [serverId]: { uri, diagnostics[] } }. Single-server results omit the // { [serverId]: { uri, diagnostics[] } }. Single-server results omit the
// header to avoid noise. // header to avoid noise.
function formatDiagnostics(result: unknown, limit = 20): string { function formatDiagnostics(result: unknown, limit = 20): string {
if (isLspUnavailable(result)) return formatUnavailable(result);
if (!result || typeof result !== "object") return "(no diagnostics)"; if (!result || typeof result !== "object") return "(no diagnostics)";
const grouped = result as Record<string, any>; const grouped = result as Record<string, any>;
@@ -276,6 +300,34 @@ function isExpectedError(error: unknown): boolean {
); );
} }
function unavailableForFile(filePath: string, includeDiagnosticsOnly = false): LspUnavailable {
const ext = path.extname(filePath).replace(/^\./, "");
const label = ext ? `.${ext}` : "files without an extension";
const matches = getServersForPath(filePath).filter((server) =>
server.match.includes(ext) && (includeDiagnosticsOnly || !server.diagnosticsOnly)
);
if (matches.length === 0) {
return lspUnavailable(`no LSP server is registered for ${label}`);
}
const disabled = matches.filter((server) => disabledServers.has(server.id));
if (disabled.length === matches.length) {
return lspUnavailable(
`matching LSP server(s) are disabled: ${disabled.map((s) => s.id).join(", ")}`,
);
}
const unavailable = matches.filter((server) => !isServerAvailable(server));
if (unavailable.length > 0) {
return lspUnavailable(
`matching LSP server(s) are not on PATH: ${unavailable.map((s) => s.command).join(", ")}`,
);
}
return lspUnavailable(`no applicable LSP server is available for ${label}`);
}
// Disabled Servers - In-memory set of disabled server IDs. Scoped to this // Disabled Servers - In-memory set of disabled server IDs. Scoped to this
// extension process; the shared daemon is never mutated by disable/enable. // extension process; the shared daemon is never mutated by disable/enable.
const lspToolNames = [ const lspToolNames = [
@@ -302,14 +354,12 @@ async function runLsp(
// touching the daemon so other pi instances sharing it are unaffected. // touching the daemon so other pi instances sharing it are unaffected.
const server = pickServer(filePath); const server = pickServer(filePath);
if (disabledServers.has(server.id)) { if (disabledServers.has(server.id)) {
throw new Error( return unavailableForFile(filePath);
`LSP server "${server.id}" is disabled. Use /lsp-enable ${server.id} to re-enable.`,
);
} }
return await daemonRequest(filePath, server.id, method, params); return await daemonRequest(filePath, server.id, method, params);
} catch (error) { } catch (error) {
if (isExpectedError(error)) { if (isExpectedError(error)) {
return undefined; return unavailableForFile(filePath);
} }
// Daemon-wrapped errors (plain Error with expected message) are also // Daemon-wrapped errors (plain Error with expected message) are also
// expected — the daemon catches pickServer() throws and returns them // expected — the daemon catches pickServer() throws and returns them
@@ -319,38 +369,51 @@ async function runLsp(
(error.message.includes("No LSP server registered") || (error.message.includes("No LSP server registered") ||
error.message.includes("not found on PATH")) error.message.includes("not found on PATH"))
) { ) {
return undefined; return unavailableForFile(filePath);
} }
throw error; throw error;
} }
} }
// Pick Diagnostic Servers - Returns all available, non-disabled servers // Pick Diagnostic Servers - Returns all available, non-disabled servers
// matching the file's extension. Used for fan-out diagnostics. // matching the file's extension. Resolves the per-repo config from the
// file's directory so user-defined servers participate in fan-out.
function pickDiagnosticServers(filePath: string): string[] { function pickDiagnosticServers(filePath: string): string[] {
const ext = path.extname(filePath).replace(/^\./, ""); const ext = path.extname(filePath).replace(/^\./, "");
return servers return getServersForPath(filePath)
.filter((s) => s.match.includes(ext) && isServerAvailable(s) && !disabledServers.has(s.id)) .filter((s) => s.match.includes(ext) && isServerAvailable(s) && !disabledServers.has(s.id))
.map((s) => s.id); .map((s) => s.id);
} }
// Timeout Wrapper - Rejects a promise after the given number of milliseconds.
// Used to prevent async hooks from blocking pi indefinitely.
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`Timeout after ${ms}ms`)), ms);
promise.then(
(value) => { clearTimeout(timer); resolve(value); },
(reason) => { clearTimeout(timer); reject(reason); },
);
});
}
// Run LSP Diagnostics - Fans out to all matching servers in a single // Run LSP Diagnostics - Fans out to all matching servers in a single
// daemon call. Returns the grouped result map or undefined if no servers. // daemon call. Returns an explicit unavailable result if no server applies.
async function runDiagnostics(filePath: string): Promise<unknown> { async function runDiagnostics(filePath: string): Promise<unknown> {
try { try {
const serverIds = pickDiagnosticServers(filePath); const serverIds = pickDiagnosticServers(filePath);
if (serverIds.length === 0) return undefined; if (serverIds.length === 0) return unavailableForFile(filePath, true);
return await daemonDiagnostics(filePath, serverIds, 1500); return await daemonDiagnostics(filePath, serverIds, 1500);
} catch (error) { } catch (error) {
if (isExpectedError(error)) { if (isExpectedError(error)) {
return undefined; return unavailableForFile(filePath, true);
} }
if ( if (
error instanceof Error && error instanceof Error &&
(error.message.includes("No LSP server registered") || (error.message.includes("No LSP server registered") ||
error.message.includes("not found on PATH")) error.message.includes("not found on PATH"))
) { ) {
return undefined; return unavailableForFile(filePath, true);
} }
throw error; throw error;
} }
@@ -541,7 +604,9 @@ export default function (pi: ExtensionAPI) {
if (!pi.getFlag("lsp-auto-check")) return; if (!pi.getFlag("lsp-auto-check")) return;
// Skip If All Disabled - No LSP server is available for this instance. // Skip If All Disabled - No LSP server is available for this instance.
if (servers.every((s) => disabledServers.has(s.id))) return; // Resolve against ctx.cwd so user-defined servers count toward this check.
const cwdServers = getServersForPath(ctx.cwd);
if (cwdServers.every((s) => disabledServers.has(s.id))) return;
// Edit & Write Only // Edit & Write Only
if (!["edit", "write"].includes(event.toolName)) return; if (!["edit", "write"].includes(event.toolName)) return;
@@ -555,8 +620,10 @@ export default function (pi: ExtensionAPI) {
const absolutePath = path.resolve(ctx.cwd, filePath); const absolutePath = path.resolve(ctx.cwd, filePath);
try { try {
// Run LSP diagnostics // Run LSP diagnostics with timeout - Prevent the auto-check hook from
const result = await runDiagnostics(absolutePath); // blocking pi if the daemon or LSP server is slow or unresponsive.
const result = await withTimeout(runDiagnostics(absolutePath), 3000);
if (isLspUnavailable(result)) return;
const formatted = formatDiagnostics(result, 10); const formatted = formatDiagnostics(result, 10);
// Only send a message if there are actual diagnostics // Only send a message if there are actual diagnostics
@@ -619,9 +686,16 @@ export default function (pi: ExtensionAPI) {
// --- Server Control Commands --- // --- Server Control Commands ---
// Resolve Active Servers - Per-cwd helper so commands see the same
// per-repo overrides the LLM does. We accept an optional cwd because some
// closures (e.g. completions) don't get a context.
const resolveServers = (cwd?: string) => getServersForPath(cwd ?? process.cwd());
// Shared Argument Completions - Suggests registered server IDs plus "all". // Shared Argument Completions - Suggests registered server IDs plus "all".
// Uses process.cwd() since completion handlers don't receive a context;
// close enough for argument hints.
const serverCompletions = (prefix: string) => { const serverCompletions = (prefix: string) => {
const ids = [...servers.map((s) => s.id), "all"].filter((id) => const ids = [...resolveServers().map((s) => s.id), "all"].filter((id) =>
id.startsWith(prefix), id.startsWith(prefix),
); );
return ids.length > 0 ? ids.map((id) => ({ value: id, label: id })) : null; return ids.length > 0 ? ids.map((id) => ({ value: id, label: id })) : null;
@@ -629,16 +703,17 @@ export default function (pi: ExtensionAPI) {
// Parse Server IDs - Validates args against registered servers. Bare/empty // Parse Server IDs - Validates args against registered servers. Bare/empty
// or "all" returns every server ID. Throws on unknown names. // or "all" returns every server ID. Throws on unknown names.
function parseServerIds(args: string | undefined): string[] { function parseServerIds(args: string | undefined, cwd: string): string[] {
if (!args || !args.trim()) return servers.map((s) => s.id); const list = resolveServers(cwd);
if (!args || !args.trim()) return list.map((s) => s.id);
const ids = args.trim().split(/\s+/); const ids = args.trim().split(/\s+/);
if (ids.includes("all")) return servers.map((s) => s.id); if (ids.includes("all")) return list.map((s) => s.id);
const invalid = ids.filter((id) => !servers.some((s) => s.id === id)); const invalid = ids.filter((id) => !list.some((s) => s.id === id));
if (invalid.length > 0) { if (invalid.length > 0) {
throw new Error( throw new Error(
`Unknown server(s): ${invalid.join( `Unknown server(s): ${invalid.join(
", ", ", ",
)}. Available: ${servers.map((s) => s.id).join(", ")}`, )}. Available: ${list.map((s) => s.id).join(", ")}`,
); );
} }
return ids; return ids;
@@ -647,9 +722,10 @@ export default function (pi: ExtensionAPI) {
// Update Tool Visibility - When all servers are disabled, remove LSP tools // Update Tool Visibility - When all servers are disabled, remove LSP tools
// from the active set so the LLM won't attempt them. When any is enabled, // from the active set so the LLM won't attempt them. When any is enabled,
// restore them. Captures current active tools at toggle time. // restore them. Captures current active tools at toggle time.
function updateToolVisibility(): void { function updateToolVisibility(cwd: string): void {
const list = resolveServers(cwd);
const current = pi.getActiveTools(); const current = pi.getActiveTools();
if (servers.every((s) => disabledServers.has(s.id))) { if (list.every((s) => disabledServers.has(s.id))) {
// All disabled — strip LSP tools // All disabled — strip LSP tools
pi.setActiveTools(current.filter((name) => !lspToolNames.includes(name))); pi.setActiveTools(current.filter((name) => !lspToolNames.includes(name)));
} else { } else {
@@ -699,13 +775,13 @@ export default function (pi: ExtensionAPI) {
getArgumentCompletions: serverCompletions, getArgumentCompletions: serverCompletions,
handler: async (args, ctx) => { handler: async (args, ctx) => {
try { try {
const ids = parseServerIds(args); const ids = parseServerIds(args, ctx.cwd);
for (const id of ids) { for (const id of ids) {
disabledServers.add(id); disabledServers.add(id);
} }
updateToolVisibility(); updateToolVisibility(ctx.cwd);
const label = const total = resolveServers(ctx.cwd).length;
ids.length === servers.length ? "all servers" : ids.join(", "); const label = ids.length === total ? "all servers" : ids.join(", ");
ctx.ui.notify(`Disabled: ${label}`, "info"); ctx.ui.notify(`Disabled: ${label}`, "info");
} catch (error) { } catch (error) {
const msg = const msg =
@@ -725,13 +801,13 @@ export default function (pi: ExtensionAPI) {
getArgumentCompletions: serverCompletions, getArgumentCompletions: serverCompletions,
handler: async (args, ctx) => { handler: async (args, ctx) => {
try { try {
const ids = parseServerIds(args); const ids = parseServerIds(args, ctx.cwd);
for (const id of ids) { for (const id of ids) {
disabledServers.delete(id); disabledServers.delete(id);
} }
updateToolVisibility(); updateToolVisibility(ctx.cwd);
const label = const total = resolveServers(ctx.cwd).length;
ids.length === servers.length ? "all servers" : ids.join(", "); const label = ids.length === total ? "all servers" : ids.join(", ");
ctx.ui.notify(`Enabled: ${label}`, "info"); ctx.ui.notify(`Enabled: ${label}`, "info");
} catch (error) { } catch (error) {
const msg = const msg =
@@ -751,16 +827,16 @@ export default function (pi: ExtensionAPI) {
getArgumentCompletions: serverCompletions, getArgumentCompletions: serverCompletions,
handler: async (args, ctx) => { handler: async (args, ctx) => {
try { try {
const ids = parseServerIds(args); const ids = parseServerIds(args, ctx.cwd);
if (ids.length === servers.length) { const total = resolveServers(ctx.cwd).length;
if (ids.length === total) {
await daemonDestroyServer(); await daemonDestroyServer();
} else { } else {
for (const id of ids) { for (const id of ids) {
await daemonDestroyServer(id); await daemonDestroyServer(id);
} }
} }
const label = const label = ids.length === total ? "all servers" : ids.join(", ");
ids.length === servers.length ? "all servers" : ids.join(", ");
ctx.ui.notify(`Destroyed: ${label}`, "info"); ctx.ui.notify(`Destroyed: ${label}`, "info");
} catch (error) { } catch (error) {
const msg = const msg =

52
package-lock.json generated
View File

@@ -8,6 +8,9 @@
"name": "@evan/pi-lsp", "name": "@evan/pi-lsp",
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"chokidar": "^5.0.0",
"ignore": "^7.0.5",
"picomatch": "^4.0.4",
"vscode-jsonrpc": "^8.2.1", "vscode-jsonrpc": "^8.2.1",
"vscode-languageserver-protocol": "^3.17.5" "vscode-languageserver-protocol": "^3.17.5"
}, },
@@ -17,6 +20,7 @@
"devDependencies": { "devDependencies": {
"@mariozechner/pi-coding-agent": "^0.72.0", "@mariozechner/pi-coding-agent": "^0.72.0",
"@types/node": "^22.10.0", "@types/node": "^22.10.0",
"@types/picomatch": "^4.0.3",
"oxlint": "^1.62.0", "oxlint": "^1.62.0",
"tsx": "^4.19.2", "tsx": "^4.19.2",
"typebox": "^1.1.37", "typebox": "^1.1.37",
@@ -2789,6 +2793,13 @@
"undici-types": "~6.21.0" "undici-types": "~6.21.0"
} }
}, },
"node_modules/@types/picomatch": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/@types/picomatch/-/picomatch-4.0.3.tgz",
"integrity": "sha512-iG0T6+nYJ9FAPmx9SsUlnwcq1ZVRuCXcVEvWnntoPlrOpwtSTKNDC9uVAxTsC3PUvJ+99n4RpAcNgBbHX3JSnQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/retry": { "node_modules/@types/retry": {
"version": "0.12.0", "version": "0.12.0",
"resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz", "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz",
@@ -2967,6 +2978,21 @@
"url": "https://github.com/chalk/chalk?sponsor=1" "url": "https://github.com/chalk/chalk?sponsor=1"
} }
}, },
"node_modules/chokidar": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-5.0.0.tgz",
"integrity": "sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw==",
"license": "MIT",
"dependencies": {
"readdirp": "^5.0.0"
},
"engines": {
"node": ">= 20.19.0"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/cli-highlight": { "node_modules/cli-highlight": {
"version": "2.1.11", "version": "2.1.11",
"resolved": "https://registry.npmjs.org/cli-highlight/-/cli-highlight-2.1.11.tgz", "resolved": "https://registry.npmjs.org/cli-highlight/-/cli-highlight-2.1.11.tgz",
@@ -3642,7 +3668,6 @@
"version": "7.0.5", "version": "7.0.5",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-7.0.5.tgz", "resolved": "https://registry.npmjs.org/ignore/-/ignore-7.0.5.tgz",
"integrity": "sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==", "integrity": "sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==",
"dev": true,
"license": "MIT", "license": "MIT",
"engines": { "engines": {
"node": ">= 4" "node": ">= 4"
@@ -4085,6 +4110,18 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/picomatch": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz",
"integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==",
"license": "MIT",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/proper-lockfile": { "node_modules/proper-lockfile": {
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz", "resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz",
@@ -4180,6 +4217,19 @@
"once": "^1.3.1" "once": "^1.3.1"
} }
}, },
"node_modules/readdirp": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-5.0.0.tgz",
"integrity": "sha512-9u/XQ1pvrQtYyMpZe7DXKv2p5CNvyVwzUB6uhLAnQwHMSgKMBR62lc7AHljaeteeHXn11XTAaLLUVZYVZyuRBQ==",
"license": "MIT",
"engines": {
"node": ">= 20.19.0"
},
"funding": {
"type": "individual",
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/require-directory": { "node_modules/require-directory": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",

View File

@@ -21,12 +21,16 @@
"test:integration": "NODE_OPTIONS='--import=tsx' node --test test/integration/**/*.ts" "test:integration": "NODE_OPTIONS='--import=tsx' node --test test/integration/**/*.ts"
}, },
"dependencies": { "dependencies": {
"chokidar": "^5.0.0",
"ignore": "^7.0.5",
"picomatch": "^4.0.4",
"vscode-jsonrpc": "^8.2.1", "vscode-jsonrpc": "^8.2.1",
"vscode-languageserver-protocol": "^3.17.5" "vscode-languageserver-protocol": "^3.17.5"
}, },
"devDependencies": { "devDependencies": {
"@mariozechner/pi-coding-agent": "^0.72.0", "@mariozechner/pi-coding-agent": "^0.72.0",
"@types/node": "^22.10.0", "@types/node": "^22.10.0",
"@types/picomatch": "^4.0.3",
"oxlint": "^1.62.0", "oxlint": "^1.62.0",
"tsx": "^4.19.2", "tsx": "^4.19.2",
"typebox": "^1.1.37", "typebox": "^1.1.37",

View File

@@ -3,8 +3,42 @@
// //
// Add new servers here. `match` is a list of file extensions (no dot) OR // Add new servers here. `match` is a list of file extensions (no dot) OR
// language ids; either matches. // language ids; either matches.
import * as fs from "node:fs";
import * as path from "node:path";
import type { ServerConfig } from "./src/types.ts"; import type { ServerConfig } from "./src/types.ts";
// Resolve Python Path - Prefer the project's virtualenv when present so
// Pyright sees the same interpreter/dependencies as project commands.
function resolvePythonPath(rootDir: string): string | undefined {
const candidates = [
path.join(rootDir, ".venv", "bin", "python"),
path.join(rootDir, "venv", "bin", "python"),
];
return candidates.find((candidate) => fs.existsSync(candidate));
}
// Pyright Settings - Minimal editor settings needed for diagnostics and import
// resolution. Shared by workspace/configuration and didChangeConfiguration.
function pyrightSettings(rootDir: string): {
pythonPath: string | undefined;
analysis: {
diagnosticMode: string;
typeCheckingMode: string;
autoSearchPaths: boolean;
useLibraryCodeForTypes: boolean;
};
} {
return {
pythonPath: resolvePythonPath(rootDir),
analysis: {
diagnosticMode: "openFilesOnly",
typeCheckingMode: "basic",
autoSearchPaths: true,
useLibraryCodeForTypes: true,
},
};
}
// Global Root Markers — appended to every server's rootMarkers list // Global Root Markers — appended to every server's rootMarkers list
export const globalRootMarkers = [".git"]; export const globalRootMarkers = [".git"];
@@ -25,6 +59,20 @@ export const servers: ServerConfig[] = [
rootMarkers: ["pnpm-workspace.yaml", "tsconfig.json", "package.json"], rootMarkers: ["pnpm-workspace.yaml", "tsconfig.json", "package.json"],
languageId: "typescript", languageId: "typescript",
}, },
{
id: "svelteserver",
match: ["svelte"],
command: "svelteserver",
args: ["--stdio"],
rootMarkers: [
"svelte.config.js",
"svelte.config.ts",
"svelte.config.cjs",
"svelte.config.mjs",
"package.json",
],
languageId: "svelte",
},
{ {
id: "pyright", id: "pyright",
match: ["py"], match: ["py"],
@@ -32,6 +80,15 @@ export const servers: ServerConfig[] = [
args: ["--stdio"], args: ["--stdio"],
rootMarkers: ["pyproject.toml", "setup.py", "setup.cfg"], rootMarkers: ["pyproject.toml", "setup.py", "setup.cfg"],
languageId: "python", languageId: "python",
workspaceConfiguration: {
initialSettings: ({ rootDir }) => ({ python: pyrightSettings(rootDir) }),
getSection: (section, { rootDir }) => {
const settings = pyrightSettings(rootDir);
if (section === "python") return settings;
if (section === "python.analysis") return settings.analysis;
return null;
},
},
}, },
{ {
id: "lua-language-server", id: "lua-language-server",
@@ -43,12 +100,44 @@ export const servers: ServerConfig[] = [
}, },
{ {
id: "vscode-html-language-server", id: "vscode-html-language-server",
match: ["html", "css", "jsonl", "jsonc", "json"], match: ["html"],
command: "vscode-html-language-server", command: "vscode-html-language-server",
args: ["--stdio"], args: ["--stdio"],
rootMarkers: ["package.json"], rootMarkers: ["package.json"],
languageId: "html", languageId: "html",
}, },
{
id: "vscode-css-language-server",
match: ["css", "scss", "less"],
command: "vscode-css-language-server",
args: ["--stdio"],
rootMarkers: ["package.json"],
languageId: "css",
},
{
id: "vscode-json-language-server",
match: ["json", "jsonc", "jsonl"],
command: "vscode-json-language-server",
args: ["--stdio"],
rootMarkers: ["package.json"],
languageId: "json",
},
{
id: "bash-language-server",
match: ["sh", "bash"],
command: "bash-language-server",
args: ["start"],
rootMarkers: [".git"],
languageId: "shellscript",
},
{
id: "sqls",
match: ["sql"],
command: "sqls",
args: [],
rootMarkers: [".git"],
languageId: "sql",
},
{ {
id: "nil", id: "nil",
match: ["nix"], match: ["nix"],

View File

@@ -7,8 +7,11 @@ import {
type MessageConnection, type MessageConnection,
} from "vscode-jsonrpc/node.js"; } from "vscode-jsonrpc/node.js";
import type { import type {
FileSystemWatcher,
InitializeParams, InitializeParams,
PublishDiagnosticsParams, PublishDiagnosticsParams,
Registration,
Unregistration,
} from "vscode-languageserver-protocol"; } from "vscode-languageserver-protocol";
import type { ServerConfig } from "./types.ts"; import type { ServerConfig } from "./types.ts";
import { ServerNotFoundError } from "./types.ts"; import { ServerNotFoundError } from "./types.ts";
@@ -40,6 +43,8 @@ export class LspClient {
// version numbers in didOpen/didChange. We track them so the daemon // version numbers in didOpen/didChange. We track them so the daemon
// can resync files via notifyChange after on-disk edits. // can resync files via notifyChange after on-disk edits.
private versions = new Map<string, number>(); private versions = new Map<string, number>();
private fileWatchers = new Map<string, FileSystemWatcher[]>();
private watchersListeners = new Set<() => void>();
constructor(private readonly server: ServerConfig) {} constructor(private readonly server: ServerConfig) {}
@@ -102,11 +107,47 @@ export class LspClient {
} }
}, },
); );
// Accept Common Server Requests - Return empty/null so servers don't // Accept Common Server Requests - Return one configuration response per
// stall. Good enough for a CLI; a real client would answer properly. // requested item. Server-specific settings live in server.ts so adding
this.conn.onRequest("workspace/configuration", () => []); // another picky server doesn't grow conditionals in this transport layer.
this.conn.onRequest("client/registerCapability", () => null); this.conn.onRequest(
this.conn.onRequest("client/unregisterCapability", () => null); "workspace/configuration",
(params: { items?: { section?: string }[] }) => {
const items = params.items ?? [];
const config = this.server.workspaceConfiguration;
return items.map((item) =>
config?.getSection?.(item.section, { rootDir, env }) ?? null,
);
},
);
this.conn.onRequest(
"client/registerCapability",
(params: { registrations?: Registration[] }) => {
let changed = false;
for (const reg of params.registrations ?? []) {
if (reg.method !== "workspace/didChangeWatchedFiles") continue;
const opts = reg.registerOptions as
| { watchers?: FileSystemWatcher[] }
| undefined;
this.fileWatchers.set(reg.id, opts?.watchers ?? []);
changed = true;
}
if (changed) for (const l of this.watchersListeners) l();
return null;
},
);
this.conn.onRequest(
"client/unregisterCapability",
(params: { unregisterations?: Unregistration[] }) => {
let changed = false;
for (const unreg of params.unregisterations ?? []) {
if (unreg.method !== "workspace/didChangeWatchedFiles") continue;
if (this.fileWatchers.delete(unreg.id)) changed = true;
}
if (changed) for (const l of this.watchersListeners) l();
return null;
},
);
this.conn.listen(); this.conn.listen();
@@ -125,7 +166,11 @@ export class LspClient {
publishDiagnostics: {}, publishDiagnostics: {},
synchronization: { didSave: true }, synchronization: { didSave: true },
}, },
workspace: { workspaceFolders: true, configuration: true }, workspace: {
workspaceFolders: true,
configuration: true,
didChangeWatchedFiles: { dynamicRegistration: true },
},
}, },
}; };
await this.conn.sendRequest("initialize", { await this.conn.sendRequest("initialize", {
@@ -136,6 +181,16 @@ export class LspClient {
}, },
}); });
this.conn.sendNotification("initialized", {}); this.conn.sendNotification("initialized", {});
// Push Configuration - Some servers do not always request workspace/configuration,
// but still consume settings delivered through didChangeConfiguration.
const settings = this.server.workspaceConfiguration?.initialSettings?.({
rootDir,
env,
});
if (settings !== undefined && settings !== null) {
this.conn.sendNotification("workspace/didChangeConfiguration", { settings });
}
} }
// Wait For Ready - Resolves when there are no outstanding progress // Wait For Ready - Resolves when there are no outstanding progress
@@ -192,11 +247,34 @@ export class LspClient {
return uri; return uri;
} }
closeDocument(uri: string): void {
this.versions.delete(uri);
this.diagnostics.delete(uri);
this.conn.sendNotification("textDocument/didClose", {
textDocument: { uri },
});
}
// Send Raw LSP Request - Passthrough used by the command dispatcher. // Send Raw LSP Request - Passthrough used by the command dispatcher.
sendRequest<R = unknown>(method: string, params: unknown): Promise<R> { sendRequest<R = unknown>(method: string, params: unknown): Promise<R> {
return this.conn.sendRequest(method, params) as Promise<R>; return this.conn.sendRequest(method, params) as Promise<R>;
} }
sendNotification(method: string, params: unknown): void {
this.conn.sendNotification(method, params);
}
getFileWatchers(): FileSystemWatcher[] {
return Array.from(this.fileWatchers.values()).flat();
}
onWatchersChanged(listener: () => void): () => void {
this.watchersListeners.add(listener);
return () => {
this.watchersListeners.delete(listener);
};
}
// Clear Diagnostics - Drops the cached diagnostics for a URI so callers // Clear Diagnostics - Drops the cached diagnostics for a URI so callers
// can force waitForDiagnostics to await a fresh publish after didChange. // can force waitForDiagnostics to await a fresh publish after didChange.
clearDiagnostics(uri: string): void { clearDiagnostics(uri: string): void {

141
src/config.ts Normal file
View File

@@ -0,0 +1,141 @@
// Per-Repo Config - Loads `.pi-lsp.json` from the nearest ancestor of a
// given path and merges it with the built-in server registry. Config is a
// JSON file shaped like:
//
// {
// "servers": [
// { "id": "rust-analyzer", "match": ["rs"], "command": "rust-analyzer",
// "args": [], "rootMarkers": ["Cargo.toml"], "languageId": "rust" },
// { "id": "gopls", "args": ["-remote=auto", "-vv"] }
// ],
// "disable": ["oxlint"]
// }
//
// Merge Semantics:
// - Entries whose `id` matches a built-in shallow-merge their fields (user wins).
// - Entries with a new `id` append, and must include all required fields.
// - The `disable` list filters the merged result by id.
//
// Caching: Resolved lists are cached per config-file path and invalidated by
// mtime. If no config file exists, the built-in registry is returned.
import * as fs from "node:fs";
import * as path from "node:path";
import { servers as builtinServers } from "../server.ts";
import type { ServerConfig } from "./types.ts";
const CONFIG_FILE = ".pi-lsp.json";
interface PiLspConfig {
servers?: Array<Partial<ServerConfig> & { id: string }>;
disable?: string[];
}
interface CacheEntry {
mtimeMs: number;
resolved: ServerConfig[];
}
const cache = new Map<string, CacheEntry>();
// Find Config File - Walks upward from `fromDir` looking for `.pi-lsp.json`.
// Returns the absolute path or null if none found before the filesystem root.
function findConfigFile(fromDir: string): string | null {
let dir = path.resolve(fromDir);
const { root } = path.parse(dir);
while (true) {
const candidate = path.join(dir, CONFIG_FILE);
if (fs.existsSync(candidate)) return candidate;
if (dir === root) return null;
dir = path.dirname(dir);
}
}
// Merge Config - Built-ins by id, user overrides shallow-merge in, new ids
// append (with required-field validation), and `disable` filters the result.
function mergeConfig(config: PiLspConfig, sourcePath: string): ServerConfig[] {
const byId = new Map<string, ServerConfig>(
builtinServers.map((s) => [s.id, { ...s }]),
);
// Apply Overrides And New Servers
for (const override of config.servers ?? []) {
if (!override.id || typeof override.id !== "string") {
throw new Error(
`pi-lsp config (${sourcePath}): every entry in "servers" must have a string "id"`,
);
}
const existing = byId.get(override.id);
if (existing) {
byId.set(override.id, { ...existing, ...override } as ServerConfig);
} else {
// New Server - Must include all required spawn fields.
const required = ["match", "command", "args", "rootMarkers"] as const;
const missing = required.filter((k) => override[k] === undefined);
if (missing.length > 0) {
throw new Error(
`pi-lsp config (${sourcePath}): new server "${override.id}" is missing required field(s): ${missing.join(", ")}`,
);
}
byId.set(override.id, override as ServerConfig);
}
}
// Apply Disable Filter
const disabled = new Set(config.disable ?? []);
return Array.from(byId.values()).filter((s) => !disabled.has(s.id));
}
// Get Servers For Path - Returns the merged ServerConfig list applicable to
// `fromDir` (or the directory containing a file path). Cached per config-file
// path with mtime invalidation. On parse/merge failure, logs to stderr and
// falls back to the built-in registry so a broken config never breaks LSP.
export function getServersForPath(fromPath: string): ServerConfig[] {
// Resolve To Directory - Accept either a file path or a directory.
let dir = path.resolve(fromPath);
try {
const st = fs.statSync(dir);
if (!st.isDirectory()) dir = path.dirname(dir);
} catch {
dir = path.dirname(dir);
}
const configPath = findConfigFile(dir);
if (!configPath) return builtinServers;
let mtimeMs: number;
try {
mtimeMs = fs.statSync(configPath).mtimeMs;
} catch {
return builtinServers;
}
const cached = cache.get(configPath);
if (cached && cached.mtimeMs === mtimeMs) return cached.resolved;
let parsed: PiLspConfig;
try {
const raw = fs.readFileSync(configPath, "utf8");
parsed = JSON.parse(raw);
} catch (err) {
process.stderr.write(
`pi-lsp: failed to read/parse ${configPath}: ${(err as Error).message}\n`,
);
return builtinServers;
}
let resolved: ServerConfig[];
try {
resolved = mergeConfig(parsed, configPath);
} catch (err) {
process.stderr.write(`pi-lsp: ${(err as Error).message}\n`);
return builtinServers;
}
cache.set(configPath, { mtimeMs, resolved });
return resolved;
}
// Clear Config Cache - Test hook; not used in production.
export function clearConfigCache(): void {
cache.clear();
}

View File

@@ -5,10 +5,11 @@
import * as fs from "node:fs"; import * as fs from "node:fs";
import * as net from "node:net"; import * as net from "node:net";
import * as path from "node:path"; import * as path from "node:path";
import type { FileSystemWatcher } from "vscode-languageserver-protocol";
import { LspClient } from "./client.ts"; import { LspClient } from "./client.ts";
import { findRoot, pathToUri } from "./root.ts"; import { findRoot, findServerById, pathToUri } from "./root.ts";
import type { ServerConfig } from "./types.ts"; import type { ServerConfig } from "./types.ts";
import { servers } from "../server.ts"; import { WorkspaceWatcher, type FileEvent } from "./watcher.ts";
import { import {
logPath, logPath,
socketPath, socketPath,
@@ -18,8 +19,12 @@ import {
type LaunchContext, type LaunchContext,
} from "./daemonProtocol.ts"; } from "./daemonProtocol.ts";
// Default Idle TTL - 5 minutes. Per-server overrides via ServerConfig.idleTtlMs.
const DEFAULT_IDLE_TTL_MS = 5 * 60 * 1000; const DEFAULT_IDLE_TTL_MS = 5 * 60 * 1000;
const WATCHER_READY_TIMEOUT_MS = 5000;
const FILE_CHANGE_DELETED = 3;
const WATCH_KIND_CREATE = 1;
const WATCH_KIND_CHANGE = 2;
const WATCH_KIND_DELETE = 4;
// Client Entry - One LspClient per (server.id, rootDir), plus the bookkeeping // Client Entry - One LspClient per (server.id, rootDir), plus the bookkeeping
// needed to keep files in sync and evict on idleness. // needed to keep files in sync and evict on idleness.
@@ -38,6 +43,9 @@ interface ClientEntry {
idleTimer: NodeJS.Timeout | null; idleTimer: NodeJS.Timeout | null;
ttlMs: number; ttlMs: number;
lastUsed: number; lastUsed: number;
watcher: WorkspaceWatcher | null;
unsubscribeWatchers: (() => void) | null;
usesDerivedWatchers: boolean;
} }
const entries = new Map<string, ClientEntry>(); const entries = new Map<string, ClientEntry>();
@@ -51,25 +59,17 @@ function log(...args: unknown[]) {
); );
} }
// Find Server By ID - Looks up a ServerConfig from the registry by ID.
// Throws if the ID is not registered.
function findServerById(serverId: string): ServerConfig {
const server = servers.find((s) => s.id === serverId);
if (!server) {
throw new Error(`Unknown server ID: "${serverId}". Registered: ${servers.map((s) => s.id).join(", ")}`);
}
return server;
}
// Get Or Create Entry - Looks up the cached client for a server+file, // Get Or Create Entry - Looks up the cached client for a server+file,
// spawning a fresh LspClient if needed. The returned entry is guaranteed // spawning a fresh LspClient if needed. The returned entry is guaranteed
// to have its `ready` promise resolved before the caller uses it. // to have its `ready` promise resolved before the caller uses it. The
// server registry is resolved against any `.pi-lsp.json` reachable from
// `filePath`, so per-repo config overrides take effect at spawn time.
async function getOrCreateEntry( async function getOrCreateEntry(
filePath: string, filePath: string,
serverId: string, serverId: string,
launch: LaunchContext, launch: LaunchContext,
): Promise<ClientEntry> { ): Promise<ClientEntry> {
const server = findServerById(serverId); const server = findServerById(filePath, serverId);
const rootDir = findRoot(filePath, server.rootMarkers); const rootDir = findRoot(filePath, server.rootMarkers);
const key = `${server.id}::${rootDir}`; const key = `${server.id}::${rootDir}`;
const existing = entries.get(key); const existing = entries.get(key);
@@ -97,6 +97,9 @@ async function getOrCreateEntry(
idleTimer: null, idleTimer: null,
ttlMs, ttlMs,
lastUsed: Date.now(), lastUsed: Date.now(),
watcher: null,
unsubscribeWatchers: null,
usesDerivedWatchers: false,
}; };
entries.set(key, entry); entries.set(key, entry);
try { try {
@@ -105,10 +108,104 @@ async function getOrCreateEntry(
entries.delete(key); entries.delete(key);
throw err; throw err;
} }
await attachWatcher(entry);
bumpIdle(entry); bumpIdle(entry);
return entry; return entry;
} }
// Attach Watcher - Registration can happen during initialize, before the daemon subscribes.
async function attachWatcher(entry: ClientEntry): Promise<void> {
if (process.env.PI_LSP_DISABLE_WATCHERS) return;
entry.unsubscribeWatchers = entry.client.onWatchersChanged(() => void refreshWatcher(entry));
await refreshWatcher(entry);
}
function watcherPatterns(entry: ClientEntry): FileSystemWatcher[] {
const registered = entry.client.getFileWatchers();
entry.usesDerivedWatchers = registered.length === 0;
return registered.length > 0 ? registered : derivedServerWatchers(entry.server);
}
function derivedServerWatchers(server: ServerConfig): FileSystemWatcher[] {
const extensions = server.match
.map((m) => m.replace(/^\./, ""))
.filter((m) => /^[A-Za-z0-9_-]+$/.test(m));
if (extensions.length === 0) return [];
const globPattern = extensions.length === 1
? `**/*.${extensions[0]}`
: `**/*.{${extensions.join(",")}}`;
return [{
globPattern,
kind: WATCH_KIND_CREATE | WATCH_KIND_CHANGE | WATCH_KIND_DELETE,
}];
}
async function refreshWatcher(entry: ClientEntry): Promise<void> {
if (process.env.PI_LSP_DISABLE_WATCHERS) return;
const patterns = watcherPatterns(entry);
if (patterns.length === 0 && !entry.watcher) return;
if (!entry.watcher) {
entry.watcher = new WorkspaceWatcher(entry.rootDir, (events) =>
forwardEvents(entry, events),
);
log(`watcher`, entry.server.id, entry.rootDir, `patterns=${patterns.length}`);
}
if (process.env.LSP_DEBUG) {
log(`watcher patterns`, entry.server.id, JSON.stringify(patterns));
}
entry.watcher.setPatterns(patterns);
if (patterns.length > 0) await waitForWatcherReady(entry);
}
async function waitForWatcherReady(entry: ClientEntry): Promise<void> {
if (!entry.watcher) return;
let timeout: NodeJS.Timeout | null = null;
let timedOut = false;
try {
await Promise.race([
entry.watcher.ready(),
new Promise<void>((resolve) => {
timeout = setTimeout(() => {
timedOut = true;
resolve();
}, WATCHER_READY_TIMEOUT_MS);
}),
]);
} finally {
if (timeout) clearTimeout(timeout);
}
if (timedOut) {
log(`watcher ready timeout`, entry.server.id, entry.rootDir);
}
}
function forwardEvents(entry: ClientEntry, events: FileEvent[]): void {
try {
for (const event of events) {
if (event.type !== FILE_CHANGE_DELETED || !entry.opened.has(event.uri)) continue;
entry.client.closeDocument(event.uri);
entry.opened.delete(event.uri);
void refreshWatcher(entry);
}
if (process.env.LSP_DEBUG) {
log(`watcher fire`, entry.server.id, JSON.stringify(events));
}
for (const uri of entry.opened.keys()) entry.client.clearDiagnostics(uri);
entry.client.sendNotification("workspace/didChangeWatchedFiles", {
changes: events,
});
if (entry.usesDerivedWatchers && entry.server.id === "typescript-language-server") {
void entry.client.sendRequest("workspace/executeCommand", {
command: "typescript.tsserverRequest",
arguments: ["reloadProjects"],
}).catch((err) => log("typescript reloadProjects failed", (err as Error).message));
}
} catch (err) {
log(`watcher send failed`, entry.server.id, (err as Error).message);
}
}
// Bump Idle - Resets the idle eviction timer. Called on every request that // Bump Idle - Resets the idle eviction timer. Called on every request that
// touches the entry. We log evictions so the daemon's behavior is visible. // touches the entry. We log evictions so the daemon's behavior is visible.
function bumpIdle(entry: ClientEntry) { function bumpIdle(entry: ClientEntry) {
@@ -122,6 +219,8 @@ function evict(entry: ClientEntry, reason: string) {
log(`evict`, entry.key, reason); log(`evict`, entry.key, reason);
entries.delete(entry.key); entries.delete(entry.key);
if (entry.idleTimer) clearTimeout(entry.idleTimer); if (entry.idleTimer) clearTimeout(entry.idleTimer);
if (entry.unsubscribeWatchers) entry.unsubscribeWatchers();
void entry.watcher?.dispose();
void entry.client.dispose(); void entry.client.dispose();
// Auto Shutdown - If this was the last entry, there's nothing left to // Auto Shutdown - If this was the last entry, there's nothing left to
// manage. Tear down the daemon so it doesn't sit idle forever. // manage. Tear down the daemon so it doesn't sit idle forever.
@@ -301,6 +400,8 @@ function shutdownDaemon(reason: string) {
if (server) server.close(); if (server) server.close();
for (const entry of entries.values()) { for (const entry of entries.values()) {
if (entry.idleTimer) clearTimeout(entry.idleTimer); if (entry.idleTimer) clearTimeout(entry.idleTimer);
if (entry.unsubscribeWatchers) entry.unsubscribeWatchers();
void entry.watcher?.dispose();
void entry.client.dispose(); void entry.client.dispose();
} }
entries.clear(); entries.clear();

View File

@@ -1,11 +1,16 @@
import * as fs from "node:fs"; import * as fs from "node:fs";
import * as path from "node:path"; import * as path from "node:path";
import { pathToFileURL, fileURLToPath } from "node:url"; import { pathToFileURL, fileURLToPath } from "node:url";
import { servers, globalRootMarkers } from "../server.ts"; import { globalRootMarkers } from "../server.ts";
import { getServersForPath } from "./config.ts";
import type { ServerConfig } from "./types.ts"; import type { ServerConfig } from "./types.ts";
import { UnsupportedExtensionError } from "./types.ts"; import { UnsupportedExtensionError } from "./types.ts";
import { isOnPath } from "./util.ts"; import { isOnPath } from "./util.ts";
// Re-Export - Centralizes the path-aware registry helper so callers can
// import it from `./root.ts` alongside pickServer/findRoot.
export { getServersForPath };
// Resolve File URI To Local Path // Resolve File URI To Local Path
export function uriToPath(uri: string): string { export function uriToPath(uri: string): string {
return fileURLToPath(uri); return fileURLToPath(uri);
@@ -31,15 +36,30 @@ export function isServerAvailable(server: ServerConfig): boolean {
// Pick Server By File Extension - match[] entries are matched against the // Pick Server By File Extension - match[] entries are matched against the
// file's extension (no dot). First available, non-diagnosticsOnly server wins. // file's extension (no dot). First available, non-diagnosticsOnly server wins.
// Resolves the per-repo config from the file's directory before matching.
export function pickServer(filePath: string): ServerConfig { export function pickServer(filePath: string): ServerConfig {
const ext = path.extname(filePath).replace(/^\./, ""); const ext = path.extname(filePath).replace(/^\./, "");
const hit = servers.find((s) => s.match.includes(ext) && !s.diagnosticsOnly && isServerAvailable(s)); const list = getServersForPath(filePath);
const hit = list.find((s) => s.match.includes(ext) && !s.diagnosticsOnly && isServerAvailable(s));
if (!hit) { if (!hit) {
throw new UnsupportedExtensionError(`.${ext}`); throw new UnsupportedExtensionError(`.${ext}`);
} }
return hit; return hit;
} }
// Find Server By ID - Looks up a ServerConfig by id within the registry
// resolved for the given path. Throws if the id is not registered.
export function findServerById(filePath: string, id: string): ServerConfig {
const list = getServersForPath(filePath);
const hit = list.find((s) => s.id === id);
if (!hit) {
throw new Error(
`Unknown server ID: "${id}". Registered: ${list.map((s) => s.id).join(", ")}`,
);
}
return hit;
}
// Find Project Root By Walking Upward - stops at the first directory // Find Project Root By Walking Upward - stops at the first directory
// containing any rootMarker. Falls back to the file's directory. // containing any rootMarker. Falls back to the file's directory.
export function findRoot(filePath: string, markers: string[]): string { export function findRoot(filePath: string, markers: string[]): string {

View File

@@ -17,6 +17,22 @@ export class ServerNotFoundError extends Error {
} }
} }
export interface WorkspaceConfigurationContext {
rootDir: string;
env: NodeJS.ProcessEnv;
}
export interface ServerWorkspaceConfiguration {
// Initial Settings - Optional payload pushed via workspace/didChangeConfiguration
// after initialize/initialized for servers that don't always request config.
initialSettings?: (ctx: WorkspaceConfigurationContext) => unknown;
// Section Settings - Optional handler for workspace/configuration requests.
getSection?: (
section: string | undefined,
ctx: WorkspaceConfigurationContext,
) => unknown;
}
export interface ServerConfig { export interface ServerConfig {
// Stable identifier (useful for logs and future daemon cache keys). // Stable identifier (useful for logs and future daemon cache keys).
id: string; id: string;
@@ -38,6 +54,9 @@ export interface ServerConfig {
// hover/definition/references/completion/documentSymbol but included // hover/definition/references/completion/documentSymbol but included
// in lsp_diagnostics and auto-check. // in lsp_diagnostics and auto-check.
diagnosticsOnly?: boolean; diagnosticsOnly?: boolean;
// Workspace Configuration - Optional server-specific settings exposed through
// workspace/configuration and workspace/didChangeConfiguration.
workspaceConfiguration?: ServerWorkspaceConfiguration;
} }
// Supported high-level commands exposed via the CLI. Extend this union // Supported high-level commands exposed via the CLI. Extend this union

237
src/watcher.ts Normal file
View File

@@ -0,0 +1,237 @@
import * as fs from "node:fs";
import * as path from "node:path";
import chokidar, { type FSWatcher } from "chokidar";
import ignore, { type Ignore } from "ignore";
import picomatch from "picomatch";
import type { FileSystemWatcher } from "vscode-languageserver-protocol";
import { pathToUri } from "./root.ts";
const FILE_CHANGE_CREATED = 1;
const FILE_CHANGE_CHANGED = 2;
const FILE_CHANGE_DELETED = 3;
const WATCH_KIND_CREATE = 1;
const WATCH_KIND_CHANGE = 2;
const WATCH_KIND_DELETE = 4;
const DEBOUNCE_QUIET_MS = 50;
const DEBOUNCE_MAX_WAIT_MS = 500;
export const BASELINE_IGNORES = [
"**/.git/**",
"**/.DS_Store",
"**/.hg/**",
"**/.svn/**",
];
const NO_GITIGNORE_FALLBACK = ["**/node_modules/**", "**/.git/**"];
export interface FileEvent {
uri: string;
type: 1 | 2 | 3;
}
function buildIgnoreMatcher(rootDir: string): (relPath: string) => boolean {
const gitignorePath = path.join(rootDir, ".gitignore");
let ig: Ignore | null = null;
if (fs.existsSync(gitignorePath)) {
try {
ig = ignore().add(fs.readFileSync(gitignorePath, "utf8"));
} catch {
ig = null;
}
}
const baselineMatcher = picomatch(BASELINE_IGNORES);
if (ig) {
return (relPath) => {
if (baselineMatcher(relPath)) return true;
const posixRel = relPath.split(path.sep).join("/");
if (!posixRel || posixRel === ".") return false;
return ig.ignores(posixRel);
};
}
const fallbackMatcher = picomatch(NO_GITIGNORE_FALLBACK);
return (relPath) => baselineMatcher(relPath) || fallbackMatcher(relPath);
}
function compileWatchers(
watchers: FileSystemWatcher[],
rootDir: string,
): (relPath: string, absPath: string) => number {
const compiled = watchers.map((w) => {
const pattern = typeof w.globPattern === "string"
? w.globPattern
: resolveRelativePattern(w.globPattern, rootDir);
return {
match: picomatch(pattern, { dot: true }),
kind: w.kind ?? (WATCH_KIND_CREATE | WATCH_KIND_CHANGE | WATCH_KIND_DELETE),
};
});
return (relPath, absPath) => {
const posixRel = relPath.split(path.sep).join("/");
const posixAbs = absPath.split(path.sep).join("/");
let kind = 0;
for (const c of compiled) {
if (c.match(posixRel) || c.match(posixAbs)) kind |= c.kind;
}
return kind;
};
}
// Relative Pattern - Servers may send baseUri as a string or WorkspaceFolder.
function resolveRelativePattern(
rp: { baseUri: string | { uri: string }; pattern: string },
rootDir: string,
): string {
const baseUri = typeof rp.baseUri === "string" ? rp.baseUri : rp.baseUri.uri;
if (!baseUri.startsWith("file://")) return rp.pattern;
const basePath = decodeURIComponent(baseUri.slice("file://".length));
const relBase = path.relative(rootDir, basePath);
if (relBase.startsWith("..") || path.isAbsolute(relBase)) return rp.pattern;
return relBase ? `${relBase.split(path.sep).join("/")}/${rp.pattern}` : rp.pattern;
}
export class WorkspaceWatcher {
private chokidar: FSWatcher | null = null;
private isIgnored: (relPath: string) => boolean;
private matchKind: (relPath: string, absPath: string) => number = () => 0;
private pending = new Map<string, 1 | 2 | 3>();
private quietTimer: NodeJS.Timeout | null = null;
private maxWaitTimer: NodeJS.Timeout | null = null;
private disposed = false;
private readyPromise: Promise<void> = Promise.resolve();
private resolveReady: (() => void) | null = null;
constructor(
private readonly rootDir: string,
private readonly onEvents: (events: FileEvent[]) => void,
) {
this.isIgnored = buildIgnoreMatcher(rootDir);
}
setPatterns(watchers: FileSystemWatcher[]): void {
if (this.disposed) return;
this.matchKind = compileWatchers(watchers, this.rootDir);
if (watchers.length === 0) {
this.cancelPending();
void this.stopChokidar();
return;
}
if (!this.chokidar) this.startChokidar();
}
ready(): Promise<void> {
return this.readyPromise;
}
private startChokidar(): void {
this.readyPromise = new Promise<void>((resolve) => {
this.resolveReady = resolve;
});
this.chokidar = chokidar.watch(this.rootDir, {
ignoreInitial: true,
followSymlinks: false,
ignored: (absPath: string) => {
if (absPath === this.rootDir) return false;
const rel = path.relative(this.rootDir, absPath);
return this.isIgnored(rel);
},
awaitWriteFinish: { stabilityThreshold: 50, pollInterval: 25 },
});
this.chokidar.on("add", (p) => this.queue(p, FILE_CHANGE_CREATED));
this.chokidar.on("change", (p) => this.queue(p, FILE_CHANGE_CHANGED));
this.chokidar.on("unlink", (p) => this.queue(p, FILE_CHANGE_DELETED));
this.chokidar.on("ready", () => this.resolveReady?.());
this.chokidar.on("error", (err) => {
process.stderr.write(`[pi-lsp:watcher] ${this.rootDir}: ${String(err)}\n`);
});
}
private async stopChokidar(): Promise<void> {
if (!this.chokidar) return;
const w = this.chokidar;
this.chokidar = null;
this.resolveReady?.();
this.resolveReady = null;
this.readyPromise = Promise.resolve();
await w.close();
}
private cancelPending(): void {
if (this.quietTimer) clearTimeout(this.quietTimer);
if (this.maxWaitTimer) clearTimeout(this.maxWaitTimer);
this.quietTimer = null;
this.maxWaitTimer = null;
this.pending.clear();
}
private queue(absPath: string, type: 1 | 2 | 3): void {
const rel = path.relative(this.rootDir, absPath);
const kind = this.matchKind(rel, absPath);
if (kind === 0) return;
if (type === FILE_CHANGE_CREATED && !(kind & WATCH_KIND_CREATE)) return;
if (type === FILE_CHANGE_CHANGED && !(kind & WATCH_KIND_CHANGE)) return;
if (type === FILE_CHANGE_DELETED && !(kind & WATCH_KIND_DELETE)) return;
const uri = pathToUri(absPath);
const prev = this.pending.get(uri);
const next = coalesce(prev, type);
if (next === null) this.pending.delete(uri);
else this.pending.set(uri, next);
this.scheduleFlush();
}
private scheduleFlush(): void {
if (this.quietTimer) clearTimeout(this.quietTimer);
this.quietTimer = setTimeout(() => this.flush(), DEBOUNCE_QUIET_MS);
if (!this.maxWaitTimer) {
this.maxWaitTimer = setTimeout(() => this.flush(), DEBOUNCE_MAX_WAIT_MS);
}
}
private flush(): void {
if (this.quietTimer) clearTimeout(this.quietTimer);
if (this.maxWaitTimer) clearTimeout(this.maxWaitTimer);
this.quietTimer = null;
this.maxWaitTimer = null;
if (this.pending.size === 0) return;
const events: FileEvent[] = Array.from(this.pending, ([uri, type]) => ({
uri,
type,
}));
this.pending.clear();
try {
this.onEvents(events);
} catch (err) {
process.stderr.write(
`[pi-lsp:watcher] onEvents threw: ${(err as Error).message}\n`,
);
}
}
async dispose(): Promise<void> {
if (this.disposed) return;
this.disposed = true;
this.cancelPending();
await this.stopChokidar();
}
}
function coalesce(
prev: 1 | 2 | 3 | undefined,
next: 1 | 2 | 3,
): 1 | 2 | 3 | null {
if (prev === undefined) return next;
if (prev === FILE_CHANGE_CREATED && next === FILE_CHANGE_DELETED) return null;
if (prev === FILE_CHANGE_DELETED && next === FILE_CHANGE_CREATED) {
return FILE_CHANGE_CHANGED;
}
if (prev === FILE_CHANGE_CREATED && next === FILE_CHANGE_CHANGED) {
return FILE_CHANGE_CREATED;
}
return next;
}

View File

@@ -25,10 +25,11 @@ export const tsx = path.resolve(
"cli.mjs", "cli.mjs",
); );
// Unique Test Socket — each test run gets its own Unix socket so we don't // Unique Test Socket — each suite gets its own Unix socket so parallel
// touch any real session daemon. // integration tests don't race through the same daemon.
export function testSocket(): string { export function testSocket(): string {
return path.join(os.tmpdir(), `pi-lsp-test-${process.pid}.sock`); const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-lsp-test-"));
return path.join(dir, "daemon.sock");
} }
// Set Test Socket — sets PI_LSP_SOCKET_PATH for the current process and // Set Test Socket — sets PI_LSP_SOCKET_PATH for the current process and
@@ -39,7 +40,7 @@ export function setTestSocket(env: Record<string, string | undefined>): () => vo
return () => { return () => {
delete env.PI_LSP_SOCKET_PATH; delete env.PI_LSP_SOCKET_PATH;
try { try {
fs.unlinkSync(sock); fs.rmSync(path.dirname(sock), { recursive: true, force: true });
} catch { } catch {
// Socket may not exist — that's fine. // Socket may not exist — that's fine.
} }

View File

@@ -13,15 +13,15 @@ describe("cli daemon lifecycle", () => {
const env = { ...process.env }; const env = { ...process.env };
let cleanup: () => void; let cleanup: () => void;
before(() => { before(async () => {
cleanup = setTestSocket(env); cleanup = setTestSocket(env);
// Stop any stale daemon on this socket before tests run. // Stop any stale daemon on this socket before tests run.
stopTestDaemon(env); await stopTestDaemon(env);
}); });
after(() => { after(async () => {
// Tear down daemon and clean up socket after all tests. // Tear down daemon and clean up socket after all tests.
stopTestDaemon(env); await stopTestDaemon(env);
cleanup(); cleanup();
}); });

View File

@@ -0,0 +1,169 @@
import { describe, it, before, after } from "node:test";
import * as assert from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import {
setTestSocket,
stopTestDaemon,
runCliJson,
requireServer,
} from "../helpers.ts";
const skip = requireServer("gopls");
async function pollUntil<T>(
fn: () => Promise<T>,
predicate: (v: T) => boolean,
timeoutMs: number,
intervalMs = 250,
): Promise<T> {
const deadline = Date.now() + timeoutMs;
let last: T = await fn();
while (Date.now() < deadline) {
if (predicate(last)) return last;
await new Promise((r) => setTimeout(r, intervalMs));
last = await fn();
}
return last;
}
interface DiagResult {
[serverId: string]: { diagnostics?: { message: string }[] };
}
describe("watcher: gopls picks up external file changes", { skip: skip ?? undefined }, () => {
let tmpDir: string;
let mainFile: string;
let helperFile: string;
const env = { ...process.env };
let cleanup: () => void;
before(async () => {
cleanup = setTestSocket(env);
await stopTestDaemon(env);
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-lsp-gopls-watch-"));
fs.writeFileSync(path.join(tmpDir, "go.mod"), "module example.com/wtest\n\ngo 1.21\n");
mainFile = path.join(tmpDir, "main.go");
helperFile = path.join(tmpDir, "helper.go");
fs.writeFileSync(
mainFile,
"package main\n\nfunc main() {\n\tHelper()\n}\n",
);
});
after(async () => {
await stopTestDaemon(env);
cleanup();
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it("initially reports undefined symbol", async () => {
const result = await pollUntil(
async () =>
(await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":3000}'],
env,
)) as DiagResult,
(r) => {
const diags = r["gopls"]?.diagnostics ?? [];
return diags.some(
(d) =>
d.message.toLowerCase().includes("undefined") ||
d.message.includes("Helper"),
);
},
15000,
500,
);
const diags = result["gopls"]?.diagnostics ?? [];
const hasUndefined = diags.some((d) =>
d.message.toLowerCase().includes("undefined") || d.message.includes("Helper"),
);
assert.ok(
hasUndefined,
`Expected undefined-symbol diagnostic, got: ${JSON.stringify(diags)}`,
);
});
it("clears the diagnostic after helper.go is created externally", async () => {
fs.writeFileSync(
helperFile,
"package main\n\nfunc Helper() {}\n",
);
const result = await pollUntil(
async () =>
(await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":3000}'],
env,
)) as DiagResult,
(r) => {
const diags = r["gopls"]?.diagnostics ?? [];
return !diags.some(
(d) =>
d.message.toLowerCase().includes("undefined") ||
d.message.includes("Helper"),
);
},
15000,
500,
);
const finalDiags = result["gopls"]?.diagnostics ?? [];
const stillUndefined = finalDiags.some(
(d) =>
d.message.toLowerCase().includes("undefined") ||
d.message.includes("Helper"),
);
assert.ok(
!stillUndefined,
`Expected diagnostic to clear after creating helper.go, still got: ${JSON.stringify(finalDiags)}`,
);
});
it("closes an opened file when it is deleted externally", async () => {
fs.writeFileSync(
helperFile,
"package main\n\nfunc Helper(x int) {}\n",
);
await runCliJson([helperFile, "diagnostics", '{"timeoutMs":3000}'], env);
await pollUntil(
async () =>
(await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":3000}'],
env,
)) as DiagResult,
(r) => {
const diags = r["gopls"]?.diagnostics ?? [];
return diags.some((d) => d.message.includes("not enough arguments"));
},
15000,
500,
);
fs.rmSync(helperFile);
const result = await pollUntil(
async () =>
(await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":3000}'],
env,
)) as DiagResult,
(r) => {
const diags = r["gopls"]?.diagnostics ?? [];
return diags.some((d) => d.message.toLowerCase().includes("undefined"));
},
15000,
500,
);
const finalDiags = result["gopls"]?.diagnostics ?? [];
assert.ok(
finalDiags.some((d) => d.message.toLowerCase().includes("undefined")),
`Expected undefined-symbol diagnostic after deleting opened helper.go, got: ${JSON.stringify(finalDiags)}`,
);
});
});

View File

@@ -0,0 +1,143 @@
import { describe, it, before, after } from "node:test";
import * as assert from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import {
setTestSocket,
stopTestDaemon,
runCliJson,
requireServer,
} from "../helpers.ts";
const skip = requireServer("typescript-language-server");
async function pollUntil<T>(
fn: () => Promise<T>,
predicate: (v: T) => boolean,
timeoutMs: number,
intervalMs = 250,
): Promise<T> {
const deadline = Date.now() + timeoutMs;
let last: T = await fn();
while (Date.now() < deadline) {
if (predicate(last)) return last;
await new Promise((r) => setTimeout(r, intervalMs));
last = await fn();
}
return last;
}
interface DiagResult {
[serverId: string]: { diagnostics?: { message: string }[] };
}
describe("watcher: typescript handles derived file patterns", { skip: skip ?? undefined }, () => {
let tmpDir: string;
let mainFile: string;
let helperFile: string;
const env = { ...process.env };
let cleanup: () => void;
before(async () => {
delete env.NODE_OPTIONS;
cleanup = setTestSocket(env);
await stopTestDaemon(env);
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-lsp-ts-watch-"));
fs.writeFileSync(
path.join(tmpDir, ".pi-lsp.json"),
JSON.stringify({ disable: ["oxlint"] }),
);
fs.writeFileSync(
path.join(tmpDir, "tsconfig.json"),
JSON.stringify(
{
compilerOptions: {
target: "ES2022",
module: "ESNext",
moduleResolution: "Bundler",
strict: true,
noEmit: true,
},
include: ["*.ts"],
},
null,
2,
),
);
mainFile = path.join(tmpDir, "main.ts");
helperFile = path.join(tmpDir, "helper.ts");
fs.writeFileSync(
mainFile,
'import { helper } from "./helper";\n\nconsole.log(helper());\n',
);
fs.writeFileSync(helperFile, "export function helper(): number {\n return 1;\n}\n");
});
after(async () => {
await stopTestDaemon(env);
cleanup();
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it("clears missing module after an unopened imported file is created", async () => {
fs.rmSync(helperFile);
const missing = (await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":5000}'],
env,
)) as DiagResult;
assert.ok(
(missing["typescript-language-server"]?.diagnostics ?? []).some((d) =>
d.message.includes("Cannot find module './helper'")
),
);
fs.writeFileSync(helperFile, "export function helper(): number {\n return 1;\n}\n");
const result = await pollUntil(
async () =>
(await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":3000}'],
env,
)) as DiagResult,
(r) => (r["typescript-language-server"]?.diagnostics ?? []).length === 0,
15000,
500,
);
assert.deepEqual(result["typescript-language-server"]?.diagnostics ?? [], []);
});
it("reports missing module after an opened imported file is deleted", async () => {
const initial = (await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":5000}'],
env,
)) as DiagResult;
assert.deepEqual(initial["typescript-language-server"]?.diagnostics ?? [], []);
await runCliJson([helperFile, "diagnostics", '{"timeoutMs":5000}'], env);
fs.rmSync(helperFile);
const result = await pollUntil(
async () =>
(await runCliJson(
[mainFile, "diagnostics", '{"timeoutMs":3000}'],
env,
)) as DiagResult,
(r) => {
const diags = r["typescript-language-server"]?.diagnostics ?? [];
return diags.some((d) => d.message.includes("Cannot find module './helper'"));
},
15000,
500,
);
const diags = result["typescript-language-server"]?.diagnostics ?? [];
assert.ok(
diags.some((d) => d.message.includes("Cannot find module './helper'")),
`Expected missing-module diagnostic after deleting opened helper.ts, got: ${JSON.stringify(diags)}`,
);
});
});

130
test/unit/config.test.ts Normal file
View File

@@ -0,0 +1,130 @@
// Config Unit Tests — getServersForPath() merge semantics, mtime caching,
// and graceful fallback on parse errors.
import { describe, it, beforeEach, afterEach } from "node:test";
import * as assert from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { getServersForPath, clearConfigCache } from "../../src/config.ts";
import { servers as builtinServers } from "../../server.ts";
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-lsp-config-test-"));
clearConfigCache();
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
clearConfigCache();
});
function writeConfig(dir: string, content: unknown): void {
fs.writeFileSync(path.join(dir, ".pi-lsp.json"), JSON.stringify(content));
}
describe("getServersForPath", () => {
it("returns built-in servers when no config file is found", () => {
// tmpDir lives under /tmp; no ancestor will have .pi-lsp.json.
const result = getServersForPath(tmpDir);
assert.deepStrictEqual(
result.map((s) => s.id),
builtinServers.map((s) => s.id),
);
});
it("appends a new server entry from config", () => {
writeConfig(tmpDir, {
servers: [
{
id: "rust-analyzer",
match: ["rs"],
command: "rust-analyzer",
args: [],
rootMarkers: ["Cargo.toml"],
languageId: "rust",
},
],
});
const result = getServersForPath(tmpDir);
const rust = result.find((s) => s.id === "rust-analyzer");
assert.ok(rust, "rust-analyzer should be present");
assert.deepStrictEqual(rust!.match, ["rs"]);
// Built-ins are still there
assert.ok(result.find((s) => s.id === "gopls"));
});
it("shallow-merges overrides into existing built-in servers", () => {
writeConfig(tmpDir, {
servers: [{ id: "gopls", args: ["-vv"] }],
});
const result = getServersForPath(tmpDir);
const gopls = result.find((s) => s.id === "gopls")!;
assert.deepStrictEqual(gopls.args, ["-vv"]);
// Other fields preserved from built-in
assert.strictEqual(gopls.command, "gopls");
assert.deepStrictEqual(gopls.match, ["go"]);
});
it("filters out servers listed in `disable`", () => {
writeConfig(tmpDir, { disable: ["oxlint", "gopls"] });
const result = getServersForPath(tmpDir);
assert.ok(!result.some((s) => s.id === "oxlint"));
assert.ok(!result.some((s) => s.id === "gopls"));
assert.ok(result.some((s) => s.id === "pyright"));
});
it("walks upward to find config in an ancestor directory", () => {
writeConfig(tmpDir, { disable: ["oxlint"] });
const sub = path.join(tmpDir, "a", "b", "c");
fs.mkdirSync(sub, { recursive: true });
const result = getServersForPath(sub);
assert.ok(!result.some((s) => s.id === "oxlint"));
});
it("accepts a file path and walks from its directory", () => {
writeConfig(tmpDir, { disable: ["oxlint"] });
const file = path.join(tmpDir, "src", "main.go");
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, "package main");
const result = getServersForPath(file);
assert.ok(!result.some((s) => s.id === "oxlint"));
});
it("falls back to built-ins on invalid JSON", () => {
fs.writeFileSync(path.join(tmpDir, ".pi-lsp.json"), "{ not valid");
const result = getServersForPath(tmpDir);
assert.deepStrictEqual(
result.map((s) => s.id),
builtinServers.map((s) => s.id),
);
});
it("falls back to built-ins when a new server is missing required fields", () => {
writeConfig(tmpDir, {
servers: [{ id: "incomplete", match: ["foo"] }],
});
const result = getServersForPath(tmpDir);
assert.deepStrictEqual(
result.map((s) => s.id),
builtinServers.map((s) => s.id),
);
});
it("invalidates cache on mtime change", () => {
writeConfig(tmpDir, { disable: ["oxlint"] });
let result = getServersForPath(tmpDir);
assert.ok(!result.some((s) => s.id === "oxlint"));
// Rewrite With Different Content And Bump mtime
const cfgPath = path.join(tmpDir, ".pi-lsp.json");
fs.writeFileSync(cfgPath, JSON.stringify({ disable: ["gopls"] }));
const future = new Date(Date.now() + 5000);
fs.utimesSync(cfgPath, future, future);
result = getServersForPath(tmpDir);
assert.ok(result.some((s) => s.id === "oxlint"));
assert.ok(!result.some((s) => s.id === "gopls"));
});
});

293
test/unit/watcher.test.ts Normal file
View File

@@ -0,0 +1,293 @@
import { describe, it, before, after, beforeEach } from "node:test";
import * as assert from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { WorkspaceWatcher, type FileEvent } from "../../src/watcher.ts";
import { pathToUri } from "../../src/root.ts";
async function waitFor(
predicate: () => boolean,
timeoutMs = 2000,
): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (predicate()) return true;
await new Promise((r) => setTimeout(r, 25));
}
return predicate();
}
const FLUSH_WAIT_MS = 700;
describe("WorkspaceWatcher", () => {
let tmpDir: string;
let received: FileEvent[][] = [];
let watcher: WorkspaceWatcher | null = null;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-lsp-watcher-"));
});
after(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
beforeEach(async () => {
if (watcher) {
await watcher.dispose();
watcher = null;
}
received = [];
for (const entry of fs.readdirSync(tmpDir)) {
fs.rmSync(path.join(tmpDir, entry), { recursive: true, force: true });
}
});
it("emits Created event for new file matching pattern", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.writeFileSync(path.join(tmpDir, "foo.ts"), "x");
const ok = await waitFor(() => received.length > 0);
assert.ok(ok, "Expected at least one batch");
const all = received.flat();
const match = all.find((e) => e.uri === pathToUri(path.join(tmpDir, "foo.ts")));
assert.ok(match, `Expected event for foo.ts, got ${JSON.stringify(all)}`);
assert.strictEqual(match.type, 1);
});
it("emits Deleted event when file removed", async () => {
const file = path.join(tmpDir, "del.ts");
fs.writeFileSync(file, "x");
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.unlinkSync(file);
const ok = await waitFor(() =>
received.flat().some((e) => e.type === 3 && e.uri === pathToUri(file)),
);
assert.ok(ok, `Expected delete event, got ${JSON.stringify(received)}`);
});
it("emits Changed event when file content changes", async () => {
const file = path.join(tmpDir, "chg.ts");
fs.writeFileSync(file, "x");
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.writeFileSync(file, "y");
const ok = await waitFor(() =>
received.flat().some((e) => e.type === 2 && e.uri === pathToUri(file)),
);
assert.ok(ok, `Expected change event, got ${JSON.stringify(received)}`);
});
it("skips files not matching the pattern", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.writeFileSync(path.join(tmpDir, "ignored.txt"), "x");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
assert.strictEqual(
all.length,
0,
`Expected no events for .txt, got ${JSON.stringify(all)}`,
);
});
it("honors .gitignore at root", async () => {
fs.writeFileSync(path.join(tmpDir, ".gitignore"), "ignored-dir/\n");
fs.mkdirSync(path.join(tmpDir, "ignored-dir"));
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.writeFileSync(path.join(tmpDir, "ignored-dir", "x.ts"), "x");
fs.writeFileSync(path.join(tmpDir, "watched.ts"), "x");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
const uris = all.map((e) => e.uri);
assert.ok(
uris.includes(pathToUri(path.join(tmpDir, "watched.ts"))),
"watched.ts should fire",
);
assert.ok(
!uris.some((u) => u.includes("ignored-dir")),
`ignored-dir/x.ts should be filtered, got ${JSON.stringify(uris)}`,
);
});
it("respects WatchKind to filter event types", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts", kind: 1 }]);
await watcher.ready();
const file = path.join(tmpDir, "only-create.ts");
fs.writeFileSync(file, "x");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
fs.unlinkSync(file);
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
assert.ok(
all.some((e) => e.type === 1),
"Expected create event",
);
assert.ok(
!all.some((e) => e.type === 3),
`Did not expect delete events, got ${JSON.stringify(all)}`,
);
});
it("batches multiple rapid events into one onEvents call", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
for (let i = 0; i < 5; i++) {
fs.writeFileSync(path.join(tmpDir, `f${i}.ts`), "x");
}
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
assert.strictEqual(all.length, 5, `Expected 5 events, got ${all.length}`);
assert.ok(
received.length <= 2,
`Expected <=2 batches, got ${received.length}: ${JSON.stringify(received)}`,
);
});
it("ignores .git/ even when not in gitignore", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*" }]);
await watcher.ready();
fs.mkdirSync(path.join(tmpDir, ".git"));
fs.writeFileSync(path.join(tmpDir, ".git", "HEAD"), "ref: foo");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
assert.ok(
!all.some((e) => e.uri.includes("/.git/")),
`.git contents should be ignored, got ${JSON.stringify(all)}`,
);
});
it("stops emitting after dispose", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
await watcher.dispose();
watcher = null;
fs.writeFileSync(path.join(tmpDir, "post-dispose.ts"), "x");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
assert.strictEqual(all.length, 0, `Expected no events after dispose`);
});
it("matches absolute-path glob patterns (gopls-style)", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([
{ globPattern: `${tmpDir}/**/*.{go,mod}` },
]);
await watcher.ready();
fs.writeFileSync(path.join(tmpDir, "helper.go"), "package x\n");
const ok = await waitFor(() =>
received
.flat()
.some((e) => e.uri === pathToUri(path.join(tmpDir, "helper.go"))),
);
assert.ok(ok, `Expected event for absolute glob, got ${JSON.stringify(received)}`);
});
it("coalesces Created+Deleted to no-op", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
const file = path.join(tmpDir, "transient.ts");
fs.writeFileSync(file, "x");
fs.unlinkSync(file);
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
assert.ok(
!all.some((e) => e.uri === pathToUri(file)),
`Transient file should not surface, got ${JSON.stringify(all)}`,
);
});
it("coalesces Deleted+Created (replacement) to Changed", async () => {
const file = path.join(tmpDir, "replace.ts");
fs.writeFileSync(file, "v1");
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.unlinkSync(file);
fs.writeFileSync(file, "v2");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
const all = received.flat();
const events = all.filter((e) => e.uri === pathToUri(file));
assert.ok(events.length > 0, `Expected an event for replaced file`);
const types = events.map((e) => e.type).sort();
const acceptable =
JSON.stringify(types) === JSON.stringify([2]) ||
JSON.stringify(types) === JSON.stringify([1, 3]);
assert.ok(
acceptable,
`Expected [2] or [1,3], got ${JSON.stringify(types)}`,
);
});
it("drops pending events when patterns are cleared", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([{ globPattern: "**/*.ts" }]);
await watcher.ready();
fs.writeFileSync(path.join(tmpDir, "pending.ts"), "x");
await new Promise((r) => setTimeout(r, 10));
watcher.setPatterns([]);
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
assert.strictEqual(
received.length,
0,
`Pending batch should be dropped, got ${JSON.stringify(received)}`,
);
});
it("empty pattern set means no watching", async () => {
watcher = new WorkspaceWatcher(tmpDir, (evs) => received.push(evs));
watcher.setPatterns([]);
await watcher.ready();
fs.writeFileSync(path.join(tmpDir, "no-patterns.ts"), "x");
await new Promise((r) => setTimeout(r, FLUSH_WAIT_MS));
assert.strictEqual(received.length, 0);
});
});