Go to declaration opens cached file

Answered

Hello,
Every time I ctrl + click on a method/class, Pycharm opens a cached file. 
The path is something like this:

/home/<username>/.cache/JetBrains/PyCharm2021.3/remote_sources/-2110645019/-354516469/<path/to/file.py>`

Changes to files are not detected or re-cached. This causes Pycharm to think a method doesn't exist in a file when it actually does, it's just the cache that is stale. To fix this you have to re-start Pycharm which is pretty annoying to do every time you change a file. 

I was able to reproduce the same error in the latest version 2022.3. I confirmed with other devs in my team and we all have the same issue. 

Is there any configuration that can completely invalidate the cache and force it to use the actual file?

0
5 comments

I was able to fix this issue by adding going to File -> Settings -> Project <name of the project> -> Project structure and right-clicking on my project folder and selecting "Sources".

That, however, added a new problem. Now whenever I run tests with the debugger, I can't find some internal libraries that have the same name as my project. 

let's say my project is called foo and I use a library called authentication that is published by my company foo:

from foo.domain.models import User # Class inside foo/domains/models.py
from foo.authentication import JwtDecoder # Class inside the foo-authentication library

This works fine when running the code in a docker container but when running it with the pycharm it raises a ModuleNotFoundError: No module named 'foo.authentication'. It seems that adding the project structure from Pycharm is somehow not allowing python's default behavior to look at other paths before raising a ModuleNotFoundError. Instead, if the module is not found under my projects it'll raise.

Any tips for this issue?

0

PyCharm2021.3/remote_sources directory stores a local copy of your remote interpreter (for performance reasons). The issue you are experiencing may appear if you install your project in development mode to your interpreter or with `pip install -e`.

We have a ticket about that in our issue tracker https://youtrack.jetbrains.com/issue/PY-25868/Cant-go-to-declaration-when-using-remote-interpreter-source-and-e-flag

Marking all folders as sources root recursively may help https://youtrack.jetbrains.com/issue/PY-25868/Cant-go-to-declaration-when-using-remote-interpreter-source-and-e-flag#focus=Comments-27-2432104.0-0

0

Setting the folders as sources root doesn't fix it for me. 
I am installing my packages with poetry but I am not setting them as editable. 

The weird thing is that any other library works. The only ones that can't be resolved are the ones that have the same name as the project. 

The issue you linked was created 5 years ago, it has priority as "Major" and is still open... I assume this will never be solved. Any other guesses? 


0

Sergey Karpov
I uploaded a video with the issues we're having. It has 2 videos.

Here's the upload id 2023_02_10_tavy6DoivCtPS9M6UZrhPd 

0

Awesome, thank you for the videos! I filed a bug about the environment variables getting lost https://youtrack.jetbrains.com/issue/PY-58989/Docker-Compose-interpreter-Environment-Variables-are-lost-after-IDE-restart, please vote for it and feel free to comment. I put the workaround in the issue description.

About the second issue. Does restarting PyCharm help in resolving the new module that you created? If so, try using this action from Search Everywhere (double Shift) next time instead:
And regarding the issue with resolving the library and the package inside the project. Do you have anything in those __init__.py files? If not, removing them (to make a namespace package) should solve the problem.

0

Please sign in to leave a comment.