Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yushan26
Copy link

@yushan26 yushan26 commented May 7, 2025

When # gazelle:python_generation_mode package is enabled, relative imports are currently not being added to the deps field of the generated target.

For example, given the following Python code:

from .library import add as _add
from .library import divide as _divide
from .library import multiply as _multiply
from .library import subtract as _subtract

The expected py_library rule should include a dependency on the local library package:

py_library(
    name = "py_default_library",
    srcs = ["__init__.py"],
    visibility = ["//visibility:public"],
    deps = [
        "//example/library:py_default_library",
    ],
)

However, the actual generated rule is missing the deps entry:

py_library(
    name = "py_default_library",
    srcs = ["__init__.py"],
    visibility = ["//visibility:public"],
)

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.

Added a directive # gazelle:experimental_allow_relative_imports true to allow this feature to be opt in.

@yushan26 yushan26 requested review from dougthor42 and aignas as code owners May 7, 2025 22:42
@yushan26 yushan26 force-pushed the resolve-rel-imports branch from 37c9386 to 8914db1 Compare May 7, 2025 23:08
Copy link
Contributor

@linzhp linzhp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @aignas Can you take a look?

@aignas
Copy link
Collaborator

aignas commented May 21, 2025

Don't have time right now to look into gazelle PRs at a great detail, so I'll defer this one to @dougthor42, Thanks @linzhp, for the initial review!

@dougthor42
Copy link
Collaborator

@yushan26 can you take a look at the discussion in #2203? There are some edge case issues that I'd like to know if you've addressed. It looks like you're focusing on the "importing a class/function identifier" case. Does this also support importing relative modules?

from ..my_library import some_function  # looks like you've got this covered
from ...my_library.foo import some_function  # this too
from .library import other_module
from .. import some_module
from .. import some_function  # eg the function is exposed in ../__init__.py

Also, if we add support for relative imports, we need to make sure that it works when # gazelle:python_generation_mode file.

@linzhp
Copy link
Contributor

linzhp commented May 23, 2025

Also, if we add support for relative imports

To clarify, relative imports are already supported before this PR (see this test), but it's partially broken in package mode (on importing another package) and not supported in file mode. This PR fixes the package mode. I am leaning towards fixing the file mode in a separate PR, unless you feel strongly about fixing both of them in one PR.

@yushan26 yushan26 force-pushed the resolve-rel-imports branch from 24ace24 to 7d1f434 Compare May 23, 2025 22:55
@yushan26
Copy link
Author

Yea good point. Updated the test case to support the scenario and updated the function.

Copy link
Collaborator

@dougthor42 dougthor42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am leaning towards fixing the file mode in a separate PR

SGTM.


Other requests:

  1. Please update the PR title to follow the guidelines.
  2. Please update the changelog.

@yushan26 yushan26 changed the title Resolve dependencies when imports are relative to the package path feat(gazelle) For package mode, resolve dependencies when imports are relative to the package path May 27, 2025
@yushan26 yushan26 requested a review from rickeylev as a code owner May 27, 2025 20:19
@yushan26
Copy link
Author

yushan26 commented May 27, 2025

@dougthor42 Thanks for the review. I also included the directive # gazelle:experimental_allow_relative_imports to allow opt-in instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants