-
-
Notifications
You must be signed in to change notification settings - Fork 640
feat(gazelle): For package mode, resolve dependencies when imports are relative to the package path #2865
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
Merged
feat(gazelle): For package mode, resolve dependencies when imports are relative to the package path #2865
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df9b83a
Resolve dependencies when imports are relative to the package path
yushan8 38103ef
update
yushan8 847fdae
Address comments and add directive
yushan8 247547b
Merge branch 'main' into resolve-rel-imports
aignas c9331e3
Merge branch 'main' into resolve-rel-imports
dougthor42 332e008
Update gazelle/README.md
dougthor42 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 was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/relative_imports_package_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,2 @@ | ||
| # gazelle:python_generation_mode package | ||
| # gazelle:experimental_allow_relative_imports true |
15 changes: 15 additions & 0 deletions
15
gazelle/python/testdata/relative_imports_package_mode/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,15 @@ | ||
| load("@rules_python//python:defs.bzl", "py_binary") | ||
|
|
||
| # gazelle:python_generation_mode package | ||
| # gazelle:experimental_allow_relative_imports true | ||
|
|
||
| py_binary( | ||
| name = "relative_imports_package_mode_bin", | ||
| srcs = ["__main__.py"], | ||
| main = "__main__.py", | ||
| visibility = ["//:__subpackages__"], | ||
| deps = [ | ||
| "//package1", | ||
| "//package2", | ||
| ], | ||
| ) |
6 changes: 6 additions & 0 deletions
6
gazelle/python/testdata/relative_imports_package_mode/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,6 @@ | ||
| # Resolve deps for relative imports | ||
|
|
||
| This test case verifies that the generated targets correctly handle relative imports in | ||
| Python. Specifically, when the Python generation mode is set to "package," it ensures | ||
| that relative import statements such as from .foo import X are properly resolved to | ||
| their corresponding modules. |
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
gazelle/python/testdata/relative_imports_package_mode/__main__.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 package1.module1 import function1 | ||
| from package2.module3 import function3 | ||
|
|
||
| print(function1()) | ||
| print(function3()) |
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
gazelle/python/testdata/relative_imports_package_mode/package1/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,11 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "package1", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "module1.py", | ||
| "module2.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/relative_imports_package_mode/package1/__init__.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 @@ | ||
| def some_function(): | ||
| pass |
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
gazelle/python/testdata/relative_imports_package_mode/package1/my_library/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,7 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "my_library", | ||
| srcs = ["__init__.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
7 changes: 7 additions & 0 deletions
7
gazelle/python/testdata/relative_imports_package_mode/package1/my_library/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,7 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "my_library", | ||
| srcs = ["__init__.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/relative_imports_package_mode/package1/my_library/__init__.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 @@ | ||
| def some_function(): | ||
| return "some_function" |
Empty file.
7 changes: 7 additions & 0 deletions
7
gazelle/python/testdata/relative_imports_package_mode/package1/my_library/foo/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,7 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "foo", | ||
| srcs = ["__init__.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/relative_imports_package_mode/package1/my_library/foo/__init__.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 @@ | ||
| def some_function(): | ||
| return "some_function" |
10 changes: 10 additions & 0 deletions
10
gazelle/python/testdata/relative_imports_package_mode/package1/subpackage1/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,10 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "subpackage1", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "some_module.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
10 changes: 10 additions & 0 deletions
10
gazelle/python/testdata/relative_imports_package_mode/package1/subpackage1/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,10 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "subpackage1", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "some_module.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/relative_imports_package_mode/package1/subpackage1/__init__.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,3 @@ | ||
|
|
||
| def some_init(): | ||
| return "some_init" |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/relative_imports_package_mode/package1/subpackage1/some_module.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,3 @@ | ||
|
|
||
| def some_function(): | ||
| return "some_function" |
Empty file.
16 changes: 16 additions & 0 deletions
16
.../python/testdata/relative_imports_package_mode/package1/subpackage1/subpackage2/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,16 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "subpackage2", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "script.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| deps = [ | ||
| "//package1/my_library", | ||
| "//package1/my_library/foo", | ||
| "//package1/subpackage1", | ||
| "//package1/subpackage1/subpackage2/library", | ||
| ], | ||
| ) |
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions
7
...testdata/relative_imports_package_mode/package1/subpackage1/subpackage2/library/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,7 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "library", | ||
| srcs = ["other_module.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
Empty file.
11 changes: 11 additions & 0 deletions
11
.../python/testdata/relative_imports_package_mode/package1/subpackage1/subpackage2/script.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,11 @@ | ||
| from ...my_library import ( | ||
| some_function, | ||
| ) # Import path should be package1.my_library.some_function | ||
| from ...my_library.foo import ( | ||
| some_function, | ||
| ) # Import path should be package1.my_library.foo.some_function | ||
| from .library import ( | ||
| other_module, | ||
| ) # Import path should be package1.subpackage1.subpackage2.library.other_module | ||
| from .. import some_module # Import path should be package1.subpackage1.some_module | ||
| from .. import some_function # Import path should be package1.subpackage1.some_function |
Empty file.
12 changes: 12 additions & 0 deletions
12
gazelle/python/testdata/relative_imports_package_mode/package2/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,12 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "package2", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "module3.py", | ||
| "module4.py", | ||
| ], | ||
| visibility = ["//:__subpackages__"], | ||
| deps = ["//package2/library"], | ||
| ) |
20 changes: 20 additions & 0 deletions
20
gazelle/python/testdata/relative_imports_package_mode/package2/__init__.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,20 @@ | ||
| from .library import add as _add | ||
| from .library import divide as _divide | ||
| from .library import multiply as _multiply | ||
| from .library import subtract as _subtract | ||
|
|
||
|
|
||
| def add(a, b): | ||
| return _add(a, b) | ||
|
|
||
|
|
||
| def divide(a, b): | ||
| return _divide(a, b) | ||
|
|
||
|
|
||
| def multiply(a, b): | ||
| return _multiply(a, b) | ||
|
|
||
|
|
||
| def subtract(a, b): | ||
| return _subtract(a, b) |
Empty file.
7 changes: 7 additions & 0 deletions
7
gazelle/python/testdata/relative_imports_package_mode/package2/library/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,7 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "library", | ||
| srcs = ["__init__.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
14 changes: 14 additions & 0 deletions
14
gazelle/python/testdata/relative_imports_package_mode/package2/library/__init__.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,14 @@ | ||
| def add(a, b): | ||
| return a + b | ||
|
|
||
|
|
||
| def divide(a, b): | ||
| return a / b | ||
|
|
||
|
|
||
| def multiply(a, b): | ||
| return a * b | ||
|
|
||
|
|
||
| def subtract(a, b): | ||
| return a - b |
5 changes: 5 additions & 0 deletions
5
gazelle/python/testdata/relative_imports_package_mode/package2/module3.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 .library import function5 | ||
|
|
||
|
|
||
| def function3(): | ||
| return "function3 " + function5() |
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.