CoatiSoftware / SourcetrailPythonIndexer

Python Indexer for Sourcetrail based on jedi, parso and SourcetrailDB
GNU General Public License v3.0
90 stars 28 forks source link

symbol not found if package ambiguous #73

Open mbway opened 3 years ago

mbway commented 3 years ago

the codebase I'm working with is structured similarly to this. I've tried to simplify the situation as much as possible:

.
├── environment
├── src
│   ├── __init__.py
│   ├── mypackage
│   │   ├── __init__.py
│   │   └── mymodule.py
│   └── otherpackage
│       ├── helper.py
│       └── __init__.py
├── test
│   ├── __init__.py
│   └── mypackage
│       ├── __init__.py
│       └── test_mypackage.py

where environment sets PYTHONPATH like so:

THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export PYTHONPATH="${THIS_DIR}/src:${THIS_DIR}/test"

this is so that packages under src can import from other packages like from otherpackage.helper import .... By experimenting I found that Sourcetrail uses the current PYTHONPATH during scanning, so if only src is in the path then everything is OK, except that references to functions made in the test code are not discovered. However if test is included as well then any imports like

from mypackage.mymodule import do_thing

anywhere in the codebase fail with

Imported symbol named "mymodule" has not been found.

I think this is because it's ambiguous whether the import refers to src/mypackage or test/mypackage. However the python interpreter itself does not get confused because of the ordering in PYTHONPATH. It would be nice if Sourcetrail could support situations like this as well. I appreciate that fully qualifying imports or renaming the test packages would work but this would require large changes and may break in the future depending on the behaviour of IDE auto-importing etc.