-
-
Notifications
You must be signed in to change notification settings - Fork 588
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
base: main
Are you sure you want to change the base?
Conversation
37c9386
to
8914db1
Compare
@@ -0,0 +1 @@ | |||
# gazelle:python_generation_mode package |
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:
- have all test cases in "testdata/relative_imports", with some packages in package mode, some in project mode and some in file mode, or
- have "testdata/relative_imports_package_mode", "testdata/relative_imports_file_mode" and "testdata/relative_imports_project_mode"
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.
When
# gazelle:python_generation_mode package
is enabled, relative imports are currently not being added to thedeps
field of the generated target.For example, given the following Python code:
The expected py_library rule should include a dependency on the local library package:
However, the actual generated rule is missing the deps entry:
This change updates file_parser.go to ensure that relative imports (those starting with a .) are parsed and preserved. In
Resolve()
, logic is added to correctly interpret relative paths:A single dot (.) refers to the current package.
Multiple dots (.., ..., etc.) traverse up parent directories.
The relative import is resolved against the current label.Pkg path that imports the module and converted into an path relative to the root before dependency resolution.
As a result, dependencies for relative imports are now correctly added to the deps field in package generation mode.