Open guibou opened 4 years ago
The Java implementation of Starlark has no datatype capable of holding byte strings, so template
will indeed not work for binary file copies. (Technically strings can hold bytes, but only because of Bazel's regrettable use of Latin-1 to interpret file names and file contents, which means that bytes in file names and BUILD/.bzl files are internally represented as the chars of a Java string. This is a deep and pervasive bug that requires a deep and pervasive fix.)
A copy function seems reasonable, but I have very little experience with the repository_rule API; someone more knowledgeable should review it. Tony? @aiuto
@guibou, as a workaround for the lack of the copy API, you can use the following to copy binary files without corrupting them.
ctx.file(TARGET_FILENAME, ctx.read(SOURCE_FILENAME), executable=True, legacy_utf8=False)
However, it may not be efficient in case of very large files.
I would also appreciate if this feature could be added, it would be a better way of copying binary files.
@mai93 Thank you for your workaround. Is there a way to "copy" the executable flag of the source file, instead of setting it unconditionally at True
as your example does?
I've found how to copy the executable bit, see https://github.com/tweag/rules_nixpkgs/pull/161/files
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team (@bazelbuild/triage
) if you think this issue is still relevant or you are interested in getting the issue resolved.
@bazelbuild/triage not stale
Description of the problem / feature request:
I'd like a
repository_ctx.copy
function, to copy file. I'm currently usingrepository_ctx.template
, but that's an abuse of the API and it does not work with binariy files: #11857Feature requests: what underlying problem are you trying to solve with this feature?
Coping file in repository rules. I can use
execute(["cp", ...)
, but acopy
api can be more convenient and more robust as it does not depend on the availability of acp
command.