From 71a27f64eeca77ff4a0362ec2043d07a711249e3 Mon Sep 17 00:00:00 2001 From: Pras Velagapudi Date: Sun, 6 Nov 2022 08:55:19 -0500 Subject: [PATCH 1/2] De-duplicate run args passed to bazel image targets. When 'bazel run' is called, it expects that the args specified in the BUILD file are not known to the run command and will prepend them to all other arguments. However, we have already baked those commands into the image, because users expect that 'args' in the case of an image target will be included in the image. This templated script de-duplicates the args specified in the BUILD file by passing itself the number of args already included in the image. --- container/incremental_load.sh.tpl | 10 ++++++++++ container/layer_tools.bzl | 1 + 2 files changed, 11 insertions(+) diff --git a/container/incremental_load.sh.tpl b/container/incremental_load.sh.tpl index b0e0157c8..ff727ac48 100644 --- a/container/incremental_load.sh.tpl +++ b/container/incremental_load.sh.tpl @@ -249,6 +249,16 @@ function read_variables() { # This is not executed if the single argument --norun is passed or # no run_statements are generated (in which case, 'run' is 'False'). if [[ "%{run}" == "True" ]]; then + # When 'bazel run' is called, it expects that the args specified in the BUILD + # file are not known to the run command and will prepend them to all other + # arguments. + # + # However, we have already baked those commands into the image, because users + # expect that 'args' in the case of an image target will be included in the + # image. This templated script de-duplicates the args specified in the BUILD + # file by passing itself the number of args already included in the image. + shift "%{run_num_args}" + docker_args=() container_args=() diff --git a/container/layer_tools.bzl b/container/layer_tools.bzl index 33ab3fb8c..4bf413b49 100644 --- a/container/layer_tools.bzl +++ b/container/layer_tools.bzl @@ -290,6 +290,7 @@ def incremental_load( "%{docker_flags}": " ".join(toolchain_info.docker_flags), "%{docker_tool_path}": docker_path(toolchain_info), "%{load_statements}": "\n".join(load_statements), + "%{run_num_args}": str(len(ctx.attr.args)), "%{run_statement}": run_statement, "%{run_tag}": run_tag, "%{run}": str(run), From 0a0627272081ba8078ddf1dcb5b832a7a5777c43 Mon Sep 17 00:00:00 2001 From: Pras Velagapudi Date: Sun, 6 Nov 2022 09:02:43 -0500 Subject: [PATCH 2/2] make comments clearer --- container/incremental_load.sh.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/incremental_load.sh.tpl b/container/incremental_load.sh.tpl index ff727ac48..18513fba8 100644 --- a/container/incremental_load.sh.tpl +++ b/container/incremental_load.sh.tpl @@ -256,7 +256,7 @@ if [[ "%{run}" == "True" ]]; then # However, we have already baked those commands into the image, because users # expect that 'args' in the case of an image target will be included in the # image. This templated script de-duplicates the args specified in the BUILD - # file by passing itself the number of args already included in the image. + # file by skipping the number of args already included in the image command. shift "%{run_num_args}" docker_args=()