Skip to content

Fix #144: completion uses the project's interpreter - #147

Open
swlim9999 wants to merge 1 commit into
albilu:masterfrom
swlim9999:pr/144-project-venv-completion
Open

swlim9999 wants to merge 1 commit into
albilu:masterfrom
swlim9999:pr/144-project-venv-completion

Conversation

@swlim9999

Copy link
Copy Markdown
Contributor

Fixes #144: completion now uses the project's interpreter (its venv, Poetry environment or selected Python) instead of the plugin's own pylsp environment.

Cause

pylsp's Jedi plugins use pylsp.plugins.jedi.environment. The plugin never sent it, so Jedi analysed code with the Python that runs pylsp (<userdir>/.pythonlsp/.venv). That's why only the standard library and packages installed in that environment completed.

Change

New PythonLspConfiguration sends the pylsp settings, with plugins.jedi.environment set:

  • When a server starts or restarts. PythonLspClient.startServer registers the new server. Its settings are sent once NetBeans has initialized it (LSPBindings change event). Previously they were sent only from the project-opened hook, so restarted servers and files outside projects got none. The hook still sends them too, for a server that kept running while its project was closed and reopened.
  • When the interpreter is changed in Project Properties → General. Only that project's servers are updated.
  • When the settings are saved in Options → Python → LSP. Each server keeps its own project's interpreter. Previously every server got the same settings without an interpreter, and so did non-Python language servers.

The interpreter is, in order:

  1. nbproject.python_path, or the Poetry environment (what Run uses).
  2. Otherwise a plugins.jedi.environment the user put in the settings JSON (kept as is).
  3. Otherwise the default Python platform. Files outside projects use this too.

Two guards, both checked against pylsp 1.15 and Jedi 0.20:

  • An interpreter file that doesn't exist (for example, a deleted .venv) is not sent. With such a path, jedi.create_environment raises, which would break every Jedi request (completion, hover, go to definition), not just fall back.
  • The bundled settings.json has "env_vars": {}. While no environment is set, pylsp ignores env_vars. Once one is set, Jedi starts that interpreter with exactly these variables, so an empty map would mean an empty environment. That works on Linux, but CPython on Windows fails to start without SYSTEMROOT. An empty env_vars is therefore left out, so pylsp passes on its own environment (minus PYTHONPATH, as pylsp always does). A non-empty env_vars is sent unchanged.

pom.xml: surefire sets netbeans.user to target/test-userdir, because PythonUtility needs it in tests.

Tests

  • PythonLspConfigurationTest (12 tests, run in CI):

    • settings building, env_vars handling and interpreter precedence;
    • a missing interpreter is not sent, and a damaged platforms.json falls back to pylsp's own environment;
    • startup configuration waits for initialization and is sent once;
    • restart, Project Properties changes and Options changes each keep the right interpreter per project;
    • a server that has exited doesn't stop the others from being updated;
    • servers that exit before or after initialization are forgotten.

    It works on both RELEASE290 and RELEASE310; the private LSPBindings constructor differs between them.

  • PythonLspInterpreterIntegrationTest uses a real pylsp. It is skipped unless -Dpython.lsp.test.executable=/absolute/path/to/python-with-pylsp is set. It creates a venv that has a module pylsp's environment lacks, and checks three things with the bundled default settings: completion misses the module without the interpreter (the issue), finds it with it, and loses it again after switching back.

Built with clean verify on RELEASE290 and RELEASE310: 32 tests pass, including the real-pylsp test (pylsp 1.15.0, Jedi 0.20.1, Python 3.14).

Also checked in a real NetBeans 31, with an isolated profile and the packaged NBM, through NetBeans' own LSP client. The project's .venv contains a module that pylsp's environment doesn't have:

  • completion offers the module's symbol; the build from current master does not;
  • changing the project interpreter removes it, and changing back restores it;
  • saving the Options settings keeps it;
  • killing the pylsp process, so NetBeans starts a new one, keeps it.

How to test by hand

  1. In a project with a .venv, install a package that the plugin's own environment lacks, e.g. .venv/bin/pip install requests.
  2. Select that .venv as the project's Python in Project Properties.
  3. Type import requests and then requests. in a file: completion lists get, post, and so on. On master it lists nothing.

Not tested on Windows or macOS.

This change was written with AI assistance (Claude Opus) and reviewed and tested as described above.

🤖 Generated with Claude Code

pylsp's Jedi used the Python that runs pylsp, so only packages of the
plugin's own environment completed. Send plugins.jedi.environment with
the project's interpreter (nbproject.python_path, Poetry, else a
configured jedi.environment, else the default platform) when a server
starts or restarts, when the project interpreter changes and when the
LSP settings are saved. Missing interpreters and the default empty
env_vars are not sent, because both break Jedi.

Tests: PythonLspConfigurationTest, plus a real-pylsp integration test
(-Dpython.lsp.test.executable).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

LSP Server not using project venv

1 participant