fix: improve handling of duplicate files in copy_to_directory (#205)

This commit is contained in:
Greg Magolan 2022-08-01 09:56:52 -07:00 committed by GitHub
parent c4f6fcff42
commit 06d54eef4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View File

@ -412,6 +412,25 @@ def _copy_paths(
return src_path, output_path, src_file
def _merge_into_copy_path(copy_paths, src_path, dst_path, src_file):
for i, s in enumerate(copy_paths):
_, maybe_dst_path, maybe_src_file = s
if dst_path == maybe_dst_path:
if src_file == maybe_src_file:
return True
if src_file.short_path == maybe_src_file.short_path:
if maybe_src_file.is_source and not src_file.is_source:
# If the files are the at the same path but one in the source tree and one in
# the output tree, always copy the output tree file. This is also the default
# Bazel behavior for layout out runfiles if there are files that have the same
# path in the source tree and the output tree. This can happen, for example, if
# the source file and a generated file that is a copy to the source file are
# both added to the package which can happen, for example, through 'additional_files'
# in 'copy_to_directory_action'.
copy_paths[i] = (src_path, dst_path, src_file)
return True
return False
def _copy_to_dir_bash(ctx, copy_paths, dst_dir, allow_overwrites):
cmds = [
"set -o errexit -o nounset -o pipefail",
@ -682,7 +701,8 @@ def copy_to_directory_action(
)
if src_path != None:
dst_path = skylib_paths.normalize("/".join([dst.path, output_path]))
copy_paths.append((src_path, dst_path, src_file))
if not _merge_into_copy_path(copy_paths, src_path, dst_path, src_file):
copy_paths.append((src_path, dst_path, src_file))
if DefaultInfo in src:
for src_file in src[DefaultInfo].files.to_list():
found_input_paths = True
@ -698,11 +718,9 @@ def copy_to_directory_action(
)
if src_path != None:
dst_path = skylib_paths.normalize("/".join([dst.path, output_path]))
copy_paths.append((src_path, dst_path, src_file))
if not _merge_into_copy_path(copy_paths, src_path, dst_path, src_file):
copy_paths.append((src_path, dst_path, src_file))
for additional_file in additional_files:
if additional_file in ctx.files.srcs:
# already added above
continue
found_input_paths = True
src_path, output_path, src_file = _copy_paths(
src = additional_file,
@ -716,7 +734,8 @@ def copy_to_directory_action(
)
if src_path != None:
dst_path = skylib_paths.normalize("/".join([dst.path, output_path]))
copy_paths.append((src_path, dst_path, src_file))
if not _merge_into_copy_path(copy_paths, src_path, dst_path, src_file):
copy_paths.append((src_path, dst_path, src_file))
if not found_input_paths:
fail("No files or directories found in srcs.")

View File

@ -1,14 +1,23 @@
load("//lib:diff_test.bzl", "diff_test")
load("//lib:copy_to_directory.bzl", "copy_to_directory")
load("//lib:copy_to_bin.bzl", "copy_to_bin")
load(":lib.bzl", "lib")
load(":pkg.bzl", "pkg")
copy_to_bin(
name = "copy_1",
srcs = ["1"],
)
lib(
name = "lib",
srcs = ["1"],
# intentionally dup on "1" to make sure it is gracefully handled
others = [
"1",
# also pass in a copy_to_bin copy of "1" to spice things up;
# this case is handled in the fix in https://github.com/aspect-build/bazel-lib/pull/205
"copy_1",
"2",
],
)