Skip to content
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

pip_parse() does not correctly read *.pth files? #2071

Closed
EricCousineau-TRI opened this issue Jul 17, 2024 · 8 comments
Closed

pip_parse() does not correctly read *.pth files? #2071

EricCousineau-TRI opened this issue Jul 17, 2024 · 8 comments

Comments

@EricCousineau-TRI
Copy link

EricCousineau-TRI commented Jul 17, 2024

🚀 feature request

Relevant Rules

package_annotation() from pip.bzl

Description

From docs, as of time of writing, you can add BUILD content, or add / remove files, but doesn't seem like you can change py_library(deps, imports).

For example, rerun.io is an awesome-looking package.
But it doesn't work when using using bazel + rules_python @ 0.34.0:
https://github.com/EricCousineau-TRI/repro/tree/e084a7434286cf426a71350f79755c7cbab6d6eb/bazel/rules_python_rerun
Seems like either the wheel doesn't declare things s.t. rules_python adds ./site-packages/rerun_sdk to the Python path (it only adds ./site-packages).

Describe the solution you'd like

For now, would be nice to have something like

pip_parse(
    ...
    annotations = {
        "rerun-sdk": package_annotation(
            extra_imports = ["site-packages/rerun_sdk"],
        ),
    },
)

Describe alternatives you've considered

For now, will try package_annotation(..., additive_build_content), and include that whenever I use pip("rerun-sdk").

EDIT: This workaround worked: EricCousineau-TRI/repro@96e1336

@EricCousineau-TRI EricCousineau-TRI changed the title Add option to inject additonal imports and deps via package_annotation Add option to inject additonal imports and deps via package_annotation ? Jul 17, 2024
@aignas
Copy link
Collaborator

aignas commented Jul 18, 2024

imports is not supported, but deps is supported via the whl patching mechanism. You should just patch the whl METADATA file that defines the dependencies and then our code generation will do the right thing. Why do you need to modify imports?

@EricCousineau-TRI
Copy link
Author

EricCousineau-TRI commented Jul 18, 2024

in this case, it's missing site-packages/rerun_sdk in the PYTHONPATH:
https://github.com/EricCousineau-TRI/repro/tree/e084a7434286cf426a71350f79755c7cbab6d6eb/bazel/rules_python_rerun
it's not an inter-package dependency, but rather a missing dir on path within the whl itself.

perhaps it's because there's a .pth file that rules_python is not parsing? from above linked repro

$ cd $(bazel info output_base)
$ cat external/pip_deps_rerun_sdk/site-packages/rerun_sdk.pth
rerun_sdk

from docs https://docs.python.org/3/library/site.html
it seems like the .pth file should effectively change sys.path (or in this case, imports)?

@EricCousineau-TRI
Copy link
Author

EricCousineau-TRI commented Jul 18, 2024

(if this seems like a valid thing, I can file a new issue edit this issue and reword the title)

@aignas
Copy link
Collaborator

aignas commented Jul 19, 2024

Thanks for the repro and the logs, this makes it easier to understand. The rules_python not handling the .pth file could be something that is the problem here.

I think ideally the solution would be to investigate how to make rules_python respect the .pth files, but I am not sure how to do it yet. Supporting imports or deps via the annotations does not seem like the right solution here, because it increases the API surface and still forces all of the users to apply workarounds.

@EricCousineau-TRI EricCousineau-TRI changed the title Add option to inject additonal imports and deps via package_annotation ? pip_parse() does not correctly read *.pth files? Jul 19, 2024
@EricCousineau-TRI
Copy link
Author

EricCousineau-TRI commented Jul 19, 2024

sweet, welcome

I think simplest way is to assume it's non-executable *.pth (maybe check for import sys as simple check) and just have code populate imports:

# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["site-packages"],

would probably need something like parsed_pth_files as argument to that function, and then the repository rule has to cat those contents accordingly.

@EricCousineau-TRI
Copy link
Author

very loose code: main...EricCousineau-TRI:rules_python:issue-2071

@aignas
Copy link
Collaborator

aignas commented Jul 20, 2024 via email

@groodt
Copy link
Collaborator

groodt commented Aug 24, 2024

Closing this issue as a duplicate of #2156

Any PR to workaround .pth is welcome, but the PR I've linked above is a more holistic issue to capture the overall friction that rules_python causes for some packages.

@groodt groodt closed this as completed Aug 24, 2024
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

3 participants