bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.99k stars 4.03k forks source link

feature request: repository_ctx.copy #11858

Open guibou opened 4 years ago

guibou commented 4 years ago

Description of the problem / feature request:

I'd like a repository_ctx.copy function, to copy file. I'm currently using repository_ctx.template, but that's an abuse of the API and it does not work with binariy files: #11857

Feature requests: what underlying problem are you trying to solve with this feature?

Coping file in repository rules. I can use execute(["cp", ...), but a copy api can be more convenient and more robust as it does not depend on the availability of a cp command.

alandonovan commented 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

mai93 commented 4 years ago

@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.

crncnnr commented 3 years ago

I would also appreciate if this feature could be added, it would be a better way of copying binary files.

guibou commented 3 years ago

@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?

guibou commented 3 years ago

I've found how to copy the executable bit, see https://github.com/tweag/rules_nixpkgs/pull/161/files

github-actions[bot] commented 1 year ago

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.

fmeum commented 1 year ago

@bazelbuild/triage not stale