microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.67k stars 770 forks source link

Pylance reports errors in `.venv` despite being listed in `exclude` #6044

Closed simensol closed 1 week ago

simensol commented 1 week ago

Environment data

Code Snippet

I have tried to exclude .venv from both settings.json and pyproject.toml.

/workspaces/backend/.vscode/settings.json

{
    ...
    "python.analysis.exclude": ["**/.venv/**"],
    ...
}

/workspaces/backend/pyproject.toml:

[tool.pyright]
pythonVersion = "3.11"
pythonPlatform = "Linux"
typeCheckingMode = "strict"
exclude = [
    ".venv/",
    "**/migrations",
    "**/node_modules",
]
stubPath = "typings"
...

Expected behavior

Errors should not be shown for Poetry’s virtual environment in /workspaces/backend/.venv.

Actual behavior

Many errors are reported from Poetry’s virtual environment in /workspaces/backend/.venv. Sometimes restarting the language server fixes the issue, while other times reloading the VSCode window is necessary. This issue did not occur previously, but I do not recall when it started happening.

Two examples from the same code base:

backend backend

Logs

Top of verbose logs:

2024-06-21 04:43:53.560 [info] [Info  - 4:43:53 AM] (61801) Received fs event 'change' for config file
2024-06-21 04:43:53.560 [info] [Info  - 4:43:53 AM] (61801) SourceFile: Received fs event 'change' for path '/workspaces/backend/pyproject.toml'
2024-06-21 04:43:53.661 [info] [Info  - 4:43:53 AM] (61801) Reloading configuration file at /workspaces/backend/pyproject.toml
2024-06-21 04:43:53.661 [info] [Info  - 4:43:53 AM] (61801) Setting pythonPath for service "backend": "/workspaces/backend/.venv/bin/python"
2024-06-21 04:43:53.661 [info] [Info  - 4:43:53 AM] (61801) Setting environmentName for service "backend": "3.11.7 (.venv venv)"
2024-06-21 04:43:53.661 [info] [Info  - 4:43:53 AM] (61801) Loading pyproject.toml file at /workspaces/backend/pyproject.toml
2024-06-21 04:43:53.662 [info] [Warn  - 4:43:53 AM] (61801) The useLibraryCodeForTypes has been specified in both the config file and the client settings. The value in the config file (true) will take precedence
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801) Search paths for file:///workspaces/backend
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /root/.vscode-server/extensions/ms-python.vscode-pylance-2024.6.1/dist/typeshed-fallback/stdlib
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/typings
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /root/.vscode-server/extensions/ms-python.vscode-pylance-2024.6.1/dist/typeshed-fallback/stubs/...
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /root/.vscode-server/extensions/ms-python.vscode-pylance-2024.6.1/dist/bundled/stubs
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /usr/local/lib/python3.11
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /usr/local/lib/python3.11/lib-dynload
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/.venv/lib/python3.11/site-packages
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/packages/dev_dependencies/src
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/packages/eisitools/src
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/packages/feide/src
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/packages/pylint_eisi/src
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801)   /workspaces/backend/packages/sms/src
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801) Adding fs watcher for library directories:
 file:///usr/local/lib/python3.11
file:///usr/local/lib/python3.11/lib-dynload
file:///workspaces/backend/.venv/lib/python3.11/site-packages
2024-06-21 04:43:53.946 [info] [Info  - 4:43:53 AM] (61801) Adding fs watcher for directories:
 file:///workspaces/backend
2024-06-21 04:43:54.152 [info] [Info  - 4:43:54 AM] (61801) Found 185 source files
...
debonte commented 1 week ago

You want to set ignore rather than exclude. For example:

"python.analysis.ignore": [".venv"]

exclude allows you to carve out parts of include to tell us that those parts are not really part of your project. However, if we analyze a file that imports an "excluded" file, we'll still analyze that excluded file and report diagnostics for it.

ignore on the other hand tells us that you don't want to see diagnostics for certain files.

There's more information on these settings here.