You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make py_binary have zipped binary as implicit output #3530
I'd like to be able to depend on the zipped python binary for distribution, but not build the zipped binary usually. To do this, I'd like to specify //path/to/my/py_binary_target.zip as a dependency of my distribution artifact.
At the moment, it's possible to build with --build_python_zip, but I'd rather not have to use different flags for ordinary builds and distribution builds.
An alternative would be to add an explicit attribute to py_binary/py_test rules that changes the behavior for that single target, from the current behavior (generated "py_binary_target" is a shell script that invokes the "py_binary_target.runfiles/" tree on Linux), to the "--build_python_zip" behavior ("py_binary_target" is a zip file with '#!" line).
The implicit output seems a lot more predictable: if you want the zip, ask for the zip. It also allows for faster iteration as bazel run won't require the building of a zip.
As a final point, if anyone ever lands PEX support (#2038) then that could also be implemented as an implicit output, rather than Yet Another Parameter.
In considering the native rules' zip files vs subpar, I'm thinking we should aim to implement this and deprecate subpar. See google/subpar#44 for more discussion.
It may also be worth renaming the .zip extension to .pyz after PEP 441.
Reacted by Rohan Agarwal, Daniel J. Beutel, Markus Padourek and Patrick Selamy
My understanding is that Python itself shouldn't care about the file extension, but using a different extension may affect file associations in the OS shell. While not all versions of Python would recognize .pyz under windows, you'd think it'd be no worse than leaving it as .zip where the default association would be some extractor tool.
Pep 441 explicitly mentions that the zip format may be used with shebang lines and self-extracting archives, so I don't see a conflict.
I'd be keen to try help implement this as well if necessary. Or is this something that you are already wishing to take on? I imagine this would need to be added to rules_python and not Bazel itself, however, I have no current knowledge of what --build_python_zip is doing behind the covers in the Bazel code.
@groodt I think this should be implemented in the native python rule, because that's what --build_python_zip applies to.
I sent #9453, please take a look.
Implicit output requires the rule to always generate an action that outputs the declared file. But in some cases, that's not very nice. For example, when --build_python_zip is disabled, we'll have to generate a fake action for that. With output_group, we don't have that problem, if the flag is disabled, the output group will just be empty without throwing an error.
I see. Thank you @meteorcloudy. It sounds like it's not really that implicit output is bad but the flag controlled zip output plus implicit output it tricky. Why don't we just generate the implicit output unconditionally, probably under a different name like "name.pz"?
Why don't we just generate the implicit output unconditionally
Because the python zip file includes all transitive runtime dependencies, sometimes it could take a long time to build (especially on Windows). I think there are some other disadvantages of implicitly outputs in general, @oquenchil anything you could add?
So it might be that implicit outputs stay in Starlark rules in some form. Not certain about this. The problem here is what Yun said, it is expensive to create the action for that artifact unconditionally, so unfortunately you will have to use the additional flag for distribution.
An implicit output is really handy when you're working on the command line and want to both run locally (regular target), and then build the zip for deployment elsewhere (the implicit target) Working with an output group is clumsier.
You can just define an extra target in the BUILD file:
filegroup(
name = "foo_zip",
srcs= ["//bar:foo"],
output_group = "python_zip_file",
)
Then in the command line you only need to switch between bazel build //:foo and bazel build //:foo_zip, I don't think that's much harder than implicit outputs?
Indeed, but that is more verbose and clumsier than an implicit target, and also requires the author of the build files to know in advance whether or not all consumers of that file will want a zip or not (it's arguable that they should know, but in a large codebase that may not actually be true)
Activity
duggelz commentedon Nov 17, 2017
An alternative would be to add an explicit attribute to
py_binary/py_testrules that changes the behavior for that single target, from the current behavior (generated "py_binary_target" is a shell script that invokes the "py_binary_target.runfiles/" tree on Linux), to the "--build_python_zip" behavior ("py_binary_target" is a zip file with '#!" line).shs96c commentedon Mar 26, 2019
The implicit output seems a lot more predictable: if you want the zip, ask for the zip. It also allows for faster iteration as
bazel runwon't require the building of a zip.As a final point, if anyone ever lands PEX support (#2038) then that could also be implemented as an implicit output, rather than Yet Another Parameter.
brandjon commentedon May 10, 2019
In considering the native rules' zip files vs subpar, I'm thinking we should aim to implement this and deprecate subpar. See google/subpar#44 for more discussion.
It may also be worth renaming the
.zipextension to.pyzafter PEP 441.meteorcloudy commentedon May 13, 2019
Sounds good. But I guess our
.zipfile doesn't work exactly the way.pyzfile work?brandjon commentedon May 13, 2019
My understanding is that Python itself shouldn't care about the file extension, but using a different extension may affect file associations in the OS shell. While not all versions of Python would recognize
.pyzunder windows, you'd think it'd be no worse than leaving it as.zipwhere the default association would be some extractor tool.Pep 441 explicitly mentions that the zip format may be used with shebang lines and self-extracting archives, so I don't see a conflict.
groodt commentedon Sep 27, 2019
Is there any update or movement here?
It would be great to be able to reference:
//path/to/my/py_binary_target.zipand have the executable zip consumed in other targets, such as rules_docker.
meteorcloudy commentedon Sep 30, 2019
I think it's preferred to not have implicit output. How about we add it to a output group called
python_zip_file, then you can access it bygroodt commentedon Oct 1, 2019
This would be wonderful @meteorcloudy
I'd be keen to try help implement this as well if necessary. Or is this something that you are already wishing to take on? I imagine this would need to be added to rules_python and not Bazel itself, however, I have no current knowledge of what
--build_python_zipis doing behind the covers in the Bazel code.meteorcloudy commentedon Oct 1, 2019
@groodt I think this should be implemented in the native python rule, because that's what
--build_python_zipapplies to.I sent #9453, please take a look.
brandjon commentedon Oct 18, 2019
Thanks @meteorcloudy for the implementation!
mzhaom commentedon Oct 25, 2021
@meteorcloudy , could you give more context why implicit output was not preferred? I really miss the behaviour provided by implicit output.
meteorcloudy commentedon Oct 26, 2021
Implicit output requires the rule to always generate an action that outputs the declared file. But in some cases, that's not very nice. For example, when --build_python_zip is disabled, we'll have to generate a fake action for that. With output_group, we don't have that problem, if the flag is disabled, the output group will just be empty without throwing an error.
mzhaom commentedon Oct 26, 2021
I see. Thank you @meteorcloudy. It sounds like it's not really that implicit output is bad but the flag controlled zip output plus implicit output it tricky. Why don't we just generate the implicit output unconditionally, probably under a different name like "name.pz"?
meteorcloudy commentedon Oct 27, 2021
Because the python zip file includes all transitive runtime dependencies, sometimes it could take a long time to build (especially on Windows). I think there are some other disadvantages of implicitly outputs in general, @oquenchil anything you could add?
oquenchil commentedon Oct 27, 2021
So it might be that implicit outputs stay in Starlark rules in some form. Not certain about this. The problem here is what Yun said, it is expensive to create the action for that artifact unconditionally, so unfortunately you will have to use the additional flag for distribution.
shs96c commentedon Oct 27, 2021
An implicit output is really handy when you're working on the command line and want to both run locally (regular target), and then build the zip for deployment elsewhere (the implicit target) Working with an output group is clumsier.
meteorcloudy commentedon Oct 27, 2021
You can just define an extra target in the BUILD file:
Then in the command line you only need to switch between
bazel build //:fooandbazel build //:foo_zip, I don't think that's much harder than implicit outputs?shs96c commentedon Oct 27, 2021
Indeed, but that is more verbose and clumsier than an implicit target, and also requires the author of the build files to know in advance whether or not all consumers of that file will want a zip or not (it's arguable that they should know, but in a large codebase that may not actually be true)
mzhaom commentedon Oct 27, 2021
I miss the experince of building
parwith blaze, nothing needs to be added in BUILD files to switch betweenbuild :foovsbuild :foo.parpy_unzip: Create an archive that can be expanded into a standalone, r…