From 716af223c2e35d64766b5073c9208f9a91f5e870 Mon Sep 17 00:00:00 2001 From: Derek Cormier Date: Wed, 18 Sep 2024 18:16:14 -0700 Subject: [PATCH] feat: add an option to not include copy_to_directory output in runfiles (#886) * feat: add an option to not include copy_to_directory output in runfiles * chore: docs update * chore: don't repeat default --------- Co-authored-by: Alex Eagle --- docs/copy_to_directory.md | 8 +++++--- lib/private/copy_to_directory.bzl | 11 ++++++++++- lib/tests/copy_to_directory/BUILD.bazel | 4 ++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/copy_to_directory.md b/docs/copy_to_directory.md index 89696ac..949e781 100644 --- a/docs/copy_to_directory.md +++ b/docs/copy_to_directory.md @@ -7,9 +7,10 @@ Copy files and directories to an output directory. ## copy_to_directory
-copy_to_directory(name, srcs, out, allow_overwrites, exclude_srcs_packages, exclude_srcs_patterns,
-                  hardlink, include_external_repositories, include_srcs_packages,
-                  include_srcs_patterns, preserve_mtime, replace_prefixes, root_paths, verbose)
+copy_to_directory(name, srcs, out, add_directory_to_runfiles, allow_overwrites,
+                  exclude_srcs_packages, exclude_srcs_patterns, hardlink,
+                  include_external_repositories, include_srcs_packages, include_srcs_patterns,
+                  preserve_mtime, replace_prefixes, root_paths, verbose)
 
Copies files and directories to an output directory. @@ -50,6 +51,7 @@ for more information on supported globbing patterns. | name | A unique name for this target. | Name | required | | | srcs | Files and/or directories or targets that provide `DirectoryPathInfo` to copy into the output directory. | List of labels | optional | `[]` | | out | Path of the output directory, relative to this package.

If not set, the name of the target is used. | String | optional | `""` | +| add_directory_to_runfiles | Whether to add the outputted directory to the target's runfiles. | Boolean | optional | `True` | | allow_overwrites | If True, allow files to be overwritten if the same output file is copied to twice.

The order of srcs matters as the last copy of a particular file will win when overwriting. Performance of copy_to_directory will be slightly degraded when allow_overwrites is True since copies cannot be parallelized out as they are calculated. Instead all copy paths must be calculated before any copies can be started. | Boolean | optional | `False` | | exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.

Files in srcs are not copied to the output directory if the Bazel package of the file matches one of the patterns specified.

Forward slashes (`/`) should be used as path separators. A first character of `"."` will be replaced by the target's package path.

Files that have do not have matching Bazel packages are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.

Globs are supported (see rule docstring above). | List of strings | optional | `[]` | | exclude_srcs_patterns | List of paths (with glob support) to exclude from output directory.

Files in srcs are not copied to the output directory if their output directory path, after applying `root_paths`, matches one of the patterns specified.

Forward slashes (`/`) should be used as path separators.

Files that do not have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.

Globs are supported (see rule docstring above). | List of strings | optional | `[]` | diff --git a/lib/private/copy_to_directory.bzl b/lib/private/copy_to_directory.bzl index e09d506..ee0cbbe 100644 --- a/lib/private/copy_to_directory.bzl +++ b/lib/private/copy_to_directory.bzl @@ -51,6 +51,7 @@ _copy_to_directory_attr_doc = { If not set, the name of the target is used. """, + "add_directory_to_runfiles": """Whether to add the outputted directory to the target's runfiles.""", # root_paths "root_paths": """List of paths (with glob support) that are roots in the output directory. @@ -222,6 +223,12 @@ _copy_to_directory_attr = { "out": attr.string( doc = _copy_to_directory_attr_doc["out"], ), + # TODO(3.0): Remove this attribute and do not add directory to runfiles by default. + # https://github.com/aspect-build/bazel-lib/issues/748 + "add_directory_to_runfiles": attr.bool( + default = True, + doc = _copy_to_directory_attr_doc["add_directory_to_runfiles"], + ), "root_paths": attr.string_list( default = ["."], doc = _copy_to_directory_attr_doc["root_paths"], @@ -296,10 +303,12 @@ def _copy_to_directory_impl(ctx): verbose = ctx.attr.verbose, ) + runfiles = ctx.runfiles([dst]) if ctx.attr.add_directory_to_runfiles else None + return [ DefaultInfo( files = depset([dst]), - runfiles = ctx.runfiles([dst]), + runfiles = runfiles, ), ] diff --git a/lib/tests/copy_to_directory/BUILD.bazel b/lib/tests/copy_to_directory/BUILD.bazel index 807bcef..4077fe8 100644 --- a/lib/tests/copy_to_directory/BUILD.bazel +++ b/lib/tests/copy_to_directory/BUILD.bazel @@ -152,6 +152,10 @@ copy_to_directory( include_external_repositories = ["external_test_repo"], ) +# TODO: This test is a false positive. sh_test always includes default +# outputs in runfiles, so even if copy_to_directory did not add its output +# to its runfiles this test would still pass. +# See https://github.com/bazelbuild/bazel/issues/4519. sh_test( name = "case_6_test", timeout = "short",