Skip to main content
Version: 2.7.x

copy_to_bin

A rule that copies source files to the output tree.

This rule uses a Bash command (diff) on Linux/macOS/non-Windows, and a cmd.exe command (fc.exe) on Windows (no Bash is required).

Originally authored in rules_nodejs https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabea4856f8e/internal/common/copy_to_bin.bzl

Macros and Functions

copy_file_to_bin_action

Factory function that creates an action to copy a file to the output tree.

File are copied to the same workspace-relative path. The resulting files is returned.

If the file passed in is already in the output tree is then it is returned without a copy action.

To use copy_file_to_bin_action in your own rules, you need to include the toolchains it uses in your rule definition. For example:

load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS")

my_rule = rule(
...,
toolchains = COPY_FILE_TO_BIN_TOOLCHAINS,
)

Additionally, you must ensure that the coreutils toolchain is has been registered in your WORKSPACE if you are not using bzlmod:

load("@aspect_bazel_lib//lib:repositories.bzl", "register_coreutils_toolchains")

register_coreutils_toolchains()

Example usage (generated):

load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_file_to_bin_action")

copy_file_to_bin_action(
# The rule context.
ctx = None,
# The file to copy.
file = "",
)

ctx

Required.

The rule context.

file

Required.

The file to copy.

copy_files_to_bin_actions

Factory function that creates actions to copy files to the output tree.

Files are copied to the same workspace-relative path. The resulting list of files is returned.

If a file passed in is already in the output tree is then it is added directly to the result without a copy action.

Example usage (generated):

load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_files_to_bin_actions")

copy_files_to_bin_actions(
# The rule context.
ctx = None,
# List of File objects.
files = None,
)

ctx

Required.

The rule context.

files

Required.

List of File objects.

copy_to_bin

Copies a source file to output tree at the same workspace-relative path.

e.g. <execroot>/path/to/file -> <execroot>/bazel-out/<platform>/bin/path/to/file

If a file passed in is already in the output tree is then it is added directly to the DefaultInfo provided by the rule without a copy.

This is useful to populate the output folder with all files needed at runtime, even those which aren't outputs of a Bazel rule.

This way you can run a binary in the output folder (execroot or runfiles_root) without that program needing to rely on a runfiles helper library or be aware that files are divided between the source tree and the output tree.

Example usage (generated):

load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")

copy_to_bin(
# Name of the rule.
name = "",
# A list of labels
srcs = [],
)

name

Required.

Name of the rule.

srcs

Required.

A list of labels. File(s) to copy.

kwargs

Optional.

further keyword arguments, e.g. visibility