openlawlibrary / pygls

A pythonic generic language server
https://pygls.readthedocs.io/en/latest/
Apache License 2.0
563 stars 102 forks source link

Simplify vscode-playground setup and fix Python discovery #374

Closed alcarney closed 9 months ago

alcarney commented 12 months ago

Description (e.g. "Related to ...", etc.)

image

This attempts to fix the issues seen in #357 @tombh @z80dev, would you mind trying this out and seeing if it fixes your issues?

There was also a bug in how the json_server.py handled the workspace/diagnostic request when no documents were open - that should be fixed now

Code review checklist (for code reviewer to complete)

tombh commented 11 months ago

It works!

Loading up VSCode from within a source env/bin/activate shell just automatically finds the right Python interpreter now πŸ₯³

2023-09-14 18:02:22.536 [info] Extension activated.
2023-09-14 18:02:22.536 [info] python env modified, restarting server...
2023-09-14 18:02:22.536 [info] cwd: '/home/tombh/Workspace/pygls/examples/servers'
2023-09-14 18:02:22.536 [info] server: 'json_server.py'
2023-09-14 18:02:22.536 [info] Looking for environment in which to execute: 'file:///home/tombh/Workspace/pygls/examples/servers/json_server.py'
2023-09-14 18:02:22.536 [info] Found environment: /home/tombh/Workspace/pygls/env/bin/python: /home/tombh/Workspace/pygls/env/bin/python
2023-09-14 18:02:22.757 [info] client options: {
  "documentSelector": [
    {
      "scheme": "file",
      "language": "json"
    }
  ],
  "outputChannel": {
    "name": "pygls",
    "logLevel": 3
  },
  "connectionOptions": {
    "maxRestartCount": 0
  }
}
2023-09-14 18:02:28.097 [info] Validating json...

And when I make an error in the test.json it seems I get 2 sets of diagnostic reports, I assume one of them must be the example server. The other must come by default with VSCode? image

But I spent a long time trying to get the Code Actions and Inlay Hints to work, because I didn't realise that I had to edit the settigns.json file 😬 Coming from the Neovim world I thought I had to find a keyboard shortcut to show the Code Actions popup, and that I had to enable Inlay Hints in some global settings. So I think it'd be good to very briefly explain how to run each of the example servers and what to expect when each one does run.

This is so awesome!! image image

alcarney commented 11 months ago

It works!

Glad to hear it!

Loading up VSCode from within a source env/bin/activate shell just automatically finds the right Python interpreter now

Would you mind quickly trying it with the venv deactivated also? In an ideal world the extension shouldn't require you to open VSCode in any special way. (Though you might have to use the Python: Select Interpreter command as a first time setup step)

And when I make an error in the test.json it seems I get 2 sets of diagnostic reports, I assume one of them must be the example server. The other must come by default with VSCode?

Yes :)

So I think it'd be good to very briefly explain how to run each of the example servers and what to expect when each one does run.

Ok, I'll look at expanding on the README a bit more - do you mind if I pinch your screenshots?

tombh commented 11 months ago

I had a bit of a play without using the venv's activate and couldn't get it to work. I think the basic fact is that VSCode's Python extension can only find the env/bin/python interpreter when VSCode is opened in the root of the project (therefore the parent of .git/ and env).

This is what I see when opening VSCode in examples/vscode-playground: image

And this is what I see when opening VSCode from the project root: image

alcarney commented 11 months ago

I think the basic fact is that VSCode's Python extension can only find the env/bin/python interpreter when VSCode is opened in the root of the project (therefore the parent of .git/ and env).

Oh! It's amazing what rough edges you gloss over when you're used to something. πŸ˜…

That's how it works for me as well. You should be able to use the Enter interpreter path option to get a file open dialog and manually set the path to the python exe for the playground to use 🀞

Once setup VsCode should remember the choice for future sessions.

Another step to go in README...

alcarney commented 11 months ago

Alternatively....

Perhaps it's better to move the launch.json file for the playground into the root of the repo... and try and set things up so that people don't have to open VSCode in a subfolder at all πŸ€”

tombh commented 11 months ago

Oh! It's amazing what rough edges you gloss over when you're used to something. πŸ˜…

OMG I know, so true πŸ˜…

You should be able to use the Enter interpreter path option to get a file open dialog and manually set the path to the python exe for the playground to use 🀞

It doesn't seem to in my case. Again, I might be doing something wrong, but even once I've selected the ./env/bin/python from a VSCode instance opened in the examples/vscode-playground folder, I still get the No module named 'pygls' error 🫀

Perhaps it's better to move the launch.json file for the playground into the root of the repo

Hmm, yes, I suppose that might be what's needed.

alcarney commented 11 months ago

Again, I might be doing something wrong, but even once I've selected the ./env/bin/python from a VSCode instance opened in the examples/vscode-playground folder, I still get the No module named 'pygls' error 🫀

So strange! :/

@karthiknadig I don't suppose you'd able to take a quick look at this function and see if I'm missing anything obvious. It should allow people to use Python: Select Interpreter to configure the Python envrionment to use with pygls, but it doesn't seem to work for anyone except me! :sweat_smile:

Otherwise, I'll look into moving things around.

karthiknadig commented 11 months ago

When calling getActiveEnvironmentPath is it called with the workspace.uri or some uri from the current workspace. If it is called with undefined it can fail to get selected python.

alcarney commented 10 months ago

@tombh I wonder if we should merge this?

I did have a go at moving the launch.json file to the root of the repo, but VSCode didn't seem to want to launch a debug instance in the same folder as the main instance :/

At the very least the simplified folder structure should be an improvement and we have a few workarounds if discovery fails

Ideally it should Just WorkTM, but I'm not sure what else to try at this point

tombh commented 9 months ago

Sure, let's merge it. I think they're relatively minor issues really. The main thing is that there are workarounds. And having it merged might give it more exposure and more feedback and ideas.

tombh commented 9 months ago

Shall I merge this then? πŸš€

alcarney commented 9 months ago

@tombh go for it :)

tombh commented 9 months ago

Done. And a little point release as well to celebrate?

alcarney commented 9 months ago

Aren't we waiting to hear back about the impact of #411?

karthiknadig commented 9 months ago

@alcarney I will provide an Update on #411 by end of the day for me (which is Pacific time)