fix(lsp-client): pass workspace root as spawn cwd - #3
Open
nl0 wants to merge 2 commits into
Open
Conversation
Language servers that auto-discover project config from the current working directory (e.g. Astral ty looking for .venv, pytest for pyproject.toml, gopls for go.mod) can't find the right project when the server is spawned with the parent process's cwd. This surfaces as false-positive unresolved-import diagnostics when the MCP host (Claude Code, etc.) is launched from outside the workspace. Inherit the workspace root for the LSP child process so `getcwd()` inside the language server matches the LSP initialize rootUri. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Installing from a git source (e.g. `npx github:owner/lsp-mcp-server` or `npm install github:...`) does not execute prepublishOnly, so the repo otherwise fails to produce the `dist/` output that `bin` and `main` reference. Add a `prepare` hook that mirrors `prepublishOnly` so git-sourced installs work out of the box, while keeping the existing publish-time build path unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Hit the same root cause from Ruby/Sorbet via |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Language servers that resolve project-level configuration from the current working directory — e.g. Astral's
tysearching for.venv,pyrightscanning forpyproject.toml,goplslooking upgo.mod— fail to discover the correct environment whenlsp-mcp-serveris launched from a directory that isn't the workspace root (which is the common case when Claude Code is started from the user's shell cwd). The LSP still receives the rightrootUriviainitialize, but for tools that readgetcwd()directly, that's irrelevant.Manifests as e.g. spurious
Cannot resolve imported module <X>errors pointing at the MCP host's shell venv (oruvx's own tool-env), not the project's.Fix
Pass
this._workspaceRootascwdtospawn()inLSPClientImpl.initialize. The value is already known and normalized at that point (assigned one line above from therootUriparameter).Reproducer
VIRTUAL_ENVpointing at some unrelated venv, launch an MCP client that spawnslsp-mcp-server.uvx ty server..venvin its root.lsp_diagnosticsreturnsunresolved-importerrors for packages that are actually present in the project venv.With this fix,
tysees the project's.venvviagetcwd()and the false positives disappear.Secondary commit
The second commit adds a
preparescript sonpm install/npxfrom a git source (e.g.github:…) buildsdist/automatically. Orthogonal to the fix — happy to split off or drop if you'd rather keep the git-install path separate.Test plan
npm run buildcleannpm run typecheckcleannpm test— all 32 existing unit tests passnpx github:nl0/lsp-mcp-server#fix/spawn-cwd-to-workspace-root,tycorrectly resolves the project's.venvimports;typescript-language-servercontinues to work (it was already unaffected because it usesinitialize.rootUri).🤖 Generated with Claude Code