From 08385965e048bef2f302302a86764342ca201ae4 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 4 Mar 2020 10:31:08 +0100 Subject: [PATCH 1/6] windows_utils.bzl: Escaping \ and " before passing args to bash script with quote --- internal/common/windows_utils.bzl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/common/windows_utils.bzl b/internal/common/windows_utils.bzl index d41467b5ef..660f2cd990 100644 --- a/internal/common/windows_utils.bzl +++ b/internal/common/windows_utils.bzl @@ -96,7 +96,10 @@ set RUNFILES_MANIFEST_ONLY=1 call :rlocation "{sh_script}" run_script for %%a in ("{bash_bin}") do set "bash_bin_dir=%%~dpa" set PATH=%bash_bin_dir%;%PATH% -"{bash_bin}" -c "!run_script! %*" +set args=%* +set args=%args:\=\\% +set args=%args:"=\"% +"{bash_bin}" -c "!run_script! %args%" """.format( bash_bin = ctx.toolchains["@bazel_tools//tools/sh:toolchain_type"].path, sh_script = _to_manifest_path(ctx, shell_script), From 9efad5fd2bf9e2e21959190ae39e1369682c5ff0 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 4 Mar 2020 10:42:57 +0100 Subject: [PATCH 2/6] Escape \ twice --- internal/common/windows_utils.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/windows_utils.bzl b/internal/common/windows_utils.bzl index 660f2cd990..03735ae554 100644 --- a/internal/common/windows_utils.bzl +++ b/internal/common/windows_utils.bzl @@ -97,7 +97,7 @@ call :rlocation "{sh_script}" run_script for %%a in ("{bash_bin}") do set "bash_bin_dir=%%~dpa" set PATH=%bash_bin_dir%;%PATH% set args=%* -set args=%args:\=\\% +set args=%args:\=\\\\% set args=%args:"=\"% "{bash_bin}" -c "!run_script! %args%" """.format( From 5a19625b405325a46ee52ca444ede05891382bac Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 4 Mar 2020 11:57:03 +0100 Subject: [PATCH 3/6] Fix binary_with_no_arguments test --- internal/common/windows_utils.bzl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/common/windows_utils.bzl b/internal/common/windows_utils.bzl index 03735ae554..ebf840c0e1 100644 --- a/internal/common/windows_utils.bzl +++ b/internal/common/windows_utils.bzl @@ -97,9 +97,11 @@ call :rlocation "{sh_script}" run_script for %%a in ("{bash_bin}") do set "bash_bin_dir=%%~dpa" set PATH=%bash_bin_dir%;%PATH% set args=%* -set args=%args:\=\\\\% -set args=%args:"=\"% -"{bash_bin}" -c "!run_script! %args%" +if defined args ( + set args=!args:\=\\\\! + set args=!args:"=\"! +) +"{bash_bin}" -c "!run_script! !args!" """.format( bash_bin = ctx.toolchains["@bazel_tools//tools/sh:toolchain_type"].path, sh_script = _to_manifest_path(ctx, shell_script), From 878a7ac38637debcf4437186042070a0e1f5675a Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 4 Mar 2020 14:54:21 +0100 Subject: [PATCH 4/6] Add test for passing cmd args --- internal/common/test/BUILD.bazel | 17 +++++- internal/common/test/print_cmd_args.js | 23 ++++++++ internal/common/test/test_pass_cmd_args.sh | 64 ++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 internal/common/test/print_cmd_args.js create mode 100644 internal/common/test/test_pass_cmd_args.sh diff --git a/internal/common/test/BUILD.bazel b/internal/common/test/BUILD.bazel index 2d174325b8..f9e585f0d8 100644 --- a/internal/common/test/BUILD.bazel +++ b/internal/common/test/BUILD.bazel @@ -1,4 +1,4 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test") +load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test") load("//internal/common:copy_to_bin.bzl", "copy_to_bin") load("//internal/common:params_file.bzl", "params_file") @@ -48,3 +48,18 @@ nodejs_test( entry_point = ":check_params_file.js", templated_args = ["$(location :params_file.out)"], ) + +nodejs_binary( + name = "print_cmd_args", + entry_point = ":print_cmd_args.js", +) + +sh_test( + name = "test_pass_cmd_args", + srcs = ["test_pass_cmd_args.sh"], + data = [ + ":print_cmd_args", + "//third_party/github.com/bazelbuild/bazel-skylib:tests/unittest.bash", + ], + deps = ["@bazel_tools//tools/bash/runfiles"], +) diff --git a/internal/common/test/print_cmd_args.js b/internal/common/test/print_cmd_args.js new file mode 100644 index 0000000000..f3efdf4c12 --- /dev/null +++ b/internal/common/test/print_cmd_args.js @@ -0,0 +1,23 @@ +/** + * @license + * Copyright 2017 The Bazel Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const process = require('process') + + +for (var i=2; i < process.argv.length; i += 1) { + console.log("arg" + i + "=(" + process.argv[i] + ")"); +} diff --git a/internal/common/test/test_pass_cmd_args.sh b/internal/common/test/test_pass_cmd_args.sh new file mode 100644 index 0000000000..5197cbf07a --- /dev/null +++ b/internal/common/test/test_pass_cmd_args.sh @@ -0,0 +1,64 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v2 --- + +source "$(rlocation build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel-skylib/tests/unittest.bash)" \ + || { echo "Could not source build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel-skylib/tests/unittest.bash" >&2; exit 1; } + +case "$(uname -s | tr [:upper:] [:lower:])" in +msys*|mingw*|cygwin*) + declare -r is_windows=true + ;; +*) + declare -r is_windows=false + ;; +esac + +if "$is_windows"; then + export MSYS_NO_PATHCONV=1 + export MSYS2_ARG_CONV_EXCL="*" +fi + +function test_pass_cmd_args() { + if "$is_windows"; then + script=build_bazel_rules_nodejs/internal/common/test/print_cmd_args.bat + else + script=build_bazel_rules_nodejs/internal/common/test/print_cmd_args.sh + fi + + "$(rlocation ${script})" '/foo bar' "\\foo \\bar" "/foo bar/" \foo\bar /foo/bar \\foo\\bar "\foo\bar" "\\foo\\bar" '\foo\bar' '\\foo\\bar' >"$TEST_log" + + expect_log 'arg2=(/foo bar)' + expect_log 'arg3=(\\foo \\bar)' + expect_log 'arg4=(/foo bar/)' + expect_log 'arg5=(foobar)' + expect_log 'arg6=(/foo/bar)' + expect_log 'arg7=(\\foo\\bar)' + expect_log 'arg8=(\\foo\\bar)' + expect_log 'arg9=(\\foo\\bar)' + expect_log 'arg10=(\\foo\\bar)' + expect_log 'arg11=(\\\\foo\\\\bar)' +} + +run_suite "test_pass_cmd_args test suite" From 0c0e4fb20c7b5c12ec06d383be14b2b053a90c59 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 4 Mar 2020 15:01:30 +0100 Subject: [PATCH 5/6] Some fixes --- internal/common/test/test_pass_cmd_args.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 internal/common/test/test_pass_cmd_args.sh diff --git a/internal/common/test/test_pass_cmd_args.sh b/internal/common/test/test_pass_cmd_args.sh old mode 100644 new mode 100755 index 5197cbf07a..7fed151fda --- a/internal/common/test/test_pass_cmd_args.sh +++ b/internal/common/test/test_pass_cmd_args.sh @@ -48,7 +48,7 @@ function test_pass_cmd_args() { fi "$(rlocation ${script})" '/foo bar' "\\foo \\bar" "/foo bar/" \foo\bar /foo/bar \\foo\\bar "\foo\bar" "\\foo\\bar" '\foo\bar' '\\foo\\bar' >"$TEST_log" - + expect_log 'arg2=(/foo bar)' expect_log 'arg3=(\\foo \\bar)' expect_log 'arg4=(/foo bar/)' From 4e279a5cbbfb3d2fdc9cc32f1724530387e4bb5e Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 5 Mar 2020 10:54:08 +0100 Subject: [PATCH 6/6] Address reviewer comments --- internal/common/test/print_cmd_args.js | 3 --- internal/common/windows_utils.bzl | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/common/test/print_cmd_args.js b/internal/common/test/print_cmd_args.js index f3efdf4c12..bc285bc20d 100644 --- a/internal/common/test/print_cmd_args.js +++ b/internal/common/test/print_cmd_args.js @@ -15,9 +15,6 @@ * limitations under the License. */ -const process = require('process') - - for (var i=2; i < process.argv.length; i += 1) { console.log("arg" + i + "=(" + process.argv[i] + ")"); } diff --git a/internal/common/windows_utils.bzl b/internal/common/windows_utils.bzl index ebf840c0e1..1ae88c2021 100644 --- a/internal/common/windows_utils.bzl +++ b/internal/common/windows_utils.bzl @@ -97,6 +97,7 @@ call :rlocation "{sh_script}" run_script for %%a in ("{bash_bin}") do set "bash_bin_dir=%%~dpa" set PATH=%bash_bin_dir%;%PATH% set args=%* +rem Escape \ and * in args before passsing it with double quote if defined args ( set args=!args:\=\\\\! set args=!args:"=\"!