diff --git a/.aspectci/config.yaml b/.aspectci/config.yaml index 3b0b1b6..f39e676 100644 --- a/.aspectci/config.yaml +++ b/.aspectci/config.yaml @@ -4,5 +4,6 @@ env: workspaces: - . - e2e/bzlmod + - e2e/copy_to_directory tasks: test: diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e5dcb20..9e3c1dd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: diff --git a/docs/copy_to_directory.md b/docs/copy_to_directory.md index 8e3e6d9..8852a8e 100644 --- a/docs/copy_to_directory.md +++ b/docs/copy_to_directory.md @@ -26,10 +26,10 @@ copy_to_directory(name, name | A unique name for this target. | Name | required | | | allow_overwrites | If True, allow files to be overwritten if the same output file is copied to twice.

If set, then the order of srcs matters as the last copy of a particular file will win.

This setting has no effect on Windows where overwrites are always allowed. | Boolean | optional | False | | exclude_prefixes | List of path prefixes (with glob support) to exclude from output directory.

DEPRECATED: use exclude_srcs_patterns instead

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

Files and directories in srcs are not copied to the output directory if their output directory path, after applying root_paths, starts with or fully matches one of the patterns specified.

Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use include_srcs_patterns on files within directories you can use the make_directory_paths helper to specify individual files inside directories in srcs. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.

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

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.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] | -| exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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.

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)).

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.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] | +| exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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.

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

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 directory will be.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] | | exclude_srcs_patterns | List of paths (with glob support) to exclude from output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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

Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use include_srcs_patterns on files within directories you can use the make_directory_paths helper to specify individual files inside directories in srcs. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.

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

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.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] | | include_external_repositories | List of external repository names (with glob support) to include in the output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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.

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.

For example, the following copies @external_repo//path/to:file to path/to/file within the output directory.

         copy_to_directory(             name = "dir",             include_external_repositories = ["external_*"],             srcs = ["@external_repo//path/to:file"],         )         


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.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] | -| include_srcs_packages | List of Bazel packages (with glob support) to include in output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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.

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)).

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 directory will be.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] | +| include_srcs_packages | List of Bazel packages (with glob support) to include in output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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.

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

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.

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 directory will be.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] | | include_srcs_patterns | List of paths (with glob support) to include in output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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

Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use include_srcs_patterns on files within directories you can use the make_directory_paths helper to specify individual files inside directories in srcs. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.

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

Defaults to ["**"] which includes all sources.

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.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] | | out | Path of the output directory, relative to this package.

If not set, the name of the target is used. | String | optional | "" | | replace_prefixes | Map of paths prefixes (with glob support) to replace in the output directory path when copying files.

Glob patterns **, * and ? are supported but the pattern must not end with a ** glob expression. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

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.

Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use replace_prefixes on files within directories you can use the make_directory_paths helper to specify individual files inside directories in srcs. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.

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

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.

Filters and transformations are applied in the following order:

1. include_external_repositories

2. include_srcs_packages

3. exclude_srcs_packages

4. root_paths

5. include_srcs_patterns

6. exclude_srcs_patterns

7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | Dictionary: String -> String | optional | {} | diff --git a/e2e/copy_to_directory/.bazelrc b/e2e/copy_to_directory/.bazelrc new file mode 100644 index 0000000..e69de29 diff --git a/e2e/copy_to_directory/BUILD.bazel b/e2e/copy_to_directory/BUILD.bazel new file mode 100644 index 0000000..f2ac8bd --- /dev/null +++ b/e2e/copy_to_directory/BUILD.bazel @@ -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", +) diff --git a/e2e/copy_to_directory/WORKSPACE b/e2e/copy_to_directory/WORKSPACE new file mode 100644 index 0000000..880fc92 --- /dev/null +++ b/e2e/copy_to_directory/WORKSPACE @@ -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() diff --git a/e2e/copy_to_directory/a b/e2e/copy_to_directory/a new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/e2e/copy_to_directory/a @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/e2e/copy_to_directory/expected1/a b/e2e/copy_to_directory/expected1/a new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/e2e/copy_to_directory/expected1/a @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/e2e/copy_to_directory/expected1/subpkg/b b/e2e/copy_to_directory/expected1/subpkg/b new file mode 100644 index 0000000..63d8dbd --- /dev/null +++ b/e2e/copy_to_directory/expected1/subpkg/b @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/e2e/copy_to_directory/expected2/a b/e2e/copy_to_directory/expected2/a new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/e2e/copy_to_directory/expected2/a @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/e2e/copy_to_directory/expected3/subpkg/b b/e2e/copy_to_directory/expected3/subpkg/b new file mode 100644 index 0000000..63d8dbd --- /dev/null +++ b/e2e/copy_to_directory/expected3/subpkg/b @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/e2e/copy_to_directory/subpkg/BUILD.bazel b/e2e/copy_to_directory/subpkg/BUILD.bazel new file mode 100644 index 0000000..35b7175 --- /dev/null +++ b/e2e/copy_to_directory/subpkg/BUILD.bazel @@ -0,0 +1 @@ +exports_files(["b"]) diff --git a/e2e/copy_to_directory/subpkg/b b/e2e/copy_to_directory/subpkg/b new file mode 100644 index 0000000..63d8dbd --- /dev/null +++ b/e2e/copy_to_directory/subpkg/b @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/lib/private/copy_to_directory.bzl b/lib/private/copy_to_directory.bzl index 4ad4503..ac94e6a 100644 --- a/lib/private/copy_to_directory.bzl +++ b/lib/private/copy_to_directory.bzl @@ -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