Add middleware that enhances the ty configuration section with the active Python interpreter#65
Conversation
7e1ed8c to
6e934d0
Compare
| if (middleware.isDidChangeConfigurationRegistered()) { | ||
| logger.debug("Python interpreter changed, sending DidChangeConfigurationNotification"); | ||
|
|
||
| // If the Python interpreter changes, | ||
| newLSClient.sendNotification(DidChangeConfigurationNotification.type, undefined); | ||
| } |
There was a problem hiding this comment.
This is a bit overkill at the moment because we always restart the server when the interpreter changes (because we might need to use a different binary now). I kept the implementation because it took me a rather long time to figure out how to know if the server registered the capability but I can also remove it again.
I think it would also be great if we can avoid restarting the LSP if the interpreter changes....
There was a problem hiding this comment.
Yeah, agreed. I think the only reason that we would use a different binary is if the ty binary used is installed in the virtual environment where the interpreter is present.
I don't know if we can avoid the restart completely but we could check if the interpreter is even going to be used to find the binary path. If it's not going to be used, then we can avoid restarting the server when the interpreter changes.
The checks that I'm referring to is the one before line 101 in the below code:
ty-vscode/src/common/server.ts
Lines 79 to 136 in 6e934d0
So, if we know how we're going to choose the ty binary, then we can use that information to avoid restarting the server when it's not required.
| @@ -1,4 +1,4 @@ | |||
| import * as util from "util"; | |||
| import * as util from "node:util"; | |||
There was a problem hiding this comment.
I fixed a few issues that biome found along the way
src/client.ts
Outdated
| if (param.section === "ty") { | ||
| const scopeUri = param.scopeUri ? Uri.parse(param.scopeUri) : undefined; | ||
| const activeEnvironment = | ||
| pythonExtension.environments.getActiveEnvironmentPath(scopeUri); |
There was a problem hiding this comment.
I'm not sure yet if we should call resolveEnvironment here. The benefit is that we only get valid environments. The downside is that ty has to repeat this work and may still decide that this isn't an active environment (for some reason or another)
| async function getPythonExtensionAPI(): Promise<PythonExtension | undefined> { | ||
| if (_api) { | ||
| return _api; | ||
| } | ||
| _api = await PythonExtension.api(); | ||
| return _api; | ||
| export async function getPythonExtensionAPI(): Promise<PythonExtension> { | ||
| const api = _api || (await PythonExtension.api()); | ||
| _api = api; | ||
| return api; |
| if (middleware.isDidChangeConfigurationRegistered()) { | ||
| logger.debug("Python interpreter changed, sending DidChangeConfigurationNotification"); | ||
|
|
||
| // If the Python interpreter changes, | ||
| newLSClient.sendNotification(DidChangeConfigurationNotification.type, undefined); | ||
| } |
There was a problem hiding this comment.
Yeah, agreed. I think the only reason that we would use a different binary is if the ty binary used is installed in the virtual environment where the interpreter is present.
I don't know if we can avoid the restart completely but we could check if the interpreter is even going to be used to find the binary path. If it's not going to be used, then we can avoid restarting the server when the interpreter changes.
The checks that I'm referring to is the one before line 101 in the below code:
ty-vscode/src/common/server.ts
Lines 79 to 136 in 6e934d0
So, if we know how we're going to choose the ty binary, then we can use that information to avoid restarting the server when it's not required.
|
I'm assuming that this should be merged along with astral-sh/ruff#19012, right? |
|
So, all in all, to fully support this, we require implementing the following in the server:
I might be missing something but how does this work then without registering the |
b7d731f to
6a606ee
Compare
dhruvmanila
left a comment
There was a problem hiding this comment.
Looks good. I think we need to validate and probably change the way registration is being handled here. We probably don't need to look at the registration options for the didChangeConfiguration notification (as it doesn't exists) and directly add the id to didChangeRegistrations. I think the TyMiddleware is only for the ty server so that should be accurate and the server won't be providing any registration options either.
6a606ee to
0995df8
Compare
Summary
One step closer towards #26
This PR adds a custom middleware that intercepts the
configurationrequest and it adds the information for the selected and active python interpreter. The value returned by the Python extension doesn't necessarily have to point to a valid python environment. But we can use it as a fallback if it does.Test Plan
Screen.Recording.2025-07-10.at.14.23.50.mov