Skip to main content
Version: 2.8.x

copy_file

A rule that copies a file to another place.

native.genrule() is sometimes used to copy files (often wishing to rename them). The copy_file rule does this with a simpler interface than genrule.

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

This fork of bazel-skylib's copy_file adds DirectoryPathInfo support and allows multiple copy_file rules in the same package.

Macros and Functions

copy_file

Copies a file or directory to another location.

native.genrule() is sometimes used to copy files (often wishing to rename them). The 'copy_file' rule does this with a simpler interface than genrule.

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

If using this rule with source directories, it is recommended that you use the --host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1 startup option so that changes to files within source directories are detected. See https://github.com/bazelbuild/bazel/commit/c64421bc35214f0414e4f4226cc953e8c55fa0d2 for more context.

Example usage (generated):

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

copy_file(
# Name of the rule.
name = "",
# A Label
src = "",
# Path of the output file, relative to this package.
out = "",
)

name

Required.

Name of the rule.

src

Required.

A Label. The file to make a copy of. (Can also be the label of a rule that generates a file.)

out

Required.

Path of the output file, relative to this package.

is_executable

Optional. Default: False

A boolean. Whether to make the output file executable. When True, the rule's output can be executed using bazel run and can be in the srcs of binary and test rules that require executable sources. WARNING: If allow_symlink is True, src must also be executable.

Optional. Default: False

A boolean. Whether to allow symlinking instead of copying. When False, the output is always a hard copy. When True, the output can be a symlink, but there is no guarantee that a symlink is created (i.e., at the time of writing, we don't create symlinks on Windows). Set this to True if you need fast copying and your tools can handle symlinks (which most UNIX tools can).

kwargs

Optional.

further keyword arguments, e.g. visibility

copy_file_action

Factory function that creates an action to copy a file from src to dst.

If src is a TreeArtifact, dir_path must be specified as the path within the TreeArtifact to the file to copy.

This helper is used by copy_file. It is exposed as a public API so it can be used within other rule implementations.

To use copy_file_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_file.bzl", "COPY_FILE_TOOLCHAINS")

my_rule = rule(
...,
toolchains = COPY_FILE_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_file.bzl", "copy_file_action")

copy_file_action(
# The rule context.
ctx = None,
# The source file to copy or TreeArtifact to copy a single file out of.
src = "",
# The destination file.
dst = None,
)

ctx

Required.

The rule context.

src

Required.

The source file to copy or TreeArtifact to copy a single file out of.

dst

Required.

The destination file.

dir_path

Optional. Default: None

If src is a TreeArtifact, the path within the TreeArtifact to the file to copy.