-
-
Notifications
You must be signed in to change notification settings - Fork 162
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
Python import errors when loading multiple libraries under same namespace #145
Comments
This may be the same issue as #124 which was closed due to inactivity. |
Have you tried with |
Oh, that fixes it! That's great, thank you! @aaliddell is this a conscious feature of rules_proto_grpc that it requires use of Hmm.. Let me evaluate how hard it would be to set set For future reference, I had initially been hacking things to work with a patch like this: --- ./rules_proto_grpc/internal/compile.bzl 2021-04-06 02:50:34.000000000 +0300
+++ ./rules_proto_grpc/internal/compile.bzl 2021-09-20 19:09:13.569543935 +0300
@@ -454,13 +454,14 @@
# Otherwise, if we only have output files, build the output tree by
# aggregating files created by aspect into one directory
- output_root = get_package_root(ctx) + "/" + ctx.label.name
+ output_root = get_package_root(ctx)
for output_files_dict in output_files_dicts:
for root, files in output_files_dict.items():
for file in files.to_list():
# Strip root from file path
path = strip_path_prefix(file.path, root)
+ path = strip_path_prefix(path, ctx.label.package)
# Prepend prefix path if given
if prefix_path:
@@ -470,7 +471,7 @@
final_output_files_list.append(copy_file(
ctx,
file,
- "{}/{}".format(ctx.label.name, path),
+ path,
))
final_output_files[output_root] = depset(direct = final_output_files_list) |
No problem, thanks for the detailed report, it helps track these things quickly 👍 We don't require it to be set either way in general, but for cases like yours where paths overlap it has to be disabled for modules to import as you expect. It's a rather frustrating behaviour of Bazel that catches a lot of people out when using Python, myself included. Why it's still the default I do not know...there's a bug report open to flip it that has never been applied. Personally I always set it false for all Python targets or flip that flag. I think I'll add something to the docs to comment about this as a resolution, since it has come up a couple of times. |
Hmm, based on a quick try, looks like there are some corner cases where py_binary(
name = "foo",
legacy_create_init = False,
)
some_rule(
name = "bar",
dep = ":foo",
)
def _some_rule_impl(ctx):
runfiles = ctx.runfiles().merge(ctx.attr.dep[DefaultInfo].default_runfiles)
return [
DefaultInfo(runfiles = runfiles, executable = ...),
] Here it seems that the effect of Given that |
Interesting, at first glance that looks like a Bazel bug. Could be related to bazelbuild/bazel#12238 which is another case of
A migration strategy would be to flip the flag at the same time as setting
This is done to prevent different targets attempting to write to the same file, which Bazel disallows. For example, if your So in general, I would prefer to keep the targets outputting to separate trees since it's more painful not to. However, it should be possible to have a subsequent rule that merges these trees into a single one. There's a few ways this could be done:
|
Awesome, thank you for these detailed pointers @aaliddell - I'll give them a shot! |
Option 4 would be to add an attr to the compile/library rules that would make the files be generated without the target name directory. This would be a 'sharp edge' tool though, since you'll get Bazel errors about conflicting writes if any of the targets overlap. Technically you may be able to use this to wtite the generated files back to the source tree for check-in, but that's somewhat working around the Bazel sandbox. e.g:
|
Version 4 has an |
Getting into the same problem for cases when grpc proto imports another proto file.
Both Is there any suggestion on what would be the recommended practice? So for this issue, there two some very confusing and different-looking expected behaviors:
|
Using |
There is issue with subpar's par_binary for putting it in bazelrc, which seems to due to subpar being developed for older python version. |
Issue Description
If two libraries share a common root import path, they cannot both be imported at the same time.
This seems to be because the
python_proto_library
puts the generated_pb2.py
files under a deeply nested path:python_proto_library
then adds both__main__/protos/foo_py_proto_pb
and__main__/protos/bar_py_proto_pb
toPYTHONPATH
.However, if I try to do:
The first import works, because the
protos
module resolves to__main__/protos/foo_py_proto_pb
which does containfoo_pb2.py
. However the second import fails, becauseprotos
still resolves to__main__/protos/foo_py_proto_pb
(instead of__main__/protos/bar_py_proto_pb
) module which does not containbar_pb2.py
.Expand below for a repro script:
Log Output
No response
rules_proto_grpc Version
3.1.1
Bazel Version
4.2.1
OS
Ubuntu 18.04.3 LTS
Link to Demo Repo
No response
WORKSPACE Content
BUILD Content
Proto Content
No response
Any Other Content
The text was updated successfully, but these errors were encountered: