feat(gazelle): For package mode, resolve dependencies when imports are relative to the package path (#2865)
When `# gazelle:python_generation_mode package` is enabled, relative
imports are currently not being added to the `deps` field of the
generated target.
For example, given the following Python code:
```
from .library import add as _add
from .library import divide as _divide
from .library import multiply as _multiply
from .library import subtract as _subtract
```
The expected py_library rule should include a dependency on the local
library package:
```
py_library(
name = "py_default_library",
srcs = ["__init__.py"],
visibility = ["//visibility:public"],
deps = [
"//example/library:py_default_library",
],
)
```
However, the actual generated rule is missing the deps entry:
```
py_library(
name = "py_default_library",
srcs = ["__init__.py"],
visibility = ["//visibility:public"],
)
```
This change updates file_parser.go to ensure that relative imports
(those starting with a .) are parsed and preserved. In `Resolve()`,
logic is added to correctly interpret relative paths:
A single dot (.) refers to the current package.
Multiple dots (.., ..., etc.) traverse up parent directories.
The relative import is resolved against the current label.Pkg path that
imports the module and converted into an path relative to the root
before dependency resolution.
As a result, dependencies for relative imports are now correctly added
to the deps field in package generation mode.
Added a directive `# gazelle:experimental_allow_relative_imports true`
to allow this feature to be opt in.
---------
Co-authored-by: yushan <yushan@uber.com>
Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
Co-authored-by: Douglas Thor <dougthor42@users.noreply.github.com>This repository is the home of the core Python rules -- py_library, py_binary, py_test, py_proto_library, and related symbols that provide the basis for Python support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.
Documentation for rules_python is at https://rules-python.readthedocs.io and in the Bazel Build Encyclopedia.
Examples live in the examples directory.
The core rules are stable. Their implementation is subject to Bazel's backward compatibility policy. This repository aims to follow semantic versioning.
The Bazel community maintains this repository. Neither Google nor the Bazel team provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See How to contribute page for information on our development workflow.
For detailed documentation, see https://rules-python.readthedocs.io
See Bzlmod support for more details.