pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Pylance does not set current working directory to workspace root when discovering sys.path

Open iwanb opened this issue 8 months ago • 1 comments

Tools typically run Python with the workspace root as the current working directory, that's also how python normally runs in the terminal. To discover sys.path, the pylance extension runs this Python script:

['python', '-I', '-c', 'import os, os.path, sys; normalize = lambda p: os.path.normcase(os.path.normpath(p)); cwd = normalize(os.getcwd()); orig_sys_path = [p for p in s
ys.path if p != ""]; sys.path[:] = [p for p in sys.path if p != "" and normalize(p) != cwd]; import sys, json; json.dump(tuple(sys.version_info), sys.stdout)']

with ~/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist as the current working directory.

In my project I rely on the current working directory to modify sys.path during initialization (in sitecustomize hooks). Pylance cannot find the modules which rely on those sys.path modifications.

Environment data

  • Pylance version: 2025.4.1
  • OS and version: Fedora 40
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10

Code Snippet

This is a simplification of the setup we have: https://github.com/iwanb/pylance-issue-debug https://github.com/iwanb/pylance-issue-debug/blob/main/create_venv.sh

Pylance does not find mod1 from mod2, even though running it from the venv works fine, thanks to the sitecustomize.py logic:

./.venv/bin/python -c 'import mod2'

https://github.com/iwanb/pylance-issue-debug/blob/main/mysitecustomize/sitecustomize.py#L5

Repro Steps

See code snippet.

Expected behavior

Pylance runs Python with the workspace root as current working directory to discover the current sys.path.

This is how the similar pytest discovery works with this script: ./.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist/bundled/files/get_pytest_options.py.

Alternatively, provide an environment variable with the workspace root.

Actual behavior

Pylance runs Python with ~/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist as the current working directory to discover the current sys.path.

Logs

Output of path discovery with above code snippet:

2025-04-08 14:40:28.081 [info] [Info  - 14:40:28] (575020) Assuming Python version 3.10.16.final.0
2025-04-08 14:40:28.082 [info] (575020) Assuming Python platform Linux
2025-04-08 14:40:28.120 [info] [Info  - 14:40:28] (575020) Execution environment: 3.12.9 (.venv venv)
2025-04-08 14:40:28.120 [info] [Info  - 14:40:28] (575020)   Extra paths:
2025-04-08 14:40:28.120 [info] [Info  - 14:40:28] (575020)     (none)
2025-04-08 14:40:28.120 [info] [Info  - 14:40:28] (575020)   Python version: 3.10.16.final.0
2025-04-08 14:40:28.121 [info] [Info  - 14:40:28] (575020)   Python platform: Linux
2025-04-08 14:40:28.121 [info] [Info  - 14:40:28] (575020)   Search paths:
2025-04-08 14:40:28.121 [info] [Info  - 14:40:28] (575020)     ~/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist/typeshed-fallback/stdlib
2025-04-08 14:40:28.121 [info] [Info  - 14:40:28] (575020)     ~/ws/experiments/vscodepylance
2025-04-08 14:40:28.121 [info] [Info  - 14:40:28] (575020)     ~/ws/experiments/vscodepylance/typings
2025-04-08 14:40:28.121 [info] [Info  - 14:40:28] (575020)     ~/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist/typeshed-fallback/stubs/...
2025-04-08 14:40:28.122 [info] [Info  - 14:40:28] (575020)     ~/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist/bundled/stubs
2025-04-08 14:40:28.122 [info] [Info  - 14:40:28] (575020)     /usr/lib64/python3.10
2025-04-08 14:40:28.122 [info] [Info  - 14:40:28] (575020)     /usr/lib64/python3.10/lib-dynload
2025-04-08 14:40:28.122 [info] [Info  - 14:40:28] (575020)     ~/ws/experiments/vscodepylance/.venv/lib64/python3.10/site-packages
2025-04-08 14:40:28.122 [info] [Info  - 14:40:28] (575020)     ~/ws/experiments/vscodepylance/.venv/lib/python3.10/site-packages

iwanb avatar Apr 08 '25 12:04 iwanb

On my system (macOS Sonoma), the cwd is actually /. VSCODE_CWD is also /, which is a little surprising.

In my case, I'm wrapping python to set some environment variables based on the working dir. I don't think many people need this behaviour, but at the same time I can't see changing the CWD to break anything.

mon avatar Sep 18 '25 00:09 mon