Open dougthor42 opened 2 months ago
Yeah, the issues you've mentioned is a real pain. That said, I think gazelle
should be able to parse the python files for symbols and have an internal
mapping somewhere of what symbols are in which targets.
To have something that works and is still experimental, you could implement it in stages:
BUILD.bazel
file in the gazelle: resolve
directive - this may be a good way to test things manually, or have
an escape hatch when the auto-detection does not work.__init__
targets.package
generation mode, at least you would not need
to handle the __init__
targets and then the semantics could be easier as
you might get away without supporting the __init__
like targets.def
and
class
and global variables. How hard could that be... famous last words...What other things would we need to have around this issue?
🚀 feature request
Relevant Rules
Description
Sometimes a python code base uses relative imports:
While I don't necessarily agree with this style, it's something that Python supports (see PEP 328) and thus I believe that Gazelle should support it too.
Right now, Gazelle simply ignores these imports. Here's an example:
If we change a.py to use an absolute import:
Gazelle will add the dep:
Describe the solution you'd like
I'm somewhat familiar with the Gazelle code base. I believe it will be pretty easy (for the most part - see Issues below) below) to generate the full target path for the current file being processed, and then adjust the target based on how many dots
.
are in the relative import.Because of the Issues, I'd imagine that this would be an experimental, opt-in feature for a while. It would be guarded by a directive:
Here are some examples of the logic, assuming a slightly more complex dir structure:
The above examples assume file-level generation. More thought will be needed to support package-level generation.
Issues
It's difficult to know if
from ..foo import bar
should be//:foo
or//foo:bar
. Isbar
a class/function? Or isbar
another module? In the example above, it's a python module, but that's not always the case.In the
from .. import baz
case, it's possible thatbaz
is an identifier defined in the parent package's__init__.py
, and thus the dep target would be//:__init__
.Describe alternatives you've considered
I tried convincing the developers to use absolute imports, but they just weren't having it :rofl:
So I also tried using the "package" generation mode, but the issue was that unit test files also used relative imports and would not include the dep.
The current workaround is to add
# gazelle:include_dep //example:b
annotations, but those are prone to diverging from the actual code and can be tedious to write for large projects.