Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language server doesn't see packages installed in venv #111

Open
beauxq opened this issue Jan 26, 2025 · 11 comments
Open

language server doesn't see packages installed in venv #111

beauxq opened this issue Jan 26, 2025 · 11 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@beauxq
Copy link

beauxq commented Jan 26, 2025

What happened?

In the PyCharm terminal:

(.venv) :~/PycharmProjects/PythonProject$ pyright
/home/beauxq/PycharmProjects/PythonProject/h.py
  /home/beauxq/PycharmProjects/PythonProject/h.py:8:10 - error: Type "Literal['3']" is not assignable to declared type "int"
    "Literal['3']" is not assignable to "int" (reportAssignmentType)
1 error, 0 warnings, 0 informations

There's no error on the import line.


In the editor, on the first line, I have a red squiggly that says:
Import "typed_argparse" could not be resolved Pyright (reportMissingImports)

Steps to reproduce

With a new PyCharm 2024.3.1.1 (Community Edition)
(deleted ~/.config/JetBrains and ~/.local/share/JetBrains before this)

From the plugin marketplace in PyCharm, I install Pyright 0.8.0 and LSP4IJ 0.9.0
Then I create a new PyCharm project with a venv
In the PyCharm terminal, with the venv activated:

  • pip install pyright
  • pip install typed-argparse

In the project, create a new python file, h.py

from typed_argparse import TypedArgs


class C(TypedArgs):
    pass


x: int = "3"  # testing type checker

In Settings > Tools > Pyright > Project:
I set the executable to the pyright in the venv
/home/beauxq/PycharmProjects/PythonProject/.venv/bin/pyright
I set the language server executable to the pyright-langserver in the venv
/home/beauxq/PycharmProjects/PythonProject/.venv/bin/pyright-langserver
I set Running mode to LSP4IJ

Relevant log output or stack trace


Operating system

Linux

@InSyncWithFoo
Copy link
Owner

For Pyright to work correctly, it needs to be passed the interpreter path. For the language server, this value is derived from the IntelliJ module in which the file resides.

The LSP4IJ client currently does not have access to a module, only the project. This leads to the LSP settings object being created with the interpreter path set as null. It should have instead fallen back to the only module of that project (projects created by PyCharm and other non-IntelliJ-IDEA IDEs typically/always have exactly one module).

You can download the fix from here. Could you install it manually and see if it works for you?

@beauxq
Copy link
Author

beauxq commented Jan 26, 2025

I downloaded pyright-0.8.0.zip from that link.
I used that "Install Plugin from Disk..." menu item and selected that zip file.
PyCharm told me to restart the IDE for the plugin update, so I restarted PyCharm.

I still see the same Import "typed_argparse" could not be resolved Pyright (reportMissingImports) red squiggly.

@InSyncWithFoo
Copy link
Owner

InSyncWithFoo commented Jan 26, 2025

I can reproduce this with LSP4IJ but not the native client. The didChangeConfiguration notifications have the exact same content, so I'm unsure what the cause is.

While debugging, I have found that specifying the virtual environment explicitly helps (this is by no mean a fix though):

# pyproject.toml
[tool.pyright]
venvPath = "."
venv = ".venv"
// pyrightconfig.json
{
	"venvPath": ".",
	"venv": ".venv"
}

@angelozerr This seems to be some subtle difference between the two clients. Could you have a look?

@angelozerr
Copy link

I am sorry wi dont understand what you want I look? More I have not ultimate so I cannot play with lsp native

Is it possible to share lsp traces from LSP4IJ and lsp native to compare them? I wonder if it is a problem with workspace folder or env variables.

@InSyncWithFoo
Copy link
Owner

@angelozerr If you haven't heard the news, RustRover, WebStorm and Rider are free for non-commercial uses. Also, the EAP is opening. In case none of those work for you, here are my logs.

@angelozerr
Copy link

@angelozerr If you haven't heard the news, RustRover, WebStorm and Rider are free for non-commercial uses. Also, the EAP is opening. In case none of those work for you, here are my logs.

Thznks for the logs. I have noticed that with LSP4IJ rootUri from initialized params, and didOpen uri dont uses % in their uri (file:///c%3A/Users/InSync/project) although in native lsp they use this encoded uri.

@InSyncWithFoo could you try to generate the same uri than lsp native with lsp4ij nighty to use the api which support customization of the uri

@InSyncWithFoo
Copy link
Owner

@angelozerr LSPClientFeatures.getFileUri() doesn't seem to be called at all.

Also, this modification should perhaps be implemented within LSP4IJ itself. While the spec does require that servers support both C: and c%3A, VSCode always sends the latter and some servers only support this form. The native client's getFileUri()'s default implementation follows VSCode's, so perhaps LSP4IJ should do the same as well, if only to maintain compatibility.

@angelozerr
Copy link

@angelozerr LSPClientFeatures.getFileUri() doesn't seem to be called at all.

I have debugged ot and the method is called?

LSP4IJ should do the same as well, if only to maintain compatibility.

LSP4IJ sypports both syntax.

@InSyncWithFoo I re-read the issue and it seems the problem exists with another OS (/home/beauxq/PycharmProjects/PythonProject/h.py). I have no idea why there is this different behavior

@angelozerr
Copy link

InLSP native thereis this trace;

#c.i.p.l.i.c.Lsp4jServerConnector - <-- PyrightServerDescriptor@project: 
{
  "jsonrpc": "2.0",
  "method": "window/logMessage",
  "params": {
    "type": 3,
    "message": "Setting pythonPath for service \"project\": \"C:\\Users\\InSync\\project\\.venv\\Scripts\\python.exe\""
  }
}

why not in lsp4ij?

@InSyncWithFoo
Copy link
Owner

why not in lsp4ij?

That's the big question: I have no idea.

@angelozerr
Copy link

The best thing is to debug the language server, I'm sorry I will not have time to do that.

@InSyncWithFoo InSyncWithFoo removed their assignment Jan 30, 2025
@InSyncWithFoo InSyncWithFoo added the help wanted Extra attention is needed label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants