Skip to content

Commit 3af83fe

Browse files
mrclaryRyan Claryccordoba12
authored
Allow passing explicit environment variables to Jedi environment (#822)
Co-authored-by: Ryan Clary <[email protected]> Co-authored-by: Carlos Cordoba <[email protected]>
1 parent d81c7ba commit 3af83fe

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

pyls/workspace.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,22 @@ def jedi_names(self, all_scopes=False, definitions=True, references=False):
226226
def jedi_script(self, position=None):
227227
extra_paths = []
228228
environment_path = None
229+
env_vars = None
229230

230231
if self._config:
231232
jedi_settings = self._config.plugin_settings('jedi', document_path=self.path)
232233
environment_path = jedi_settings.get('environment')
233234
extra_paths = jedi_settings.get('extra_paths') or []
235+
env_vars = jedi_settings.get('env_vars')
234236

235-
environment = self.get_enviroment(environment_path) if environment_path else None
236-
sys_path = self.sys_path(environment_path) + extra_paths
237+
# Drop PYTHONPATH from env_vars before creating the environment because that makes
238+
# Jedi throw an error.
239+
if env_vars is None:
240+
env_vars = os.environ.copy()
241+
env_vars.pop('PYTHONPATH', None)
242+
243+
environment = self.get_enviroment(environment_path, env_vars=env_vars) if environment_path else None
244+
sys_path = self.sys_path(environment_path, env_vars=env_vars) + extra_paths
237245
project_path = self._workspace.root_path
238246

239247
kwargs = {
@@ -249,22 +257,25 @@ def jedi_script(self, position=None):
249257

250258
return jedi.Script(**kwargs)
251259

252-
def get_enviroment(self, environment_path=None):
260+
def get_enviroment(self, environment_path=None, env_vars=None):
253261
# TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
254262
if environment_path is None:
255263
environment = jedi.api.environment.get_cached_default_environment()
256264
else:
257265
if environment_path in self._workspace._environments:
258266
environment = self._workspace._environments[environment_path]
259267
else:
260-
environment = jedi.api.environment.create_environment(path=environment_path, safe=False)
268+
environment = jedi.api.environment.create_environment(path=environment_path,
269+
safe=False,
270+
env_vars=env_vars)
261271
self._workspace._environments[environment_path] = environment
262272

263273
return environment
264274

265-
def sys_path(self, environment_path=None):
275+
def sys_path(self, environment_path=None, env_vars=None):
266276
# Copy our extra sys path
277+
# TODO: when safe to break API, use env_vars explicitly to pass to create_environment
267278
path = list(self._extra_sys_path)
268-
environment = self.get_enviroment(environment_path=environment_path)
279+
environment = self.get_enviroment(environment_path=environment_path, env_vars=env_vars)
269280
path.extend(environment.get_sys_path())
270281
return path

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'configparser; python_version<"3.0"',
1111
'future>=0.14.0; python_version<"3"',
1212
'backports.functools_lru_cache; python_version<"3.2"',
13-
'jedi>=0.17.0,<0.18.0',
13+
'jedi>=0.17.2,<0.18.0',
1414
'python-jsonrpc-server>=0.4.0',
1515
'pluggy',
1616
'ujson<=2.0.3 ; platform_system!="Windows" and python_version<"3.0"',

vscode-client/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
"default": [],
4141
"description": "Define extra paths for jedi.Script."
4242
},
43+
"pyls.plugins.jedi.env_vars": {
44+
"type": "dictionary",
45+
"default": null,
46+
"description": "Define environment variables for jedi.Script and Jedi.names."
47+
},
4348
"pyls.plugins.jedi.environment": {
4449
"type": "string",
4550
"default": null,

0 commit comments

Comments
 (0)