fix: handle copy_to_directory '.' and './**' special patterns correctly when in the root package (#212)

This commit is contained in:
Greg Magolan 2022-08-11 16:25:51 -07:00 committed by GitHub
parent aea1c5b72e
commit 3caff577b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 159 additions and 16 deletions

View File

@ -4,5 +4,6 @@ env:
workspaces:
- .
- e2e/bzlmod
- e2e/copy_to_directory
tasks:
test:

View File

@ -21,6 +21,7 @@ jobs:
folder:
- "."
- "e2e/bzlmod"
- "e2e/copy_to_directory"
# Steps represent a sequence of tasks that will be executed as part of the job
steps:

View File

@ -26,10 +26,10 @@ copy_to_directory(<a href="#copy_to_directory-name">name</a>, <a href="#copy_to_
| <a id="copy_to_directory-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="copy_to_directory-allow_overwrites"></a>allow_overwrites | If True, allow files to be overwritten if the same output file is copied to twice.<br><br> If set, then the order of srcs matters as the last copy of a particular file will win.<br><br> This setting has no effect on Windows where overwrites are always allowed. | Boolean | optional | False |
| <a id="copy_to_directory-exclude_prefixes"></a>exclude_prefixes | List of path prefixes (with glob support) to exclude from output directory.<br><br> DEPRECATED: use <code>exclude_srcs_patterns</code> instead<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if their output directory path, after applying <code>root_paths</code>, starts with or fully matches one of the patterns specified.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>include_srcs_patterns</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Files and directories 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_packages"></a>exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A "." value expands to the target's package path (<code>ctx.label.package</code>). A "./**" value expands to the target's package path followed by a slash and a globstar (<code>"{}/**".format(ctx.label.package)</code>).<br><br> Files and directories 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_packages"></a>exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A <code>"."</code> value means exclude srcs that are in the target's package. It expands to the target's package path (<code>ctx.label.package</code>). This will be an empty string <code>""</code> if the target is in the root package.<br><br> A <code>"./**"</code> value means exclude srcs that are in subpackages of the target's package. It expands to the target's package path followed by a slash and a globstar (<code>"{}/**".format(ctx.label.package)</code>). If the target's package is the root package, <code>"./**"</code> expands to <code>["?*", "?*/**"]</code> instead.<br><br> Files and directories 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_patterns"></a>exclude_srcs_patterns | List of paths (with glob support) to exclude from output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if their output directory path, after applying <code>root_paths</code>, matches one of the patterns specified.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>include_srcs_patterns</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Files and directories 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-include_external_repositories"></a>include_external_repositories | List of external repository names (with glob support) to include in the output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files from external repositories are only copied into the output directory if the external repository they come from matches one of the external repository patterns specified.<br><br> When copied from an external repository, the file path in the output directory defaults to the file's path within the external repository. The external repository name is _not_ included in that path.<br><br> For example, the following copies <code>@external_repo//path/to:file</code> to <code>path/to/file</code> within the output directory.<br><br> <pre><code> copy_to_directory( name = "dir", include_external_repositories = ["external_*"], srcs = ["@external_repo//path/to:file"], ) </code></pre><br><br> Files and directories that come from matching external are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be. The external repository name of the file or directory from an external repository is not included in the output directory path and is considered in subsequent filters and transformations.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-include_srcs_packages"></a>include_srcs_packages | List of Bazel packages (with glob support) to include in output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are only copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A "." value expands to the target's package path (<code>ctx.label.package</code>). A "./**" value expands to the target's package path followed by a slash and a globstar (<code>"{}/**".format(ctx.label.package)</code>).<br><br> Defaults to ["**"] which includes sources from all packages.<br><br> Files and directories that 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] |
| <a id="copy_to_directory-include_srcs_packages"></a>include_srcs_packages | List of Bazel packages (with glob support) to include in output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are only copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A <code>"."</code> value means include srcs that are in the target's package. It expands to the target's package path (<code>ctx.label.package</code>). This will be an empty string <code>""</code> if the target is in the root package.<br><br> A <code>"./**"</code> value means include srcs that are in subpackages of the target's package. It expands to the target's package path followed by a slash and a globstar (<code>"{}/**".format(ctx.label.package)</code>). If the target's package is the root package, <code>"./**"</code> expands to <code>["?*", "?*/**"]</code> instead.<br><br> Defaults to <code>["**"]</code> which includes sources from all packages.<br><br> Files and directories that 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] |
| <a id="copy_to_directory-include_srcs_patterns"></a>include_srcs_patterns | List of paths (with glob support) to include in output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are only copied to the output directory if their output directory path, after applying <code>root_paths</code>, matches one of the patterns specified.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>include_srcs_patterns</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Defaults to ["**"] which includes all sources.<br><br> Files and directories that 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] |
| <a id="copy_to_directory-out"></a>out | Path of the output directory, relative to this package.<br><br> If not set, the name of the target is used. | String | optional | "" |
| <a id="copy_to_directory-replace_prefixes"></a>replace_prefixes | Map of paths prefixes (with glob support) to replace in the output directory path when copying files.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported but the pattern must not end with a <code>**</code> glob expression. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> If the output directory path for a file or directory starts with or fully matches a a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key. The final path segment matched can be a partial match of that segment and only the matching portion will be replaced. If there are multiple keys that match, the longest match wins.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>replace_prefixes</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators. <br><br> Replace prefix transformation are the final step in the list of filters and transformations. The final output path of a file or directory being copied into the output directory is determined at this step.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |

View File

View File

@ -0,0 +1,104 @@
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test")
# Test special cases of using "." and "./**" patterns in the root package
copy_to_directory(
name = "dir1",
srcs = [
":a",
"//subpkg:b",
],
include_srcs_packages = [
".",
"./**",
],
)
copy_to_directory(
name = "dir1b",
srcs = [
":a",
"//subpkg:b",
],
)
diff_test(
name = "test1",
file1 = ":dir1",
file2 = ":expected1",
)
diff_test(
name = "test1b",
file1 = ":dir1b",
file2 = ":expected1",
)
copy_to_directory(
name = "dir2",
srcs = [
":a",
"//subpkg:b",
],
include_srcs_packages = [
".",
],
)
copy_to_directory(
name = "dir2b",
srcs = [
":a",
"//subpkg:b",
],
exclude_srcs_packages = [
"./**",
],
)
diff_test(
name = "test2",
file1 = ":dir2",
file2 = ":expected2",
)
diff_test(
name = "test2b",
file1 = ":dir2b",
file2 = ":expected2",
)
copy_to_directory(
name = "dir3",
srcs = [
":a",
"//subpkg:b",
],
include_srcs_packages = [
"./**",
],
)
copy_to_directory(
name = "dir3b",
srcs = [
":a",
"//subpkg:b",
],
exclude_srcs_packages = [
".",
],
)
diff_test(
name = "test3",
file1 = ":dir3",
file2 = ":expected3",
)
diff_test(
name = "test3b",
file1 = ":dir3b",
file2 = ":expected3",
)

View File

@ -0,0 +1,8 @@
local_repository(
name = "aspect_bazel_lib",
path = "../..",
)
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
aspect_bazel_lib_dependencies()

1
e2e/copy_to_directory/a Normal file
View File

@ -0,0 +1 @@
a

View File

@ -0,0 +1 @@
a

View File

@ -0,0 +1 @@
b

View File

@ -0,0 +1 @@
a

View File

@ -0,0 +1 @@
b

View File

@ -0,0 +1 @@
exports_files(["b"])

View File

@ -0,0 +1 @@
b

View File

@ -117,11 +117,16 @@ _copy_to_directory_attr = {
Forward slashes (`/`) should be used as path separators.
A "." value expands to the target's package path (`ctx.label.package`).
A "./**" value expands to the target's package path followed by a slash and a
globstar (`"{{}}/**".format(ctx.label.package)`).
A `"."` value means include srcs that are in the target's package.
It expands to the target's package path (`ctx.label.package`). This
will be an empty string `""` if the target is in the root package.
Defaults to ["**"] which includes sources from all packages.
A `"./**"` value means include srcs that are in subpackages of the target's package.
It expands to the target's package path followed by a slash and a
globstar (`"{{}}/**".format(ctx.label.package)`). If the target's package is
the root package, `"./**"` expands to `["?*", "?*/**"]` instead.
Defaults to `["**"]` which includes sources from all packages.
Files and directories that have matching Bazel packages are subject to subsequent filters and
transformations to determine if they are copied and what their path in the output
@ -142,9 +147,14 @@ _copy_to_directory_attr = {
Forward slashes (`/`) should be used as path separators.
A "." value expands to the target's package path (`ctx.label.package`).
A "./**" value expands to the target's package path followed by a slash and a
globstar (`"{{}}/**".format(ctx.label.package)`).
A `"."` value means exclude srcs that are in the target's package.
It expands to the target's package path (`ctx.label.package`). This
will be an empty string `""` if the target is in the root package.
A `"./**"` value means exclude srcs that are in subpackages of the target's package.
It expands to the target's package path followed by a slash and a
globstar (`"{{}}/**".format(ctx.label.package)`). If the target's package is
the root package, `"./**"` expands to `["?*", "?*/**"]` instead.
Files and directories 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
@ -590,6 +600,22 @@ def _copy_to_directory_impl(ctx):
),
]
def _expand_src_packages_patterns(patterns, package):
result = []
for pattern in patterns:
if pattern == ".":
result.append(package)
elif pattern == "./**":
if package:
result.append("{}/**".format(package))
else:
# special case for the root package so we match on subpackages but
# not on the empty root package itself
result.extend(["?*", "?*/**"])
else:
result.append(pattern)
return result
def copy_to_directory_action(
ctx,
srcs,
@ -661,13 +687,9 @@ def copy_to_directory_action(
# Replace "." in root_paths with the package name of the target
root_paths = [p if p != "." else ctx.label.package for p in root_paths]
# Replace "." in include_srcs_packages & exclude_srcs_packages with the package name of the target
include_srcs_packages = [p if p != "." else ctx.label.package for p in include_srcs_packages]
exclude_srcs_packages = [p if p != "." else ctx.label.package for p in exclude_srcs_packages]
# Replace "./**" in include_srcs_packages & exclude_srcs_packages with the package name of the target followed by slash a globstar
include_srcs_packages = [p if p != "./**" else "{}/**".format(ctx.label.package) for p in include_srcs_packages]
exclude_srcs_packages = [p if p != "./**" else "{}/**".format(ctx.label.package) for p in exclude_srcs_packages]
# Replace "." and "./**" patterns in in include_srcs_packages & exclude_srcs_packages
include_srcs_packages = _expand_src_packages_patterns(include_srcs_packages, ctx.label.package)
exclude_srcs_packages = _expand_src_packages_patterns(exclude_srcs_packages, ctx.label.package)
# Convert and append exclude_prefixes to exclude_srcs_patterns
# TODO(2.0): remove exclude_prefixes this block and in a future breaking release