Skip to content

bazel run //:gazelle doesn't work with imports with format ("from x import y") #709

Closed
@sramirezmartin

Description

@sramirezmartin

🐞 bug report

Affected Rule

The issue is caused by the rule:

gazelle(
    name = "gazelle",
    data = GAZELLE_PYTHON_RUNTIME_DEPS,
    gazelle = "@rules_python//gazelle:gazelle_python_binary",
)

Is this a regression?

Unsure about this.

Description

When importing a google.cloud modules in the formar from google.cloud import aiplatform, we get the following error when running bazel run //:gazelle.


gazelle: ERROR: failed to validate dependencies for target "@toy_build_file_generation_example//subdir1/subdir2": "google.cloud" at line 4 from "subdir1/subdir2/test2.py" is an invalid dependency: possible solutions:
        1. Add it as a dependency in the requirements.txt file.
        2. Instruct Gazelle to resolve to a known dependency using the gazelle:resolve directive.
        3. Ignore it with a comment '# gazelle:ignore google.cloud' in the Python file.

But when we use the format import google.cloud.aiplatform as aiplatform, the command bazel run //:gazelle finishes successfully and the BUILD files are updated accordingly.

🔬 Minimal Reproduction

Here is a link to a toy example where the issue can be reproduced: https://github.com/sramirezmartin/gazelle-toy-example

The commands to run are:


git clone https://github.com/sramirezmartin/gazelle-toy-example.git
cd gazelle-toy-example
pip-compile --generate-hashes --output-file=subdir1/requirements_lock.txt subdir1/requirements.txt
bazel run //subdir1:gazelle_python_manifest.update
bazel run //:gazelle

🔥 Exception or Error


gazelle: ERROR: failed to validate dependencies for target "@toy_build_file_generation_example//subdir1/subdir2": "google.cloud" at line 4 from "subdir1/subdir2/test2.py" is an invalid dependency: possible solutions:
        1. Add it as a dependency in the requirements.txt file.
        2. Instruct Gazelle to resolve to a known dependency using the gazelle:resolve directive.
        3. Ignore it with a comment '# gazelle:ignore google.cloud' in the Python file.

🌍 Your Environment

Operating System:

  
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
  

Output of bazel version:

  
Build label: 5.1.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Apr 8 15:49:48 2022 (1649432988)
Build timestamp: 1649432988
Build timestamp as int: 1649432988
  

Rules_python version:

  
rules_python-0.8.1
  

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions