Skip to content

Commit 71693cc

Browse files
authored
tweak: only spawn lsp servers for files in current instance (or cwd if instance is global) (anomalyco#19058)
1 parent 700f571 commit 71693cc

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

packages/opencode/src/lsp/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ export namespace LSP {
177177

178178
async function getClients(file: string) {
179179
const s = await state()
180+
181+
// Only spawn LSP clients for files within the instance directory
182+
if (!Instance.containsPath(file)) {
183+
return []
184+
}
185+
180186
const extension = path.parse(file).ext || file
181187
const result: LSPClient.Info[] = []
182188

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, spyOn, test } from "bun:test"
2+
import path from "path"
3+
import * as Lsp from "../../src/lsp/index"
4+
import { LSPServer } from "../../src/lsp/server"
5+
import { Instance } from "../../src/project/instance"
6+
import { tmpdir } from "../fixture/fixture"
7+
8+
describe("lsp.spawn", () => {
9+
test("does not spawn builtin LSP for files outside instance", async () => {
10+
await using tmp = await tmpdir()
11+
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
12+
13+
try {
14+
await Instance.provide({
15+
directory: tmp.path,
16+
fn: async () => {
17+
await Lsp.LSP.touchFile(path.join(tmp.path, "..", "outside.ts"))
18+
await Lsp.LSP.hover({
19+
file: path.join(tmp.path, "..", "hover.ts"),
20+
line: 0,
21+
character: 0,
22+
})
23+
},
24+
})
25+
26+
expect(spy).toHaveBeenCalledTimes(0)
27+
} finally {
28+
spy.mockRestore()
29+
await Instance.disposeAll()
30+
}
31+
})
32+
33+
test("would spawn builtin LSP for files inside instance", async () => {
34+
await using tmp = await tmpdir()
35+
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
36+
37+
try {
38+
await Instance.provide({
39+
directory: tmp.path,
40+
fn: async () => {
41+
await Lsp.LSP.hover({
42+
file: path.join(tmp.path, "src", "inside.ts"),
43+
line: 0,
44+
character: 0,
45+
})
46+
},
47+
})
48+
49+
expect(spy).toHaveBeenCalledTimes(1)
50+
} finally {
51+
spy.mockRestore()
52+
await Instance.disposeAll()
53+
}
54+
})
55+
})

0 commit comments

Comments
 (0)