Skip to content

Commit 3215ea6

Browse files
authored
Allow extra_paths to be placed in front of sys.path (#527)
1 parent 4714d38 commit 3215ea6

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

CONFIGURATION.md

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

+5
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_extra_paths": {
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

+21-10
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_extra_paths = False
524525

525526
if self._config:
526527
jedi_settings = self._config.plugin_settings(
@@ -537,19 +538,19 @@ 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_extra_paths = jedi_settings.get("prioritize_extra_paths")
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
549+
environment = self.get_enviroment(environment_path, env_vars=env_vars)
550+
sys_path = self.sys_path(
551+
environment_path, env_vars, prioritize_extra_paths, extra_paths
551552
)
552-
sys_path = self.sys_path(environment_path, env_vars=env_vars) + extra_paths
553+
553554
project_path = self._workspace.root_path
554555

555556
# Extend sys_path with document's path if requested
@@ -559,7 +560,7 @@ def jedi_script(self, position=None, use_document_path=False):
559560
kwargs = {
560561
"code": self.source,
561562
"path": self.path,
562-
"environment": environment,
563+
"environment": environment if environment_path else None,
563564
"project": jedi.Project(path=project_path, sys_path=sys_path),
564565
}
565566

@@ -584,14 +585,24 @@ def get_enviroment(self, environment_path=None, env_vars=None):
584585

585586
return environment
586587

587-
def sys_path(self, environment_path=None, env_vars=None):
588+
def sys_path(
589+
self,
590+
environment_path=None,
591+
env_vars=None,
592+
prioritize_extra_paths=False,
593+
extra_paths=[],
594+
):
588595
# Copy our extra sys path
589-
# TODO: when safe to break API, use env_vars explicitly to pass to create_environment
590596
path = list(self._extra_sys_path)
591597
environment = self.get_enviroment(
592598
environment_path=environment_path, env_vars=env_vars
593599
)
594600
path.extend(environment.get_sys_path())
601+
if prioritize_extra_paths:
602+
path += extra_paths + path
603+
else:
604+
path += path + extra_paths
605+
595606
return path
596607

597608

0 commit comments

Comments
 (0)