Skip to content

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

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

Closed
sramirezmartin opened this issue May 19, 2022 · 2 comments
Assignees

Comments

@sramirezmartin
Copy link

sramirezmartin commented May 19, 2022

🐞 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
  
@AugustKarlstedt
Copy link

AugustKarlstedt commented May 20, 2022

workaround is possible by changing from google.cloud import aiplatform to import google.cloud.aiplatform as aiplatform -- edit: oh yeah I see you point that out in your issue.

@domenic-donato
Copy link

We ended up finding that solution, but it only works sometimes. We ended up running into situations where it throws errors about importing six which is only imported in the libraries we depend on rather than our source code. So there are more issues with this tool when it comes to correctly parsing import statements.

aptenodytes-forsteri pushed a commit to aptenodytes-forsteri/rules_python that referenced this issue Jul 16, 2022
aptenodytes-forsteri added a commit to aptenodytes-forsteri/rules_python that referenced this issue Jul 17, 2022
alexgartner-bc pushed a commit to braincorp/rules_python that referenced this issue Jul 29, 2022
Fixes bazel-contrib#709.

Test case for pip imports using "from foo import bar".

Test cases for imports of the form "from foo import bar".

Remove unnecessary flag (replaced with continue's).

Make sure from imports work with std modules.

- Add test case with `from __future__ import print_function`.

Guard python_interpreter_target workspace name on None type check. (bazel-contrib#755)

Fix for requirements_lock with PEP440 direct references (bazel-contrib#756)

Fix indentation error.

- Make sure that `from foo import bar, baz` works.
- Add test case for this.
@f0rmiga f0rmiga closed this as completed in ebeb822 Aug 4, 2022
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

No branches or pull requests

4 participants