Skip to content

Commit 172e48d

Browse files
committed
Allow extra_paths to be placed in front of sys.path.
* Add pylsp.plugins.jedi.prioritize configuration key Note: when safe to break API, Document.sys_path should be removed.
1 parent ed00eac commit 172e48d

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

CONFIGURATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
2020
| `pylsp.plugins.flake8.select` | `array` of unique `string` items | List of errors and warnings to enable. | `null` |
2121
| `pylsp.plugins.jedi.auto_import_modules` | `array` of `string` items | List of module names for jedi.settings.auto_import_modules. | `["numpy"]` |
2222
| `pylsp.plugins.jedi.extra_paths` | `array` of `string` items | Define extra paths for jedi.Script. | `[]` |
23+
| `pylsp.plugins.jedi.prioritize` | `boolean` | Whether to place extra_paths at the beginning (true) or end (false) of `sys.path` | `false` |
2324
| `pylsp.plugins.jedi.env_vars` | `object` | Define environment variables for jedi.Script and Jedi.names. | `null` |
2425
| `pylsp.plugins.jedi.environment` | `string` | Define environment for jedi.Script and Jedi.names. | `null` |
2526
| `pylsp.plugins.jedi_completion.enabled` | `boolean` | Enable or disable the plugin. | `true` |

pylsp/config/schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@
143143
},
144144
"description": "Define extra paths for jedi.Script."
145145
},
146+
"pylsp.plugins.jedi.prioritize": {
147+
"type": "boolean",
148+
"default": false,
149+
"description": "Whether to place extra_paths at the beginning (true) or end (false) of `sys.path`"
150+
},
146151
"pylsp.plugins.jedi.env_vars": {
147152
"type": [
148153
"object",
@@ -500,4 +505,4 @@
500505
"description": "The name of the folder in which rope stores project configurations and data. Pass `null` for not using such a folder at all."
501506
}
502507
}
503-
}
508+
}

pylsp/workspace.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ def jedi_script(self, position=None, use_document_path=False):
507507
extra_paths = []
508508
environment_path = None
509509
env_vars = None
510+
prioritize = False
510511

511512
if self._config:
512513
jedi_settings = self._config.plugin_settings(
@@ -523,19 +524,21 @@ def jedi_script(self, position=None, use_document_path=False):
523524

524525
extra_paths = jedi_settings.get("extra_paths") or []
525526
env_vars = jedi_settings.get("env_vars")
527+
prioritize = jedi_settings.get("prioritize")
526528

527-
# Drop PYTHONPATH from env_vars before creating the environment because that makes
528-
# Jedi throw an error.
529+
# Drop PYTHONPATH from env_vars before creating the environment to
530+
# ensure that Jedi can startup properly without module name collision.
529531
if env_vars is None:
530532
env_vars = os.environ.copy()
531533
env_vars.pop("PYTHONPATH", None)
532534

533-
environment = (
534-
self.get_enviroment(environment_path, env_vars=env_vars)
535-
if environment_path
536-
else None
537-
)
538-
sys_path = self.sys_path(environment_path, env_vars=env_vars) + extra_paths
535+
environment = self.get_enviroment(environment_path, env_vars=env_vars)
536+
537+
sys_path = list(self._extra_sys_path) + environment.get_sys_path()
538+
if prioritize:
539+
sys_path += extra_paths + sys_path
540+
else:
541+
sys_path += sys_path + extra_paths
539542
project_path = self._workspace.root_path
540543

541544
# Extend sys_path with document's path if requested
@@ -545,7 +548,7 @@ def jedi_script(self, position=None, use_document_path=False):
545548
kwargs = {
546549
"code": self.source,
547550
"path": self.path,
548-
"environment": environment,
551+
"environment": environment if environment_path else None,
549552
"project": jedi.Project(path=project_path, sys_path=sys_path),
550553
}
551554

@@ -571,8 +574,8 @@ def get_enviroment(self, environment_path=None, env_vars=None):
571574
return environment
572575

573576
def sys_path(self, environment_path=None, env_vars=None):
577+
# TODO: when safe to break API, remove this method.
574578
# Copy our extra sys path
575-
# TODO: when safe to break API, use env_vars explicitly to pass to create_environment
576579
path = list(self._extra_sys_path)
577580
environment = self.get_enviroment(
578581
environment_path=environment_path, env_vars=env_vars

0 commit comments

Comments
 (0)