From 3c0dbd589575a7a3833096a36989b212d69910e3 Mon Sep 17 00:00:00 2001 From: Josh Giles Date: Tue, 11 Jun 2024 03:30:53 -0400 Subject: [PATCH] fix: Directory hidden files in write_source_file. (#860) * Fix #667: Dir hidden files in write_source_file. Copy and manage hidden files starting with "." in write_source_files. Previously these files were not supported if they were in the top level of the directory to copy. * Add test and fix error messages from cp, chmod. * Also fix executable dir case. * Fix issue with copying directory rather than contents. --- lib/private/write_source_file.bzl | 10 +++++----- lib/tests/write_source_files/BUILD.bazel | 19 +++++++++++++++++++ .../write_source_files/hidden_dir/.hidden | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 lib/tests/write_source_files/hidden_dir/.hidden diff --git a/lib/private/write_source_file.bzl b/lib/private/write_source_file.bzl index d5ff41e..f5e85d2 100644 --- a/lib/private/write_source_file.bzl +++ b/lib/private/write_source_file.bzl @@ -201,7 +201,7 @@ fi"""] if ctx.attr.executable: executable_file = "chmod +x \"$out\"" - executable_dir = "chmod -R +x \"$out\"/*" + executable_dir = "chmod -R +x \"$out\"" else: executable_file = "chmod -x \"$out\"" if is_macos: @@ -209,7 +209,7 @@ fi"""] executable_dir = "find \"$out\" -type f | xargs chmod -x" else: # Remove execute/search bit recursively from files bit not directories: https://superuser.com/a/434418 - executable_dir = "chmod -R -x+X \"$out\"/*" + executable_dir = "chmod -R -x+X \"$out\"" for in_path, out_path in paths: contents.append(""" @@ -231,10 +231,10 @@ else echo "Copying directory $in to $out in $PWD" # in case `cp` from previous command was terminated midway which can result in read-only files/dirs chmod -R ug+w "$out" > /dev/null 2>&1 || true - rm -Rf "$out"/* + rm -Rf "$out"/{{*,.[!.]*}} mkdir -p "$out" - cp -fRL "$in"/* "$out" - chmod -R ug+w "$out"/* + cp -fRL "$in"/. "$out" + chmod -R ug+w "$out" {executable_dir} fi """.format( diff --git a/lib/tests/write_source_files/BUILD.bazel b/lib/tests/write_source_files/BUILD.bazel index fca6e1f..56a5486 100644 --- a/lib/tests/write_source_files/BUILD.bazel +++ b/lib/tests/write_source_files/BUILD.bazel @@ -101,6 +101,19 @@ output_files( target = ":g_h-desired", ) +genrule( + name = "hidden-contained", + outs = [".hidden"], + cmd = "echo 'hidden file test' > $@", +) + +copy_to_directory( + name = "hidden_dir-desired", + srcs = [ + ":hidden-contained", + ], +) + write_source_file_test( name = "a_test", in_file = ":a-desired", @@ -137,6 +150,12 @@ write_source_file_test( out_file = "g.js", ) +write_source_file( + name = "hidden_dir_test", + in_file = ":hidden_dir-desired", + out_file = "hidden_dir", +) + write_source_files( name = "macro_smoke_test", additional_update_targets = [ diff --git a/lib/tests/write_source_files/hidden_dir/.hidden b/lib/tests/write_source_files/hidden_dir/.hidden new file mode 100644 index 0000000..cc889dc --- /dev/null +++ b/lib/tests/write_source_files/hidden_dir/.hidden @@ -0,0 +1 @@ +hidden file test