Open JamesHutchison opened 1 year ago
I'm unable to reproduce this. Here's what I'm doing.
"python.analysis.logLevel": "Trace"
test.py
in workspace root.my_module
folder.something.py
in my_module
folderthing = 1
in something.py
from my_module.something import thing
in test.py
.Result: No errors. Also, as soon as I create something.py
there's a lot of Pylance log output ending with:
2023-05-15 10:22:36.414 [info] (46720) Indexing Done: c:\temp\pylance4359\my_module\something.py
Are you able to reproduce this starting from a new, empty folder as I did above? Or does it only repro in a larger project?
A log would also be useful. Please collect one as described under "Filing an issue" in the troubleshooting guide?
I'll see if I can get a dev container config that'll reproduce the issue. Since I have similar set-ups that don't have the issue I'm guessing it's something specific that needs to happen to get it into a bad state.
Alright, here's the logs:
Here's a devcontainer.json
file I used to reproduce the issue
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Debug PyLance Issue",
"image": "mcr.microsoft.com/vscode/devcontainers/python:0-3.11-bullseye",
// "build": {
// "dockerfile": "Dockerfile"
// },
// "postCreateCommand": "bash .devcontainer/postCreateCommand.sh",
// "postStartCommand": "bash .devcontainer/postStartCommand.sh",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
// "image": "mcr.microsoft.com/devcontainers/python:0-3.11",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"tamasfe.even-better-toml",
"DanielSanMedium.dscodegpt",
"njpwerner.autodocstring",
"GitHub.copilot",
"mhutchie.git-graph",
"eamodio.gitlens",
"charliermarsh.ruff",
"ms-azuretools.vscode-docker"
],
"settings": {
"python.defaultInterpreterPath": "./.venv/bin/python",
"python.formatting.provider": "black",
"python.linting.mypyEnabled": true,
"python.linting.mypyPath": "./.venv/bin/dmypy",
"python.linting.mypyArgs": [
"run",
"--",
"--ignore-missing-imports",
"--show-column-numbers",
"--no-pretty",
"--python-executable",
"./.venv/bin/python"
],
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.analysis.autoImportCompletions": true,
"python.analysis.indexing": true,
"python.linting.flake8Enabled": false,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"autoDocstring.customTemplatePath": ".vscode/autodocstring.mustache",
"ruff.path": [
"./.venv/bin/ruff"
]
},
"git.branchProtection": [
"main",
"master"
]
}
},
"features": {
"ghcr.io/devcontainers-contrib/features/poetry:2": {
"version": "latest"
},
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},
// mount the .venv directory as a named volume for performance reasons
"mounts": [
"source=${localWorkspaceFolderBasename}-venv,target=${containerWorkspaceFolder}/.venv,type=volume"
],
"workspaceFolder": "/workspaces/debug-pylance-issue"
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
Here was my actions:
my_project
directory__init__.py
something.py
with class Foo: pass
from something import Foo
in __init__.py
- issue is NOT presentmy_project/nested
my_project/nested/__init__.py
def thing(): pass
in that filemain.py
main.py
, add from nested import thing
<-- issue IS presentanother.py
under my_project
def other(): pass
to another.py
from another import other
in __init__.py
<-- issue IS presentThus
and see if that triggers anything. It doesn't.Note that using relative or absolute imports doesn't seem to matter. For example, from my_project.created_for_trace import a_thing
demonstrates the issue.
One more screen. Seems to be indexing here but still flagging the file.
2023-05-15 22:40:52.583 [info] [Info - 10:40:52 PM] (860) Skipping '/usr/local/lib/python311.zip' because it is not a valid directory
Not sure if it's connected at all, but this line in your log looks like https://github.com/microsoft/pylance-release/issues/4357
If I resolve those messages by creating the directories in question, I still see the issue
Although we're adding a watcher for the dev container's root directory we're not receiving fs events when files are added, so the file system cache (_cachedEntriesForPath
) in ImportResolver
is not cleared when new files are added, and this prevents the "Going up the given folder one by one until we can resolve the import" logic in resolveImportInternal
from finding the new file (something.py
).
Rolling back @yarnpkg/fslib
to 2.10.1
didn't help. This is not a recent regression -- it repros on 2023.4.11.
I've noticed an issue where I'm having to manually refresh tests (even though I've enabled autoTestDiscoverOnSaveEnabled
) or manually refresh git because it doesn't refresh on its own in a timely fashion. I'm wondering if its all related?
VS Code's file watchers might be the common thread. Maybe there's a problem with watchers in dev containers?
Is there a way we can escalate or work around this?
If the problem is a file watcher issue with VS code, then the easiest way to get it escalated would be to create a repro that only requires file watchers.
That probably requires creating a VS code extension to demonstrate the issue. Something like this example here: https://github.com/microsoft/vscode-extension-samples/tree/main/test-provider-sample
Or something that just watches the directory and logs console output.
Assuming that reproduced the problem, that would then be used to log a VS code issue.
Environment data
This is running in a dev container, locally
Code Snippet
Repro Steps
something.py
thing
in the example)Expected behavior
PyLance is fine and doesn't complain, because the thing in question obviously exists.
Actual behavior
PyLance complains the module doesn't exist
Logs
Rather than paste the logs, I'll just say that I can tell in the logs that the new file isn't being indexed.
Workaround
There's a couple workarounds I'm aware of.
The first workaround is to reload the window. After doing this, the import is no longer falsely flagged as missing
The second workaround is to change the
python.analysis.logLevel
. I changed it to trace while filing this report and noted that the import was suddenly no longer flagged. Perhaps changing this setting restarts the language server? Commenting out the line also seemed to fix the issue.The third workaround is to modify
python.analysis.packageIndexDepths
, add your module, and set the depth to something sufficiently deep.Possibly relevant user settings
Other history
I don't think this is a new issue. I've encountered this before in another project, and I also have a project that doesn't seem to have this problem.