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

Python imports not working in demo app #865

Open
justinmann opened this issue Feb 16, 2025 · 4 comments
Open

Python imports not working in demo app #865

justinmann opened this issue Feb 16, 2025 · 4 comments

Comments

@justinmann
Copy link

  1. Local imports do not seem to work
    Image

  2. Package imports do not seem to work
    According to this doc, https://microsoft.github.io/pyright/#/import-resolution, it will fallback to the local python interpreter to find packages. I made to pip install matplotlib in my local machine before starting the language server and no luck

@greycodee
Copy link

You can refer to this answer #861

@justinmann
Copy link
Author

Is there any way to override PyRight's file loading? If so, we should be able to redirect the file loading call over the WebSocket connection and return the file from the browser.

It looks like PyRight uses a FileSystem interface retrieved from its internal serviceProvider to handle file ops:
https://github.com/microsoft/pyright/blob/ccb873d46d4f950658286bcd4e7c2b74a35049f6/packages/pyright-internal/src/analyzer/sourceFile.ts#L263

In the demo app, it looks like pyright is run as an external process:

  1. https://github.com/microsoft/pyright/blob/ccb873d46d4f950658286bcd4e7c2b74a35049f6/packages/pyright/src/langserver.ts
  2. https://github.com/microsoft/pyright/blob/ccb873d46d4f950658286bcd4e7c2b74a35049f6/packages/pyright/src/langserver.ts

In step 2 the serviceProvider is injected, by bringing pyright in-process or by creating a new pyright server start wrapper, you could override the FileSystem interface in the serviceProvider.

@kaisalmen
Copy link
Collaborator

Hi @justinmann honestly I don't know much about pyright. If you have questions regarding it please ask in their repository.
The examples here should give you an idea how you could integrate a language server and we covered the most requested once over time.
One thing all the language server examples have in common is that we don't want to change anything in existing language server implementations. That is the reason we start them as external process and handle communication with them over a local socket.

The one thing we want to support and integrate at some point is that the client and the express app that controls the language server start and communication can sync the file systems of the "remote" client and its own. This is covered by #834 The clangd language server that runs as wasm/web worker does something like this already, but in the context of web workers. We want to generalize this and make tool available .

@justinmann
Copy link
Author

What if you modify RegisteredFileSystemProvider to connect with the language server to push the file system changes to the server?

Specifically:

  1. Change the homeDir to /tmp/[guid]
  2. Modify RegisteredFileSystemProvider to send updated files to language server
  3. Modify language server to copy those files into the same /tmp/[guid] folder on the server

To handle the global imports, you could create a .venv folder and run pip install (on the language server) whenever requirements.txt file changes.

If you want to get fancy, you could also monitor the language server file system to make this a bi-directional sync.

I would think this approach would also enable remote execution & debugging.

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

No branches or pull requests

3 participants