feat(gazelle): Allow disabling sibling module resolution #3106
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, gazelle allows absolute imports to be resolved to sibling modules: an
import foo
statement will resolve to afoo.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 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 2 places:
gazelle/python/target.go
, the directive is added toif 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;gazelle/python/generate.go
, the handling ofconftest.py
was dependent on this behavior (ie. it added a dependency on the moduleconftest
, assuming that it would be resolved to the relative module). That was modified to compute the full absolute module path instead.I also explicitly added
gazelle:python_resolve_sibling_imports true
to any test that breaks if the default value of this directive is changed tofalse
.