Skip to content

Excessive re-indexing when switching Python virtual environments #977

@Montana

Description

@Montana

Hey all,

When working on projects that frequently switch Python virtual environments (e.g., via pyenv, direnv, or manual venv activation), the language server appears to re-index the entire workspace each time the interpreter path changes, even if the underlying project files remain unchanged.

In large monorepos or projects with many dependencies, this leads to noticeable delays before features like completion, hover, and definition lookup become responsive again. It seems that interpreter changes invalidate the entire analysis cache rather than selectively refreshing only environment-specific modules.

Since python-language-server relies on libraries like Jedi and optional plugins (rope, pyflakes, etc.) for analysis and completion, frequent full refreshes can become costly in environments where developers regularly switch between multiple virtual environments.

For a fix, I've introduced a cache keyed by:

(workspace_hash, interpreter_path)

In this case the environment changes reuse previously indexed results when switching back.

The current fix/idea is:

cache_key = f"{workspace_hash}:{sys.executable}"

if cache_key in analysis_cache:
    state = analysis_cache[cache_key]
else:
    state = rebuild_workspace_index()
    analysis_cache[cache_key] = state

Additionally, incremental invalidation could be triggered only for modules whose import paths differ between environments.

Sincerely,
Michael

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions