Copies files and directories to an output directory.

Files and directories can be arranged as needed in the output directory using the root_paths, include_srcs_patters, exclude_srcs_patters and replace_prefixes attributes.

copy_to_directory

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
allow_overwritesIf 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.
BooleanoptionalFalse
exclude_prefixesList of path prefixes (with glob support) to exclude from output directory.

DEPRECATED: use exclude_srcs_patterns instead

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

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.

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[]
exclude_srcs_packagesList 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.

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[]
exclude_srcs_patternsList 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.

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[]
include_external_repositoriesList of external repository names (with glob support) to include in the output directory.

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 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 from an external repository is not included in the output directory path and is considered in subsequent filters and transformations.

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[]
include_srcs_packagesList of Bazel packages (with glob support) to include in output directory.

Files in srcs are only 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.

Defaults to [“**”] which includes sources from all packages.

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

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[“**”]
include_srcs_patternsList of paths (with glob support) to include in output directory.

Files in srcs are only 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.

Defaults to [“**”] which includes all sources.

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

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[“**”]
outPath of the output directory, relative to this package.

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

If the output directory path for a file 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.

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 being copied into the output directory is determined at this step.

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 -> Stringoptional{}
root_pathsList of paths (with glob support) that are roots in the output directory.

If any parent directory of a file being copied matches one of the root paths patterns specified, the output directory path will be the path relative to the root path instead of the path relative to the file‘s workspace. If there are multiple root paths that match, the longest match wins.

Matching is done on the parent directory of the output file path so a trailing ‘**’ glob patterm will match only up to the last path segment of the dirname and will not include the basename. Only complete path segments are matched. Partial matches on the last segment of the root path are ignored.

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

A “.” value expands to the target’s package path (ctx.label.package).

Defaults to [“.”] which results in the output directory path of files in the target‘s package and and sub-packages are relative to the target’s package and files outside of that retain their full workspace relative paths.

Glob patterns are supported. Standard wildcards (globbing patterns) plus the ** doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by gazelle. See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.

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 stringsoptional[“.”]
srcsFiles and/or directories or targets that provide DirectoryPathInfo to copy into the output directory.List of labelsoptional[]
verboseIf true, prints out verbose logs to stdoutBooleanoptionalFalse

copy_to_directory_action

Legacy helper function to copy files to a directory.

This helper calculates copy paths in Starlark during analysis and performs the copies in a bash/bat script. For improved analysis and runtime performance, it is recommended the switch to copy_to_directory_bin_action which calculates copy paths and performs copies with a tool binary, typically the @aspect_bazel_lib//tools/copy_to_directory go_binary either built from source or provided by a toolchain.

This helper is used by copy_to_directory. It is exposed as a public API so it can be used within other rule implementations where additional_files can also be passed in.

PARAMETERS

NameDescriptionDefault Value
ctxThe rule context.none
srcsFiles and/or directories or targets that provide DirectoryPathInfo to copy into the output directory.none
dstThe directory to copy to. Must be a TreeArtifact.none
additional_filesList or depset of additional files to copy that are not in the DefaultInfo or DirectoryPathInfo of srcs[]
root_pathsList of paths that are roots in the output directory.

See copy_to_directory rule documentation for more details.
[“.”]
include_external_repositoriesList of external repository names to include in the output directory.

See copy_to_directory rule documentation for more details.
[]
include_srcs_packagesList of Bazel packages to include in output directory.

See copy_to_directory rule documentation for more details.
[“**”]
exclude_srcs_packagesList of Bazel packages (with glob support) to exclude from output directory.

See copy_to_directory rule documentation for more details.
[]
include_srcs_patternsList of paths (with glob support) to include in output directory.

See copy_to_directory rule documentation for more details.
[“**”]
exclude_srcs_patternsList of paths (with glob support) to exclude from output directory.

See copy_to_directory rule documentation for more details.
[]
exclude_prefixesList of path prefixes to exclude from output directory.

See copy_to_directory rule documentation for more details.
[]
replace_prefixesMap of paths prefixes to replace in the output directory path when copying files.

See copy_to_directory rule documentation for more details.
{}
allow_overwritesIf True, allow files to be overwritten if the same output file is copied to twice.

See copy_to_directory rule documentation for more details.
False
is_windowsDeprecated and unusedNone

copy_to_directory_bin_action

Helper function to copy files to a directory using a tool binary.

The tool binary will typically be the @aspect_bazel_lib//tools/copy_to_directory go_binary either built from source or provided by a toolchain.

This helper is used by copy_to_directory. It is exposed as a public API so it can be used within other rule implementations where additional_files can also be passed in.

PARAMETERS

NameDescriptionDefault Value
ctxThe rule context.none
nameName of target creating this action used for config file generation.none
dstThe directory to copy to. Must be a TreeArtifact.none
copy_to_directory_binCopy to directory tool binary.none
filesList of files to copy into the output directory.[]
targetsList of targets that provide DirectoryPathInfo to copy into the output directory.[]
root_pathsList of paths that are roots in the output directory.

See copy_to_directory rule documentation for more details.
[“.”]
include_external_repositoriesList of external repository names to include in the output directory.

See copy_to_directory rule documentation for more details.
[]
include_srcs_packagesList of Bazel packages to include in output directory.

See copy_to_directory rule documentation for more details.
[“**”]
exclude_srcs_packagesList of Bazel packages (with glob support) to exclude from output directory.

See copy_to_directory rule documentation for more details.
[]
include_srcs_patternsList of paths (with glob support) to include in output directory.

See copy_to_directory rule documentation for more details.
[“**”]
exclude_srcs_patternsList of paths (with glob support) to exclude from output directory.

See copy_to_directory rule documentation for more details.
[]
exclude_prefixesList of path prefixes to exclude from output directory.

See copy_to_directory rule documentation for more details.
[]
replace_prefixesMap of paths prefixes to replace in the output directory path when copying files.

See copy_to_directory rule documentation for more details.
{}
allow_overwritesIf True, allow files to be overwritten if the same output file is copied to twice.

See copy_to_directory rule documentation for more details.
False
verboseIf true, prints out verbose logs to stdoutFalse

copy_to_directory_lib.impl

PARAMETERS

NameDescriptionDefault Value
ctx - none