-
-
Notifications
You must be signed in to change notification settings - Fork 591
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
Open
yushan26
wants to merge
14
commits into
bazel-contrib:main
Choose a base branch
from
yushan26:resolve-rel-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f5db7dd
Resolve dependencies when imports are relative to the package path
yushan8 58cf06b
update
yushan8 1577885
update
yushan8 ac14792
update
yushan8 aedde0a
Update
yushan8 ffd0822
update
yushan8 0b333ff
update test
yushan8 fecb2a9
update
yushan8 8914db1
update
yushan8 212323f
update
yushan8 0ba3087
Update testdata
yushan8 0e9e096
update
yushan8 59b0f49
update
yushan8 7b08030
update
yushan8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary", "py_library") | ||
load("@rules_python//python:defs.bzl", "py_binary") | ||
|
||
# gazelle:resolve py resolved_package //package2:resolved_package | ||
|
||
py_library( | ||
name = "relative_imports", | ||
srcs = [ | ||
"package1/module1.py", | ||
"package1/module2.py", | ||
], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_binary( | ||
name = "relative_imports_bin", | ||
srcs = ["__main__.py"], | ||
main = "__main__.py", | ||
visibility = ["//:__subpackages__"], | ||
deps = [ | ||
":relative_imports", | ||
"//package1", | ||
"//package2", | ||
], | ||
) |
Empty file.
10 changes: 10 additions & 0 deletions
10
gazelle/python/testdata/relative_imports/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,10 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "package1", | ||
srcs = [ | ||
"module1.py", | ||
"module2.py", | ||
], | ||
visibility = ["//:__subpackages__"], | ||
) |
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
Empty file.
8 changes: 8 additions & 0 deletions
8
gazelle/python/testdata/relative_imports/package2/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,8 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "subpackage1", | ||
srcs = ["module5.py"], | ||
visibility = ["//:__subpackages__"], | ||
deps = ["//package2"], | ||
) |
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_generation_mode package | ||
14 changes: 14 additions & 0 deletions
14
gazelle/python/testdata/resolve_deps_relative_imports/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,14 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary") | ||
|
||
# gazelle:python_generation_mode package | ||
|
||
py_binary( | ||
name = "resolve_deps_relative_imports_bin", | ||
srcs = ["__main__.py"], | ||
main = "__main__.py", | ||
visibility = ["//:__subpackages__"], | ||
deps = [ | ||
"//package1", | ||
"//package2", | ||
], | ||
) |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/resolve_deps_relative_imports/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,3 @@ | ||
# 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. |
1 change: 1 addition & 0 deletions
1
gazelle/python/testdata/resolve_deps_relative_imports/WORKSPACE
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 test data Bazel workspace. |
5 changes: 5 additions & 0 deletions
5
gazelle/python/testdata/resolve_deps_relative_imports/__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()) |
Empty file.
10 changes: 10 additions & 0 deletions
10
gazelle/python/testdata/resolve_deps_relative_imports/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,10 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "package1", | ||
srcs = [ | ||
"module1.py", | ||
"module2.py", | ||
], | ||
visibility = ["//:__subpackages__"], | ||
) |
19 changes: 19 additions & 0 deletions
19
gazelle/python/testdata/resolve_deps_relative_imports/package1/module1.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,19 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .module2 import function2 | ||
|
||
|
||
def function1(): | ||
return "function1 " + function2() |
17 changes: 17 additions & 0 deletions
17
gazelle/python/testdata/resolve_deps_relative_imports/package1/module2.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,17 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def function2(): | ||
return "function2" |
Empty file.
12 changes: 12 additions & 0 deletions
12
gazelle/python/testdata/resolve_deps_relative_imports/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/resolve_deps_relative_imports/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/resolve_deps_relative_imports/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/resolve_deps_relative_imports/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/resolve_deps_relative_imports/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() |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/resolve_deps_relative_imports/package2/module4.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 function4(): | ||
return "function4" |
15 changes: 15 additions & 0 deletions
15
gazelle/python/testdata/resolve_deps_relative_imports/test.yaml
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 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
--- |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job in covering package mode. Can we also have test cases covering file and project mode?
It seems "testdata/relative_imports" and "testdata/reslove_deps_relative_imports" are covering similar things. From the name, it's also hard to tell the difference between "testdata/relative_imports" and "testdata/reslove_deps_relative_imports"
We can either:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think 2 is preferrable? Seems more straight forward and the behavior would not change in project mode or file mode.
Is file mode also something that we want to look into supporting too? I think if we do, then implementations for relative paths in file mode should be in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can support file mode in the next PR.