Skip to content

Commit

Permalink
Simplify node_launcher and fix signal propogation.
Browse files Browse the repository at this point in the history
  • Loading branch information
globegitter authored and gregmagolan committed Jan 23, 2019
1 parent cf50b71 commit c124496
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
49 changes: 23 additions & 26 deletions internal/node/node_launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,35 @@ for ARG in "${ALL_ARGS[@]}"; do
esac
done

# Note: Bash does not forward termination signals to any child process when
# running in docker so need to manually trap and forward the signals
_term() {
kill -TERM "$child" 2>/dev/null
}

_int() {
kill -INT "$child" 2>/dev/null
}
# The EXPECTED_EXIT_CODE lets us write bazel tests which assert that
# a binary fails to run. Otherwise any failure would make such a test
# fail before we could assert that we expected that failure.
readonly EXPECTED_EXIT_CODE="TEMPLATED_expected_exit_code"
if [ "${EXPECTED_EXIT_CODE}" -eq "0" ]; then
# Replace the current process (bash) with a node process.
# This means that stdin, stdout, signals, etc will be transparently
# handled by the node process.
# If we had merely forked a child process here, we'd be responsible
# for forwarding those OS interactions.
exec "${node}" "${NODE_OPTIONS[@]}" "${script}" "${ARGS[@]}"
# exec terminates execution of this shell script, nothing later will run.
fi

set +e
"${node}" "${NODE_OPTIONS[@]}" "${script}" "${ARGS[@]}" <&0 &
child=$!
trap _term SIGTERM
trap _int SIGINT
wait "$child"
RESULT="$?"
set -e

readonly EXPECTED_EXIT_CODE="TEMPLATED_expected_exit_code"
if [ "${EXPECTED_EXIT_CODE}" -ne "0" ]; then
if (( ${RESULT} != ${EXPECTED_EXIT_CODE} )); then
echo "Expected exit code to be ${EXPECTED_EXIT_CODE}, but got ${RESULT}" >&2
if [ "${RESULT}" -eq "0" ]; then
# This exit code is handled specially by Bazel:
# https://github.com/bazelbuild/bazel/blob/486206012a664ecb20bdb196a681efc9a9825049/src/main/java/com/google/devtools/build/lib/util/ExitCode.java#L44
readonly BAZEL_EXIT_TESTS_FAILED = 3;
exit ${BAZEL_EXIT_TESTS_FAILED}
fi
else
exit 0
if (( ${RESULT} != ${EXPECTED_EXIT_CODE} )); then
echo "Expected exit code to be ${EXPECTED_EXIT_CODE}, but got ${RESULT}" >&2
if [ "${RESULT}" -eq "0" ]; then
# This exit code is handled specially by Bazel:
# https://github.com/bazelbuild/bazel/blob/486206012a664ecb20bdb196a681efc9a9825049/src/main/java/com/google/devtools/build/lib/util/ExitCode.java#L44
readonly BAZEL_EXIT_TESTS_FAILED = 3;
exit ${BAZEL_EXIT_TESTS_FAILED}
fi
else
exit 0
fi

exit ${RESULT}
exit ${RESULT}
16 changes: 2 additions & 14 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,17 @@ def _prepare_node(repository_ctx):

# The entry points for node for osx/linux and windows
if not is_windows:
# Sets process.env['PATH'] for node, npm & yarn and runs user script
# This extra step is needed as process.env['PATH'] needs to be set
# in some cases on osx/linux and in other cases PATH set in
# bin/node is sufficient. The first argument is the PATH to prepend.
repository_ctx.file("bin/node.js", content = """//Generated by node_repositories.bzl
const {spawn} = require('child_process');
process.env['PATH'] = `${process.argv[2]}:${process.env['PATH']}`;
const proc = spawn(process.argv0, process.argv.slice(3), {stdio: [process.stdin, process.stdout, process.stderr]});
proc.on('close', (code) => { process.exit(code); });
""")

# Sets PATH and runs bin/node.js passing all arguments
# Sets PATH and runs the application
repository_ctx.file("bin/node", content = """#!/usr/bin/env bash
# Generated by node_repositories.bzl
# Immediately exit if any command fails.
set -e
{get_script_dir}
export PATH="$SCRIPT_DIR":$PATH
"$SCRIPT_DIR/{node}" "$SCRIPT_DIR/{script}" "$SCRIPT_DIR" "$@"
exec "$SCRIPT_DIR/{node}" $@"
""".format(
get_script_dir = GET_SCRIPT_DIR,
node = node_exec_relative,
script = "node.js",
))
else:
# Sets PATH for node, npm & yarn and run user script
Expand Down

0 comments on commit c124496

Please sign in to comment.