Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions packages/lsp/src/server/biome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import path from 'node:path'
import process from 'node:process'
import { attachLSPProcessHandlers, log, nearestRoot } from './utils'

/** @biomejs/biome version installed by the bunx fallback. */
export const BIOME_VERSION = '2.6.2'

export const BiomeServer: LSPServerInfo = {
id: 'biome',
extensions: [
Expand Down Expand Up @@ -60,7 +63,8 @@ export const BiomeServer: LSPServerInfo = {
}

// Check global PATH
const globalBin = Bun.which('biome')
// Read PATH now rather than the copy Bun captured at startup
const globalBin = Bun.which('biome', { PATH: process.env.PATH ?? '' })
if (globalBin) {
const proc = spawn(globalBin, ['lsp-proxy', '--stdio'], {
cwd: root,
Expand All @@ -70,18 +74,18 @@ export const BiomeServer: LSPServerInfo = {
return { process: proc }
}

// Fallback: Try via bunx
try {
const proc = spawn('bunx', ['biome', 'lsp-proxy', '--stdio'], {
cwd: root,
env: { ...process.env, BUN_BE_BUN: '1' },
})
attachLSPProcessHandlers(proc, 'biome')
return { process: proc }
}
catch (err) {
log.warn({ root, err }, 'Biome not found. Install with: npm install @biomejs/biome')
// Fallback: Try via bunx. The unscoped `biome` npm package is an unrelated project.
// spawn reports a missing bunx only as an async 'error' event, so check for it first.
const bunx = Bun.which('bunx', { PATH: process.env.PATH ?? '' })
if (!bunx) {
log.warn({ root }, 'Biome not found. Install with: npm install @biomejs/biome')
return undefined
}
const proc = spawn(bunx, [`@biomejs/biome@${BIOME_VERSION}`, 'lsp-proxy', '--stdio'], {
cwd: root,
env: { ...process.env, BUN_BE_BUN: '1' },
})
attachLSPProcessHandlers(proc, 'biome')
return { process: proc }
},
}
112 changes: 80 additions & 32 deletions packages/lsp/test/unit/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { LSPServerInfo } from '../../src/server/types'
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { describe, expect, test } from 'bun:test'
import { describe, expect, spyOn, test } from 'bun:test'
import {
BiomeServer,
DartServer,
DenoServer,
EslintServer,
Expand All @@ -18,7 +20,7 @@ import {
TypescriptServer,
VueServer,
} from '../../src/server'
import { findRunnableBin } from '../../src/server/utils'
import { findRunnableBin, log } from '../../src/server/utils'

describe('LSP_SERVERS', () => {
test('contains expected servers', () => {
Expand Down Expand Up @@ -461,38 +463,45 @@ describe.skipIf(process.platform === 'win32')('findRunnableBin', () => {
})
})

describe.skipIf(process.platform === 'win32')('TypescriptServer classic fallback', () => {
// Each fake server records its name and arguments in `invoked` under the project root.
const recorder = (name: string): string => `#!/bin/sh\necho "${name} $*" > invoked\n`
// Each fake server records its name and arguments in `invoked` under the project root.
const recorder = (name: string): string => `#!/bin/sh\necho "${name} $*" > invoked\n`

/** Spawn `server` with only `pathBins` on PATH and return what the launched binary recorded. */
async function spawnServerWith(
server: LSPServerInfo,
pathBins: Record<string, string>,
setupRoot: (rootDir: string) => Promise<void> = async () => {},
): Promise<string> {
const binDir = await fs.mkdtemp(path.join(os.tmpdir(), 'lsp-fallback-bin-'))
const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'lsp-fallback-root-'))
const savedPath = process.env.PATH
try {
for (const [name, script] of Object.entries(pathBins)) {
await fs.writeFile(path.join(binDir, name), script, { mode: 0o755 })
}
await setupRoot(rootDir)
process.env.PATH = binDir
const handle = await server.spawn(rootDir)
process.env.PATH = savedPath
expect(handle).toBeDefined()
const proc = handle!.process
if (proc.exitCode === null && proc.signalCode === null) {
await new Promise(resolve => proc.once('exit', resolve))
}
return (await fs.readFile(path.join(rootDir, 'invoked'), 'utf8')).trim()
}
finally {
process.env.PATH = savedPath
await fs.rm(binDir, { recursive: true, force: true })
await fs.rm(rootDir, { recursive: true, force: true })
}
}

async function spawnTypescriptWith(
describe.skipIf(process.platform === 'win32')('TypescriptServer classic fallback', () => {
const spawnTypescriptWith = (
pathBins: Record<string, string>,
setupRoot: (rootDir: string) => Promise<void> = async () => {},
): Promise<string> {
const binDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ts-fallback-bin-'))
const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ts-fallback-root-'))
const savedPath = process.env.PATH
try {
for (const [name, script] of Object.entries(pathBins)) {
await fs.writeFile(path.join(binDir, name), script, { mode: 0o755 })
}
await setupRoot(rootDir)
process.env.PATH = binDir
const handle = await TypescriptServer.spawn(rootDir)
process.env.PATH = savedPath
expect(handle).toBeDefined()
const proc = handle!.process
if (proc.exitCode === null && proc.signalCode === null) {
await new Promise(resolve => proc.once('exit', resolve))
}
return (await fs.readFile(path.join(rootDir, 'invoked'), 'utf8')).trim()
}
finally {
process.env.PATH = savedPath
await fs.rm(binDir, { recursive: true, force: true })
await fs.rm(rootDir, { recursive: true, force: true })
}
}
setupRoot?: (rootDir: string) => Promise<void>,
): Promise<string> => spawnServerWith(TypescriptServer, pathBins, setupRoot)

test('falls back to bunx with a pinned typescript-language-server when the one on PATH cannot run', async () => {
const invoked = await spawnTypescriptWith({
Expand Down Expand Up @@ -526,6 +535,45 @@ describe.skipIf(process.platform === 'win32')('TypescriptServer classic fallback
})
})

describe.skipIf(process.platform === 'win32')('BiomeServer fallback', () => {
test('falls back to bunx with a pinned @biomejs/biome when biome is not installed', async () => {
const invoked = await spawnServerWith(BiomeServer, { bunx: recorder('bunx') })
expect(invoked).toBe('bunx @biomejs/biome@2.6.2 lsp-proxy --stdio')
})

test('prefers biome on PATH over bunx', async () => {
const invoked = await spawnServerWith(BiomeServer, { biome: recorder('path'), bunx: recorder('bunx') })
expect(invoked).toBe('path lsp-proxy --stdio')
})

test('prefers a project-local biome', async () => {
const invoked = await spawnServerWith(BiomeServer, { biome: recorder('path'), bunx: recorder('bunx') }, async (rootDir) => {
const binDir = path.join(rootDir, 'node_modules', '.bin')
await fs.mkdir(binDir, { recursive: true })
await fs.writeFile(path.join(binDir, 'biome'), recorder('local'), { mode: 0o755 })
})
expect(invoked).toBe('local lsp-proxy --stdio')
})

test('returns undefined with the install hint when bunx is missing', async () => {
const emptyDir = await fs.mkdtemp(path.join(os.tmpdir(), 'lsp-fallback-empty-'))
const savedPath = process.env.PATH
const warn = spyOn(log, 'warn')
try {
process.env.PATH = emptyDir
const handle = await BiomeServer.spawn(emptyDir)
process.env.PATH = savedPath
expect(handle).toBeUndefined()
expect(warn).toHaveBeenCalledWith(expect.anything(), 'Biome not found. Install with: npm install @biomejs/biome')
}
finally {
warn.mockRestore()
process.env.PATH = savedPath
await fs.rm(emptyDir, { recursive: true, force: true })
}
})
})

describe('VueServer', () => {
test('has correct id', () => {
expect(VueServer.id).toBe('vue')
Expand Down
Loading