rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

feat(gazelle): For package mode, resolve dependencies when imports are relative to the package path

Open yushan26 opened this issue 8 months ago • 6 comments

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 avatar May 07 '25 22:05 yushan26

This fix is not working correctly in Xcode 26 beta 2, it said:

Description: AssetRuntime not found at search paths: ["/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/System/AssetRuntime", "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/AssetRuntime"].
            Description: The operation couldn’t be completed. No such file or directory
    Failure Reason: Failed to spawn AssetCatalogAgent-AssetRuntime in LAR environment
    Underlying Errors:
        Underlying Errors:
/Users/xxx/Developer/Demos/bazel_swift/bazel_swift/AppIcon.icon: error: Failed to launch AssetCatalogAgent-AssetRuntime via AssetRuntime spawn
Error: The operation couldn’t be completed. No such file or directory

            Failure Reason: No such file or directory

and I reveal the log from a normal Xcode project, the command is like:

/Applications/Xcode.app/Contents/Developer/usr/bin/actool /Users/xxx/Developer/Demos/demo/demo/AppIcon.icon /Users/xxx/Developer/Demos/demo/demo/Assets.xcassets --compile /Users/xxx/Library/Developer/Xcode/DerivedData/demo-ddhegunspckcioekxncyeygwatgt/Build/Products/Debug-iphoneos/demo.app --output-format human-readable-text --notices --warnings --export-dependency-info /Users/xxx/Library/Developer/Xcode/DerivedData/demo-ddhegunspckcioekxncyeygwatgt/Build/Intermediates.noindex/demo.build/Debug-iphoneos/demo.build/assetcatalog_dependencies --output-partial-info-plist /Users/xxx/Library/Developer/Xcode/DerivedData/demo-ddhegunspckcioekxncyeygwatgt/Build/Intermediates.noindex/demo.build/Debug-iphoneos/demo.build/assetcatalog_generated_info.plist --app-icon AppIcon --accent-color AccentColor --compress-pngs --enable-on-demand-resources YES --development-region en --target-device iphone --target-device ipad --minimum-deployment-target 18 --platform iphoneos --bundle-identifier com.xxx.demo --generate-swift-asset-symbols /Users/xxx/Library/Developer/Xcode/DerivedData/demo-ddhegunspckcioekxncyeygwatgt/Build/Intermediates.noindex/demo.build/Debug-iphoneos/demo.build/DerivedSources/GeneratedAssetSymbols.swift --generate-objc-asset-symbols /Users/xxx/Library/Developer/Xcode/DerivedData/demo-ddhegunspckcioekxncyeygwatgt/Build/Intermediates.noindex/demo.build/Debug-iphoneos/demo.build/DerivedSources/GeneratedAssetSymbols.h --generate-asset-symbol-index /Users/xxx/Library/Developer/Xcode/DerivedData/demo-ddhegunspckcioekxncyeygwatgt/Build/Intermediates.noindex/demo.build/Debug-iphoneos/demo.build/DerivedSources/GeneratedAssetSymbols-Index.plist

there is no --lightweight-asset-runtime-mode option, and my Bazel project works after this option is removed from [apple/internal/resource_actions/actool.bzl](https://github.com/bazelbuild/rules_apple/pull/2733/files/62083d43e8b6dd5110ec20482b29bfa4892e88c4#diff-716bcac4ad9b1ad66b7e9f3f8e5040483853c5d776be05bef9b0b851a45bd208)

ihomway avatar Jun 27 '25 07:06 ihomway

With the revisions from upstream and my own alterations to maintain compatibility, this is ready for review.

aaronsky avatar Jul 05 '25 04:07 aaronsky