Skip to content

Conversation

@elad12390
Copy link
Contributor

@elad12390 elad12390 commented Nov 10, 2025

Problem

Fixes #23

When multiple LSP servers support the same file extension with different rootDir values (common in monorepos), cclsp was selecting the first matching server regardless of which workspace the file belongs to.

Root Cause

The getServerForFile() method used first-match logic based only on file extension:

const server = this.config.servers.find((server) => server.extensions.includes(extension));

This ignored the rootDir configuration entirely.

Solution

When multiple servers match a file's extension, now selects the server whose rootDir most specifically contains the file (longest matching path). This ensures each file is analyzed by the correct workspace-aware LSP server.

Example:

{
  "servers": [
    { "extensions": ["ts"], "command": ["ts-server"], "rootDir": "." },
    { "extensions": ["ts"], "command": ["ts-server"], "rootDir": "repos/app" }
  ]
}

File at repos/app/src/test.ts → Uses server with rootDir: "repos/app" (more specific)
File at other/test.ts → Uses server with rootDir: "." (fallback)

Changes

  • Updated getServerForFile() to select based on most specific rootDir
  • Added comprehensive tests for server selection logic
  • Maintains backwards compatibility with single-server configurations

Testing

✅ All existing tests pass (158 tests)
✅ Added 7 new test cases for server selection
✅ Linting and type checking pass

bun test src/server-selection.test.ts
 7 pass
 0 fail

When multiple LSP servers support the same file extension, now selects
the server whose rootDir most specifically contains the file (longest
matching path). This ensures each file is analyzed by the correct
workspace-aware LSP server.

Example: File at repos/app/src/test.ts with servers:
- Server A: rootDir: '.'
- Server B: rootDir: 'repos/app'
Now correctly selects Server B (more specific).

Fallback to first match when no rootDir contains the file, maintaining
backwards compatibility for existing single-server configurations.
@elad12390 elad12390 force-pushed the fix/server-selection-rootdir branch from 817ee1e to afc99f8 Compare November 10, 2025 11:00
@ktnyt
Copy link
Owner

ktnyt commented Nov 19, 2025

LGTM! Thank you for your contribution!

@ktnyt ktnyt merged commit e1f91a4 into ktnyt:main Nov 19, 2025
7 of 10 checks passed
@ktnyt
Copy link
Owner

ktnyt commented Nov 19, 2025

@elad12390 The fix has been released as v0.6.2. Thank you again!

@elad12390
Copy link
Contributor Author

elad12390 commented Nov 19, 2025

@ktnyt Yay thanks happy to help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support multiple LSP servers with different rootDir for monorepos

2 participants