-
-
Notifications
You must be signed in to change notification settings - Fork 639
fix(gazelle): Do not resolve absolute imports to sibling modules #3106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dougthor42
merged 9 commits into
bazel-contrib:main
from
benchling:martani/no-sibling-import
Jul 29, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc1dbf1
feat(gazelle) Allow disabling sibling module resolution
amartani 4484c85
Add overrides for existing tests that break when directive value flips
amartani 259a5a2
Fix conftest target inclusion when disabling relative imports (by always
amartani f5d00d3
Add changelog entry
amartani e66010b
Update comments
amartani 0e49623
Merge branch 'main' into martani/no-sibling-import
amartani 9f69c62
Flip default to false
amartani 29e815a
Add more test cases to sibling_imports_disabled
amartani a00ec81
Add new test case for siblings imports disabled in file mode
amartani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| # gazelle:python_generation_mode file | ||
| # gazelle:python_resolve_sibling_imports true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # gazelle:python_resolve_sibling_imports true |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/annotation_include_pytest_conftest/with_conftest/BUILD.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # gazelle:python_library_naming_convention my_$package_name$_library | ||
| # gazelle:python_binary_naming_convention my_$package_name$_binary | ||
| # gazelle:python_test_naming_convention my_$package_name$_test | ||
| # gazelle:python_resolve_sibling_imports true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| # Sibling imports | ||
|
|
||
| This test case asserts that imports from sibling modules are resolved correctly. It covers 3 different types of imports in `pkg/unit_test.py` | ||
| This test case asserts that imports from sibling modules are resolved correctly | ||
| when the `python_resolve_sibling_imports` directive is enabled (default | ||
| behavior). It covers 3 different types of imports in `pkg/unit_test.py`: | ||
|
|
||
| - `import a` - resolves to the sibling `a.py` in the same package | ||
| - `import test_util` - resolves to the sibling `test_util.py` in the same | ||
| package | ||
| - `from b import run` - resolves to the sibling `b.py` in the same package | ||
|
|
||
| When sibling imports are enabled, we allow them to be satisfied by sibling | ||
| modules (ie. modules in the same package). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # gazelle:python_resolve_sibling_imports true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # gazelle:python_resolve_sibling_imports false | ||
| # gazelle:experimental_allow_relative_imports true |
18 changes: 18 additions & 0 deletions
18
gazelle/python/testdata/sibling_imports_disabled/BUILD.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
|
||
| # gazelle:python_resolve_sibling_imports false | ||
| # gazelle:experimental_allow_relative_imports true | ||
|
|
||
| py_library( | ||
| name = "sibling_imports_disabled", | ||
| srcs = [ | ||
| "a.py", | ||
| "b.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "test_util", | ||
| srcs = ["test_util.py"], | ||
| ) |
22 changes: 22 additions & 0 deletions
22
gazelle/python/testdata/sibling_imports_disabled/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Sibling imports disabled | ||
|
|
||
| This test case asserts that imports from sibling modules are NOT resolved as | ||
| absolute imports when the `python_resolve_sibling_imports` directive is | ||
| disabled. It covers different types of imports in `pkg/unit_test.py`: | ||
|
|
||
| - `import a` - resolves to the root-level `a.py` instead of the sibling | ||
| `pkg/a.py` | ||
| - `from typing import Iterable` - resolves to the stdlib `typing` module | ||
| (not the sibling `typing.py`). | ||
| - `from .b import run` / `from .typing import A` - resolves to the sibling | ||
| `pkg/b.py` / `pkg/typing.py` (with | ||
| `gazelle:experimental_allow_relative_imports` enabled) | ||
| - `import test_util` - resolves to the root-level `test_util.py` instead of | ||
| the sibling `pkg/test_util.py` | ||
| - `from b import run` - resolves to the root-level `b.py` instead of the | ||
| sibling `pkg/b.py` | ||
|
|
||
| When sibling imports are disabled with | ||
| `# gazelle:python_resolve_sibling_imports false`, the imports remain as-is | ||
| and follow standard Python resolution rules where absolute imports can't refer | ||
| to sibling modules. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # This is a Bazel workspace for the Gazelle test data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Root level a.py file for testing disabled sibling imports |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Root level b.py file for testing disabled sibling imports | ||
| def run(): | ||
| pass |
Empty file.
27 changes: 27 additions & 0 deletions
27
gazelle/python/testdata/sibling_imports_disabled/pkg/BUILD.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
|
||
| py_library( | ||
| name = "pkg", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "a.py", | ||
| "b.py", | ||
| "typing.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "test_util", | ||
| srcs = ["test_util.py"], | ||
| deps = [":pkg"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "unit_test", | ||
| srcs = ["unit_test.py"], | ||
| deps = [ | ||
| "//:sibling_imports_disabled", | ||
| "//:test_util", | ||
| ], | ||
| ) |
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def run(): | ||
| pass |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/sibling_imports_disabled/pkg/test_util.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from .b import run | ||
| from .typing import A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| A = 1 |
5 changes: 5 additions & 0 deletions
5
gazelle/python/testdata/sibling_imports_disabled/pkg/unit_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from typing import Iterable | ||
|
|
||
| import a | ||
| import test_util | ||
| from b import run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Root level test_util.py file for testing disabled sibling imports |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/sibling_imports_disabled_file_mode/BUILD.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # gazelle:python_generation_mode file | ||
| # gazelle:python_resolve_sibling_imports false | ||
| # gazelle:experimental_allow_relative_imports true |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.