Skip to content

Commit 976bcfe

Browse files
committed
Allow extra_paths to be placed in front of sys.path.
* Add pylsp.plugins.jedi.prioritize configuration key Note that Document.sys_path should be removed OR the prioritize logic moved inside this method. Either option requires breaking the API.
1 parent 5a383df commit 976bcfe

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Diff for: CONFIGURATION.md

+1
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` |

Diff for: pylsp/config/schema.json

+6-1
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+
}

Diff for: pylsp/workspace.py

+9-4
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,9 +524,10 @@ 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 because
530+
# to 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)
@@ -535,7 +537,10 @@ def jedi_script(self, position=None, use_document_path=False):
535537
if environment_path
536538
else None
537539
)
538-
sys_path = self.sys_path(environment_path, env_vars=env_vars) + extra_paths
540+
if prioritize:
541+
sys_path = extra_paths + self.sys_path(environment_path, env_vars)
542+
else:
543+
sys_path = self.sys_path(environment_path, env_vars) + extra_paths
539544
project_path = self._workspace.root_path
540545

541546
# Extend sys_path with document's path if requested
@@ -571,8 +576,8 @@ def get_enviroment(self, environment_path=None, env_vars=None):
571576
return environment
572577

573578
def sys_path(self, environment_path=None, env_vars=None):
579+
# TODO: when safe to break API, remove this method.
574580
# Copy our extra sys path
575-
# TODO: when safe to break API, use env_vars explicitly to pass to create_environment
576581
path = list(self._extra_sys_path)
577582
environment = self.get_enviroment(
578583
environment_path=environment_path, env_vars=env_vars

0 commit comments

Comments
 (0)