sublimelsp / LSP-pylsp

Convenience package for the Python Language Server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jedi.api.environment.InvalidPythonEnvironment: Could not get version information for '.\\venv\\Scripts\\python.exe': OSError(22, 'The parameter is incorrect', None, 87, None)

kevinvalk opened this issue · comments

I am picking up work on a Python project again and now I get an exception in the pylsp service. From what I remember everything was working just fine two months ago, during I did not touch this environment. However, there obviously were package updates on the Sublime Text side.

Anyways, any ideas on how to debug and figure out what could be going wrong? Maybe this issue should be reported on the jedi side of things?

  • Windows 10
  • Python version 3.9.5 en 3.9.6 (tested both)
  • Sublime Text 4 (4113)
// Settings in here override those in "LSP-pylsp/LSP-pylsp.sublime-settings"
{
	"settings": {
		"pylsp.plugins.autopep8.enabled": false,
		"pylsp.plugins.pylsp_black.enabled": true,
		"pylsp.plugins.pyls_isort.enabled": true,
		"pylsp.plugins.jedi.environment": ".\\venv",

		// Pycodestyle settings
		"pylsp.plugins.pycodestyle.ignore": ["E501", "W503", "E203"],
		"pylsp.plugins.pycodestyle.exclude": ["venv\\**"]
	}
}
::  -> LSP-pylsp textDocument/didChange: <params with 14255 characters>
LSP-pylsp: 2021-08-23 09:32:02,756 W. Europe Daylight Time - ERROR - pylsp_jsonrpc.endpoint - Failed to handle request 219
LSP-pylsp: Traceback (most recent call last):
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\cache.py", line 110, in wrapper
LSP-pylsp:     return dct[key]
LSP-pylsp: KeyError: ((), frozenset())
LSP-pylsp: 
LSP-pylsp: During handling of the above exception, another exception occurred:
LSP-pylsp: 
LSP-pylsp: Traceback (most recent call last):
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\api\environment.py", line 77, in _get_subprocess
LSP-pylsp:     info = self._subprocess._send(None, _get_info)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\inference\compiled\subprocess\__init__.py", line 237, in _send
LSP-pylsp:     pickle_dump(data, self._get_process().stdin, PICKLE_PROTOCOL)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\cache.py", line 112, in wrapper
LSP-pylsp:     result = method(self, *args, **kwargs)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\inference\compiled\subprocess\__init__.py", line 193, in _get_process
LSP-pylsp:     process = _GeneralizedPopen(
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\inference\compiled\subprocess\__init__.py", line 43, in _GeneralizedPopen
LSP-pylsp:     return subprocess.Popen(*args, **kwargs)
LSP-pylsp:   File "C:\ProgramData\pyenv\pyenv-win\versions\3.9.6\lib\subprocess.py", line 951, in __init__
LSP-pylsp:     self._execute_child(args, executable, preexec_fn, close_fds,
LSP-pylsp:   File "C:\ProgramData\pyenv\pyenv-win\versions\3.9.6\lib\subprocess.py", line 1420, in _execute_child
LSP-pylsp:     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
LSP-pylsp: OSError: [WinError 87] The parameter is incorrect
LSP-pylsp: 
LSP-pylsp: During handling of the above exception, another exception occurred:
LSP-pylsp: 
LSP-pylsp: Traceback (most recent call last):
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp_jsonrpc\endpoint.py", line 116, in consume
LSP-pylsp:     self._handle_request(message['id'], message['method'], message.get('params'))
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp_jsonrpc\endpoint.py", line 185, in _handle_request
LSP-pylsp:     handler_result = handler(params)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp_jsonrpc\dispatchers.py", line 25, in handler
LSP-pylsp:     return method(**(params or {}))
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\python_lsp.py", line 356, in m_text_document__document_highlight
LSP-pylsp:     return self.highlight(textDocument['uri'], position)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\python_lsp.py", line 287, in highlight
LSP-pylsp:     return flatten(self._hook('pylsp_document_highlight', doc_uri, position=position)) or None
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\python_lsp.py", line 156, in _hook
LSP-pylsp:     return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\hooks.py", line 286, in __call__
LSP-pylsp:     return self._hookexec(self, self.get_hookimpls(), kwargs)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
LSP-pylsp:     return self._inner_hookexec(hook, methods, kwargs)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\manager.py", line 337, in traced_hookexec
LSP-pylsp:     return outcome.get_result()
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\callers.py", line 80, in get_result
LSP-pylsp:     raise ex[1].with_traceback(ex[2])
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\callers.py", line 52, in from_call
LSP-pylsp:     result = func()
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\manager.py", line 335, in <lambda>
LSP-pylsp:     outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\manager.py", line 84, in <lambda>
LSP-pylsp:     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\callers.py", line 208, in _multicall
LSP-pylsp:     return outcome.get_result()
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\callers.py", line 80, in get_result
LSP-pylsp:     raise ex[1].with_traceback(ex[2])
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pluggy\callers.py", line 187, in _multicall
LSP-pylsp:     res = hook_impl.function(*args)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\plugins\highlight.py", line 13, in pylsp_document_highlight
LSP-pylsp:     usages = document.jedi_script().get_references(**code_position)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\workspace.py", line 27, in wrapper
LSP-pylsp:     return method(self, *args, **kwargs)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\workspace.py", line 265, in jedi_script
LSP-pylsp:     environment = self.get_enviroment(environment_path, env_vars=env_vars) if environment_path else None
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\pylsp\workspace.py", line 294, in get_enviroment
LSP-pylsp:     environment = jedi.api.environment.create_environment(path=environment_path,
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\api\environment.py", line 367, in create_environment
LSP-pylsp:     return Environment(_get_executable_path(path, safe=safe), env_vars=env_vars)
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\api\environment.py", line 68, in __init__
LSP-pylsp:     self._get_subprocess()
LSP-pylsp:   File "<sublime path>\data\package storage\lsp-pylsp\lib\site-packages\jedi\api\environment.py", line 79, in _get_subprocess
LSP-pylsp:     raise InvalidPythonEnvironment(
LSP-pylsp: jedi.api.environment.InvalidPythonEnvironment: Could not get version information for '.\\venv\\Scripts\\python.exe': OSError(22, 'The parameter is incorrect', None, 87, None)
:: <~~ LSP-pylsp 219: {'message': "jedi.api.environment.InvalidPythonEnvironment: Could not get version information for '.\\\\venv\\\\Scripts\\\\python.exe': OSError(22, 'The parameter is incorrect', None, 87, None)", 'code': -32602, 'data': {'traceback': ['  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp_jsonrpc\\endpoint.py", line 116, in consume\n    self._handle_request(message[\'id\'], message[\'method\'], message.get(\'params\'))\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp_jsonrpc\\endpoint.py", line 185, in _handle_request\n    handler_result = handler(params)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp_jsonrpc\\dispatchers.py", line 25, in handler\n    return method(**(params or {}))\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\python_lsp.py", line 356, in m_text_document__document_highlight\n    return self.highlight(textDocument[\'uri\'], position)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\python_lsp.py", line 287, in highlight\n    return flatten(self._hook(\'pylsp_document_highlight\', doc_uri, position=position)) or None\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\python_lsp.py", line 156, in _hook\n    return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\hooks.py", line 286, in __call__\n    return self._hookexec(self, self.get_hookimpls(), kwargs)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\manager.py", line 93, in _hookexec\n    return self._inner_hookexec(hook, methods, kwargs)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\manager.py", line 337, in traced_hookexec\n    return outcome.get_result()\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\callers.py", line 80, in get_result\n    raise ex[1].with_traceback(ex[2])\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\callers.py", line 52, in from_call\n    result = func()\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\manager.py", line 335, in <lambda>\n    outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\manager.py", line 84, in <lambda>\n    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\callers.py", line 208, in _multicall\n    return outcome.get_result()\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\callers.py", line 80, in get_result\n    raise ex[1].with_traceback(ex[2])\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pluggy\\callers.py", line 187, in _multicall\n    res = hook_impl.function(*args)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\plugins\\highlight.py", line 13, in pylsp_document_highlight\n    usages = document.jedi_script().get_references(**code_position)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\workspace.py", line 27, in wrapper\n    return method(self, *args, **kwargs)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\workspace.py", line 265, in jedi_script\n    environment = self.get_enviroment(environment_path, env_vars=env_vars) if environment_path else None\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\pylsp\\workspace.py", line 294, in get_enviroment\n    environment = jedi.api.environment.create_environment(path=environment_path,\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\jedi\\api\\environment.py", line 367, in create_environment\n    return Environment(_get_executable_path(path, safe=safe), env_vars=env_vars)\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\jedi\\api\\environment.py", line 68, in __init__\n    self._get_subprocess()\n', '  File "<sublime path>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\jedi\\api\\environment.py", line 79, in _get_subprocess\n    raise InvalidPythonEnvironment(\n']}}
:: <<< LSP-pylsp 220: []

It's looking for .\\venv\\Scripts\\python.exe. Is the path specified in pylsp.plugins.jedi.environment correct? Where is python.exe actually located within your venv?

It's also a possibility that it's not able to resolve a relative path to the project's root but first let's verify where the python.exe actually is.

Yes, should have mentioned. Double checked everything path wise. Even went as far as hard coding paths, adding debug messages, changing slashes and relative path.

From the command line in the root of the project (from which I also open Sublime Text) I can run the following command:

.\\venv\\Scripts\\python.exe "<path sublime>\\data\\package storage\\lsp-pylsp\\lib\\site-packages\\jedi\\inference\\compiled\\subprocess\\__main__.py" '<path sublime>\\data\\package storage\\lsp-pylsp\\lib\\site-packages' '3.9.6'

This is the same command as jedi runs (with some modifications on the runtime properties). However, when running it this way, I get no errors (just a blank screen of the sub process waiting for communication I guess).

I suppose specifying absolute path works?

Mmm, I should really be more pedantic in my tickets...

No, no matter how I specify paths, executable, slashes, relative, absolute, I cannot get pylsps to work. Because the The parameter is incorrect , I suspect the main executable (.\venv\Scripts\python.exe) is "working". At first I thought the arguments passed to python.exe were wrong. However, as mentioned in my comment above, that seems to be fine.

So maybe the way the subprocess is created (stdin, buffer size, creation parameters, etc) is wrong. But that I believe is something on the JEDI part.

Any other thoughts that could be the problem?

EDIT:
Nop do not see any reasons on JEDI side (they did not make any changes). Maybe the Python LSP Server v1.2.0 introduced some changes that do not work in my environment? Do you know if there is any quick way to test an older version of Python LSP Server?

Perhaps the actual error is the key error at the top? Can you try removing that cache? Alternatively try deleting the whole LSP-pylsp dir in \package storage\ so it reinstalls.

I think disabling LSP-pylsp and re-enabling it (as ST package) also removes those dirs.

Removed Package Storage/LSP-pylsp to trigger new LSP-pylsp installing message. Same error....

You mentioned JEDI cache error (good catch), purged directories:

Thought that would solve it, alas, same error...

Lots of debugging later I've figured out that subprocess.Popen crashes on Windows when passed empty dict for the env parameter (env={}).

So you need to override the:

"pylsp.plugins.jedi.env_vars": {},

setting to:

"pylsp.plugins.jedi.env_vars": null,

until this is fixed in this package.

Nice work figuring that out 🥇! That indeed fixes the problem.

Thanks a lot for the quick debugging.