Fix bundled LSP paths always inserted at front of sys.path regardless of importStrategy#584
Draft
Fix bundled LSP paths always inserted at front of sys.path regardless of importStrategy#584
Conversation
- lsp_server.py: Replace update_sys_path with unconditional insert(0,...) for bundled libs and tool dirs - lsp_runner.py: Replace update_sys_path with unconditional insert(0,...) for bundled libs - lsp_utils.py: Add ImportError to contextlib.suppress in _run_module for graceful degradation Fixes: #282, #316, #344, #387, #409 Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix sys.path ordering for bundled LSP infrastructure
Fix bundled LSP paths always inserted at front of sys.path regardless of importStrategy
Mar 11, 2026
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.
When
importStrategyis"fromEnvironment", bundledlibs/was appended tosys.path, allowing user-installed packages to shadowpygls,lsprotocol,cattrs, andtyping_extensions— crashing the LSP server on startup.Changes
lsp_server.py/lsp_runner.py: Replaced theupdate_sys_pathfunction (which conditionally appended vs. inserted based on strategy) with unconditionalsys.path.insert(0, ...)for bundled paths. User environments can still provide their ownisortvia the interpreter's default path; only LSP infrastructure is locked to bundled.lsp_utils.py: AddedImportErrortocontextlib.suppressin_run_moduleso that any import failure that still slips through degrades gracefully instead of propagating up to crash the server.script_runner.pyand_debug_server.pyeach have their ownupdate_sys_pathcopies and are left untouched.Original prompt
Problem
When
importStrategyis"fromEnvironment", the bundledlibs/path is appended to the end ofsys.path. User-installed packages (e.g. viapip install -r requirements.txt) can shadow the extension's bundled LSP dependencies (pygls,lsprotocol,cattrs,typing_extensions), causing import failures that crash the server.This is the root cause behind multiple recurring crash issues: #282, #316, #344, #387, #409.
Fix
Always insert bundled LSP infrastructure paths at the front of
sys.path, regardless of import strategy. The user's environment should only affect resolution ofisortitself, never the LSP server infrastructure.Changes needed in 3 files:
1.
bundled/tool/lsp_server.pyRemove the existing
update_sys_pathfunction and the two calls to it (lines 17–36):Replace with:
2.
bundled/tool/lsp_runner.pyRemove the
update_sys_pathfunction and its call (lines 16–29):Replace with:
3.
bundled/tool/lsp_utils.pyChange line 161 from:
To:
This ensures that if a broken import still slips through despite the sys.path fix,
_run_moduledegrades gracefully instead of propagating the exception up to crash the server.Important notes
script_runner.pyor_debug_server.pyupdate_sys_pathfunction is also used inscript_runner.pyand_debug_server.pybut those have their own copies — leave those untouchedThe following is the prior conversation context from the user's chat exploration (may be truncated):
User: Running "pip install -r requirements.txt" Makes the server crash. Are there any issues describing this?
Assistant: I searched the
microsoft/vscode-isortrepository for issues related to runningpip install -r requirements.txtcausing the server to crash. There are no issues that specifically describe runningpip install -r requirements.txtas causing the isort server to crash.However, there are many closed issues about the isort server crashing due to dependency/environment conflicts — which is the likely root cause of what you're experiencing. When you run
pip install -r requirements.txt, packages installed into the Python environment can conflict with the extension's bundled dependencies, causing the LSP server to fail. Here are the most relevant ones: