-
Notifications
You must be signed in to change notification settings - Fork 540
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
import rules_python doesn't work under bzlmod #1679
Comments
cc: @aiuto |
FWIW, in ChromeOS we worked around the issue of repo mapping by creating a sitecustomize.py that reads the repo_mapping file to determine what Mind you, ChromeOS has a few extra hacks in there to make existing non-bazel imports work, but you can see the general idea. |
@rickeylev pointed out that the toolchain itself is relevant here as well, so here's the link to our toolchain. We wrap the interpreter in a script that ensures that the |
Just hit this as well, I noticed the PR that supposedly fixed this is approved but stalled. Any updates? |
Is there a broader bug tracking the issue of Python import paths in bzlmod? As pointed out in bazelbuild/bazel#2636 (comment), this also seems to make it infeasible to ever flip the |
Removes --noenable_bzlmod from Pigweed's .bazelrc and applies necessary changes to get the build working again. Also makes Pigweed usable as a bzlmod dependency. Most of the file changes in this CL are Python import statement changes required because of bazelbuild/rules_python#1679. Bug: b/258836641 Change-Id: I8d3e514e74cd6d76e721c5113c44dcbb8ddc9d40 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/211362 Commit-Queue: Ted Pudlik <tpudlik@google.com> Reviewed-by: Taylor Cramer <cramertj@google.com> Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
My personal opinion is to abandon this usage since this is Bazel specific usage. I believe for most python projects, they have a top level directory matching its pip package name. But @rickeylev should make the final decision ;) |
The comment from #1679 (comment) is related, but also adjacent to the original issue. The original issue relates to the "runfiles" library specifically. This is a very narrow and special library that is jointly included as part of rules_python, but also published independently as a package on PyPI and that's when we discovered this particular issue that the Python "runfiles" module that comes with rules_python will no longer work with bzlmod under the old import paths. The original issue does relate to general user space usage of all Python modules with Bazel. Those indeed should not be using workspace name as import prefixes. That's never been a documented or recommended approach. |
IIRC (I last looked at this around a year ago), there wasn't any documented or recommended approach, and people used the workspace name because if I To the best of my knowledge, there's no import path that will work other than that one, unless you want to screw around with |
It's probably a documentation issue, that wouldn't surprise me. Our examples are not great, but they do demonstrate the intended usage (and always have, while they've existed): e.g.
|
In that example, Before repo mapping, I believe that |
Got it. If you can please raise a different issue for that? This specific issue is about the |
I'll go ahead and do that, but FWIW, I think that workspace name as prefixes is the correct way to go, and that if we fix that, we get this for free. |
In some ways yes, but in others ways no. The So either through a dependency specifier in that resolves to a locked version in PyPI e.g. Or as a direct dependency url specifier such e.g. The reason for this issue being created, was because it can be seen as a backwards incompatible change with the integrated There's never been documented usage for how to use or resolve python source dependencies across bazel respositories to my knowledge. |
Yeah, using the Bazel repo name as the Python top-level import name was always a bad idea. Even prior to bzlmod, because repos names were decided by the end user, you never had a strong, reliable, top-level Python name for a library made available through that mechanism. The best analogy I can draw is with Java: Unfortunately, using the repo name turned into some sort of mild convention. And bzlmod threw a big wrench into it. Doing the Right Thing (rehoming code to another directoy and using the A lighter weight, more interim/transnational, solution would be nicer. I'm not sure exactly what that solution would look like, though. It's complicated by the fact that the runfiles root and each repo's root is added to sys.path -- both undesirable behaviors. I think the lighter solution looks something like, if you wanted to make
I think that would mostly work? The issue with playing games like this with sys.path and a package's sub-module search path ( Also, I'm pretty sure the above can also be done on the user side. e.g., if you're |
I'm not sure I buy that it was a bad idea. Namespacing is what prevents conflicts. If I depend on the I, for one, would much prefer writing FWIW, in ChromeOS, our main repo was called |
I feel like part of the problem here is that it feels like rules_python should be exposing runfiles as Generally, it's probably wisest for projects to create well-formed python packages, which unfortunately means storing the package in a subdirectory that provides a project/package namespace. In Pigweed, we almost never relied on the import paths provided by rules_python. Even when we did, the only exceptions were for some tests. I reluctantly feel like that's the most robust way to structure a project. Our Python libraries were well-formed python packages before we started embracing Bazel, so this change didn't impact us much (besides the confusion of I would like to echo, though, that this is a pretty jarring behavioral change that seems to warrant some sort of migration path. It's surprising to me that this issue wasn't immediately closed with "this is intentional behavior, we flipped the default value of X flag with plans to remove the option entirely in version YY" |
Removes --noenable_bzlmod from Pigweed's .bazelrc and applies necessary changes to get the build working again. Also makes Pigweed usable as a bzlmod dependency. Most of the file changes in this CL are Python import statement changes required because of bazelbuild/rules_python#1679. Original-Bug: b/258836641 Original-Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/211362 Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com> https://pigweed.googlesource.com/pigweed/pigweed pigweed, pw_toolchain Rolled-Commits: 4f0412dae17b20a..00d99767eb5ad66 Roller-URL: https://ci.chromium.org/b/8741667154616515185 GitWatcher: ignore CQ-Do-Not-Cancel-Tryjobs: true Change-Id: I5437982f041b87873fb4d81e4113584038a3f1cf Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/quickstart/bazel/+/225374 Bot-Commit: Pigweed Roller <pigweed-roller@pigweed-service-accounts.iam.gserviceaccount.com> Commit-Queue: Pigweed Roller <pigweed-roller@pigweed-service-accounts.iam.gserviceaccount.com> Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
* `runfiles` is provided both as part of the Bazel `rules_python` repository as `@rules_python//python:runfiles` and on PyPI as the `bazel-runfiles` pip package. * It could be imported from the Bazel repository as `rules_python.python.runfiles` when using WORKSPACE, but needed to be imported as `python.runfiles` when using Bzlmod. * However, the pip package is independent of whether WORKSPACE or Bzlmod is being used. * See bazelbuild/rules_python#1679 for details.
* `runfiles` is provided both as part of the Bazel `rules_python` repository as `@rules_python//python:runfiles` and on PyPI as the `bazel-runfiles` pip package. * It could be imported from the Bazel repository as `rules_python.python.runfiles` when using WORKSPACE, but needed to be imported as `python.runfiles` when using Bzlmod. * However, the pip package is independent of whether WORKSPACE or Bzlmod is being used. * See bazelbuild/rules_python#1679 for details.
🐞 bug report
This is the rules_python issue for bazelbuild/bazel#18128
Affected Rule
Import the runfiles library via the
@rules_python//python/runfiles
depending asimport rules_python.python.runfiles
Is this a regression?
Effectively, yes. The established name for the libraries rules_python provides are under the "rules_python" namespace.
Description
In bzlmod,
import rules_python.python.runfiles
doesn't work. It works for workspace builds.This is because, in bzlmod, the directory names of repos in runfiles changes from "foo" to "foo~version" (where version is the module version plus some other parts). Such a name isn't importable because it can't be known in advance, and isn't a valid python package a name.
🔬 Minimal Reproduction
@rules_python//python/runfiles
import rules_python
An import error will occur.
Analysis
Fixing this is sort of straightforward -- we need to get a directory onto sys.path that has a rules_python sub-directory.
Some complicating factors are:
import python.runfiles
to continue working. This is the name we told people to use in the meantime, so we'll need to stick with that.import src.bla.bla
themselves. This came up via slack, where I've seen people recommend using a repo-relative import name likeimport src.bla
.import runfiles
, so we have to be not interfere with that$repoRoot/python/runfiles/runfiles.py
file existing. So I'm hesitant to move that file. I don't know what or why that would be the case though, so perhaps I'm beingThe text was updated successfully, but these errors were encountered: