copy_directory
A rule that copies a directory to another place.
The rule uses a precompiled binary to perform the copy, so no shell is required.
Preserving modification times
copy_directory
and copy_to_directory
have a preserve_mtime
attribute, however
there are two caveats to consider when using this feature:
- Remote Execution / Caching: These layers will reset the modify time and are incompatible with this feature. To avoid these failures the no-remote tag can be added.
- Caching: Changes to only the modified time will not re-trigger cached actions. This can be worked around by using a clean build when these types of changes occur. For tests the external tag can be used but this will result in tests never being cached.
Macros and Functions
copy_directory
Copies a directory to another location.
This rule uses a precompiled binary to perform the copy, so no shell 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_directory.bzl", "copy_directory")
copy_directory(
# Name of the rule.
name = "",
# The directory to make a copy of
src = "",
# Path of the output directory, relative to this package.
out = "",
)
name
Required.
Name of the rule.
src
Required.
The directory to make a copy of. Can be a source directory or TreeArtifact.
out
Required.
Path of the output directory, relative to this package.
hardlink
Optional. Default: "auto"
Controls when to use hardlinks to files instead of making copies.
Creating hardlinks is much faster than making copies of files with the caveat that hardlinks share file permissions with their source.
Since Bazel removes write permissions on files in the output tree after an action completes, hardlinks to source files within source directories is not recommended since write permissions will be inadvertently removed from sources files.
- "auto": hardlinks are used if src is a tree artifact already in the output tree
- "off": files are always copied
- "on": hardlinks are always used (not recommended)
kwargs
Optional.
further keyword arguments, e.g. visibility
copy_directory_bin_action
Factory function that creates an action to copy a directory from src to dst using a tool binary.
The tool binary will typically be the @aspect_bazel_lib//tools/copy_directory
go_binary
either built from source or provided by a toolchain.
This helper is used by the copy_directory rule. It is exposed as a public API so it can be used within other rule implementations.
Example usage (generated):
load("@aspect_bazel_lib//lib:copy_directory.bzl", "copy_directory_bin_action")
copy_directory_bin_action(
# The rule context.
ctx = None,
# The source directory to copy.
src = "",
# The directory to copy to
dst = None,
# Copy to directory tool binary.
copy_directory_bin = None,
)
ctx
Required.
The rule context.
src
Required.
The source directory to copy.
dst
Required.
The directory to copy to. Must be a TreeArtifact.
copy_directory_bin
Required.
Copy to directory tool binary.
hardlink
Optional. Default: "auto"
Controls when to use hardlinks to files instead of making copies.
See copy_directory rule documentation for more details.
verbose
Optional. Default: False
print verbose logs to stdout
preserve_mtime
Optional. Default: False
preserve the modified time from the source. See the caveats above about interactions with remote execution and caching.