Skip to content

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 14 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.

@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
@@ -0,0 +1 @@
# gazelle:python_generation_mode package
Copy link
Contributor

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:

  1. have all test cases in "testdata/relative_imports", with some packages in package mode, some in project mode and some in file mode, or
  2. have "testdata/relative_imports_package_mode", "testdata/relative_imports_file_mode" and "testdata/relative_imports_project_mode"

Copy link
Author

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.

Copy link
Contributor

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.

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.

3 participants