Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow multiple copy_file in the same package #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion rules/private/copy_file_private.bzl
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: users of the public API wouldn't observe a problem because only one declare_file happens per copy_file rule, and so ctx.label.name is sufficient to disambiguate.

We hit this in a scenario where a rule uses this library code and creates multiple actions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: users of the public API wouldn't observe a problem because only one declare_file happens per copy_file rule, and so ctx.label.name is sufficient to disambiguate.

We hit this in a scenario where a rule uses this library code and creates multiple actions.

# 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
Expand Down