You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(gazelle): Do not resolve absolute imports to sibling modules (#3106)
Currently, gazelle allows absolute imports to be resolved to sibling
modules: an `import foo` statement will resolve to a `foo.py` file in
the same folder if such file exists. This seems to be a Python 2
behavior (ie. pre-`from __future__ import absolute_import`), and doesn't
work on the current rules_python setup.
This behavior is explicitly tested in the
[siblings_import](https://github.com/bazel-contrib/rules_python/tree/cbe6d38d01c14de46d90ea717d0f2090117533fa/gazelle/python/testdata/sibling_imports)
test case. However, recreating the exact same repository layout from
this test case and running `bazel test //pkg:unit_test`, the test fails
with the import failing.
This PR adds a new directive, `gazelle:python_resolve_sibling_imports`,
to allow disabling such behavior.
The actual changes are in 3 places:
- In `gazelle/python/target.go`, the directive is added to `if
t.siblingSrcs.Contains(fileName) && fileName !=
filepath.Base(dep.Filepath)`, which is where the import is converted to
a full absolute import if it matches a sibling file;
- In `gazelle/python/generate.go`, the handling of `conftest.py` was
dependent on this behavior (ie. it added a dependency on the module
`conftest`, assuming that it would be resolved to the relative module).
That was modified to compute the full absolute module path instead.
- In `gazelle/python/resolve.go`, resolve relative imports even when
using file generation mode.
I also explicitly added `gazelle:python_resolve_sibling_imports true` to
any test that breaks if the default value of this directive is changed
to `false`.
Copy file name to clipboardExpand all lines: gazelle/README.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -228,6 +228,8 @@ Python-specific directives are as follows:
228
228
| Controls whether to generate a separate `pyi_deps` attribute for type-checking dependencies or merge them into the regular `deps` attribute. When `false` (default), type-checking dependencies are merged into `deps` for backward compatibility. When `true`, generates separate `pyi_deps`. Imports in blocks with the format `if typing.TYPE_CHECKING:`/`if TYPE_CHECKING:` and type-only stub packages (eg. boto3-stubs) are recognized as type-checking dependencies. |
| Controls whether to generate a `py_proto_library` for each `proto_library` in the package. By default we load this rule from the `@protobuf` repository; use `gazelle:map_kind` if you need to load this from somewhere else. |
0 commit comments