Skip to content

Commit 0d53e64

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 cabac8e commit 0d53e64

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

CONFIGURATION.md

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

pylsp/config/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@
151151
},
152152
"description": "Define extra paths for jedi.Script."
153153
},
154+
"pylsp.plugins.jedi.prioritize": {
155+
"type": "boolean",
156+
"default": false,
157+
"description": "Whether to place extra_paths at the beginning (true) or end (false) of `sys.path`"
158+
},
154159
"pylsp.plugins.jedi.env_vars": {
155160
"type": [
156161
"object",

pylsp/workspace.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ def jedi_script(self, position=None, use_document_path=False):
521521
extra_paths = []
522522
environment_path = None
523523
env_vars = None
524+
prioritize = False
524525

525526
if self._config:
526527
jedi_settings = self._config.plugin_settings(
@@ -537,19 +538,21 @@ def jedi_script(self, position=None, use_document_path=False):
537538

538539
extra_paths = jedi_settings.get("extra_paths") or []
539540
env_vars = jedi_settings.get("env_vars")
541+
prioritize = jedi_settings.get("prioritize")
540542

541-
# Drop PYTHONPATH from env_vars before creating the environment because that makes
542-
# Jedi throw an error.
543+
# Drop PYTHONPATH from env_vars before creating the environment to
544+
# ensure that Jedi can startup properly without module name collision.
543545
if env_vars is None:
544546
env_vars = os.environ.copy()
545547
env_vars.pop("PYTHONPATH", None)
546548

547-
environment = (
548-
self.get_enviroment(environment_path, env_vars=env_vars)
549-
if environment_path
550-
else None
551-
)
552-
sys_path = self.sys_path(environment_path, env_vars=env_vars) + extra_paths
549+
environment = self.get_enviroment(environment_path, env_vars=env_vars)
550+
551+
sys_path = list(self._extra_sys_path) + environment.get_sys_path()
552+
if prioritize:
553+
sys_path += extra_paths + sys_path
554+
else:
555+
sys_path += sys_path + extra_paths
553556
project_path = self._workspace.root_path
554557

555558
# Extend sys_path with document's path if requested
@@ -559,7 +562,7 @@ def jedi_script(self, position=None, use_document_path=False):
559562
kwargs = {
560563
"code": self.source,
561564
"path": self.path,
562-
"environment": environment,
565+
"environment": environment if environment_path else None,
563566
"project": jedi.Project(path=project_path, sys_path=sys_path),
564567
}
565568

@@ -585,8 +588,8 @@ def get_enviroment(self, environment_path=None, env_vars=None):
585588
return environment
586589

587590
def sys_path(self, environment_path=None, env_vars=None):
591+
# TODO: when safe to break API, remove this method.
588592
# Copy our extra sys path
589-
# TODO: when safe to break API, use env_vars explicitly to pass to create_environment
590593
path = list(self._extra_sys_path)
591594
environment = self.get_enviroment(
592595
environment_path=environment_path, env_vars=env_vars

0 commit comments

Comments
 (0)