From 01dfcc17b3ebc4cfe60977146f03b4a93d0a5731 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Sat, 9 Oct 2021 12:42:53 -0700 Subject: [PATCH] fix: allow multiple copy_file in the same package Previously this failed on windows, as we wrote colliding batch files --- rules/private/copy_file_private.bzl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rules/private/copy_file_private.bzl b/rules/private/copy_file_private.bzl index 44f133a..9133bc7 100644 --- a/rules/private/copy_file_private.bzl +++ b/rules/private/copy_file_private.bzl @@ -19,13 +19,19 @@ cmd.exe (on Windows). '_copy_xfile' marks the resulting file executable, '_copy_file' does not. """ +def _hash_file(file): + return str(hash(file.path)) + def copy_cmd(ctx, src, dst): # Most Windows binaries built with MSVC use a certain argument quoting # scheme. Bazel uses that scheme too to quote arguments. However, # cmd.exe uses different semantics, so Bazel's quoting is wrong here. # To fix that we write the command to a .bat file so no command line # quoting or escaping is required. - bat = ctx.actions.declare_file(ctx.label.name + "-cmd.bat") + # Put a hash of the file name into the name of the generated batch file to + # make it unique within the package, so that users can define multiple copy_file's. + bat = ctx.actions.declare_file("%s-%s-cmd.bat" % (ctx.label.name, _hash_file(src))) + ctx.actions.write( output = bat, # Do not use lib/shell.bzl's shell.quote() method, because that uses