Skip to content

Commit

Permalink
feat: add support for capturing and overriding the exit code within r…
Browse files Browse the repository at this point in the history
…un_node
  • Loading branch information
mattem committed Jul 1, 2020
1 parent becd9bc commit d4f39f6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ USER_NODE_OPTIONS=()
ALL_ARGS=(TEMPLATED_args $NODE_REPOSITORY_ARGS "$@")
STDOUT_CAPTURE=""
STDERR_CAPTURE=""
EXIT_CODE_CAPTURE=""
OVERRIDE_EXIT_CODE="-1"

RUN_LINKER=true
NODE_PATCHES=true
Expand All @@ -179,8 +181,14 @@ for ARG in "${ALL_ARGS[@]:-}"; do
case "$ARG" in
# Supply custom linker arguments for first-party dependencies
--bazel_node_modules_manifest=*) MODULES_MANIFEST="${ARG#--bazel_node_modules_manifest=}" ;;
# Captures stdout of the node process to the file specified
--bazel_capture_stdout=*) STDOUT_CAPTURE="${ARG#--bazel_capture_stdout=}" ;;
# Captures stderr of the node process to the file specified
--bazel_capture_stderr=*) STDERR_CAPTURE="${ARG#--bazel_capture_stderr=}" ;;
# Captures the exit code of the node process to the file specified
--bazel_capture_exit_code=*) EXIT_CODE_CAPTURE="${ARG#--bazel_capture_exit_code=}" ;;
# Overrides the exit code of the node process
--bazel_override_exit_code=*) OVERRIDE_EXIT_CODE="${ARG#--bazel_override_exit_code=}" ;;
# Disable the node_loader.js monkey patches for require()
# Note that this means you need an explicit runfiles helper library
--nobazel_patch_module_resolver) PATCH_REQUIRE=false ;;
Expand Down Expand Up @@ -327,6 +335,10 @@ wait "${child}"
RESULT="$?"
set -e

if [[ -n "${EXIT_CODE_CAPTURE}" ]]; then
echo "${RESULT}" > "${EXIT_CODE_CAPTURE}"
fi

if [ "${EXPECTED_EXIT_CODE}" != "0" ]; then
if [ ${RESULT} != ${EXPECTED_EXIT_CODE} ]; then
echo "Expected exit code to be ${EXPECTED_EXIT_CODE}, but got ${RESULT}" >&2
Expand Down Expand Up @@ -360,4 +372,8 @@ if [[ -n "${COVERAGE_DIR:-}" ]]; then
fi
fi

if [[ "${OVERRIDE_EXIT_CODE}" -gt "-1" ]]; then
exit ${OVERRIDE_EXIT_CODE}
fi

exit ${RESULT}
11 changes: 11 additions & 0 deletions internal/node/npm_package_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ _ATTRS = {
"args": attr.string_list(mandatory = True),
"configuration_env_vars": attr.string_list(default = []),
"data": attr.label_list(allow_files = True, aspects = [module_mappings_aspect, node_modules_aspect]),
"exit_code_out": attr.output(),
"output_dir": attr.bool(),
"outs": attr.output_list(),
"override_exit_code": attr.int(default = -1),
"stderr": attr.output(),
"stdout": attr.output(),
"tool": attr.label(
Expand Down Expand Up @@ -64,6 +66,9 @@ def _impl(ctx):
if ctx.outputs.stderr:
tool_outputs.append(ctx.outputs.stderr)

if ctx.outputs.exit_code_out:
tool_outputs.append(ctx.outputs.exit_code_out)

run_node(
ctx,
executable = "tool",
Expand All @@ -73,6 +78,8 @@ def _impl(ctx):
configuration_env_vars = ctx.attr.configuration_env_vars,
stdout = ctx.outputs.stdout,
stderr = ctx.outputs.stderr,
exit_code_out = ctx.outputs.exit_code_out,
override_exit_code = ctx.attr.override_exit_code,
)

return [DefaultInfo(files = depset(outputs + tool_outputs))]
Expand Down Expand Up @@ -104,6 +111,10 @@ def npm_package_bin(tool = None, package = None, package_bin = None, data = [],
subject to the same semantics as `outs`
stdout: set to capture the stdout of the binary to a file, which can later be used as an input to another target
subject to the same semantics as `outs`
exit_code_out: set to capture the exit code of the binary to a file, which can later be used as an input to another target
subject to the same semantics as `outs`
override_exit_code: allows the exit code of the binary to be overridden by the caller, for example to allow subsequent actions
to run when used in conjunction with exit_code_out. Note that this doesn't affect the contents of exit_code_out
args: Command-line arguments to the tool.
Expand Down
8 changes: 8 additions & 0 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ generated_file_test(

# capture stderr to diagnostics.out, then analyze it in terser_diagnostics_stats printing some stats
# stdout is captured into greeter.min.js (again, without the --output $@ flags)
# the exit code is also capture to a file with exit_code_out, allowing downstream actions to read it
npm_package_bin(
name = "diagnostics_producing_bin",
args = [
Expand All @@ -269,6 +270,7 @@ npm_package_bin(
"--warn",
],
data = ["terser_input_with_diagnostics.js"],
exit_code_out = "exit.code",
package = "terser",
stderr = "diagnostics.out",
stdout = "greeter.min.js",
Expand Down Expand Up @@ -299,6 +301,12 @@ generated_file_test(
generated = ":diagnostics.scrubbed.out",
)

generated_file_test(
name = "exit_code_test",
src = "exit_code_test.golden",
generated = ":exit.code",
)

nodejs_binary(
name = "copy_to_directory",
entry_point = "copy_to_directory.js",
Expand Down
1 change: 1 addition & 0 deletions internal/node/test/exit_code_test.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
9 changes: 9 additions & 0 deletions internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def run_node(ctx, inputs, arguments, executable, **kwargs):
add_arg(arguments, "--bazel_capture_stderr=%s" % stderr_file.path)
outputs = outputs + [stderr_file]

exit_code_file = kwargs.pop("exit_code_out", None)
if exit_code_file:
add_arg(arguments, "--bazel_capture_exit_code=%s" % exit_code_file.path)
outputs = outputs + [exit_code_file]

override_exit_code = kwargs.pop("override_exit_code", None)
if override_exit_code != None:
add_arg(arguments, "--bazel_override_exit_code=%s" % override_exit_code)

# By using the run_node helper, you suggest that your program
# doesn't implicitly use runfiles to require() things
# To access runfiles, you must use a runfiles helper in the program instead
Expand Down

0 comments on commit d4f39f6

Please sign in to comment.