Skip to content

Commit

Permalink
feat: add BAZEL env variable to js_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Nov 10, 2023
1 parent 300f99e commit 575aa48
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/js_binary.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions js/private/js_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Bazel option to see more detail about the selection.
All [common binary attributes](https://bazel.build/reference/be/common-definitions#common-attributes-binaries) are supported
including `args` as the list of arguments passed Node.js.
`js_binary` always sets the `BAZEL` environment variable to `1`, if it is not already set, which can be used by Node.js
programs to detect if they are running under Bazel. This can be useful for cases where a Node.js program is run both
under Bazel and non-Bazel use cases and different code paths are required between the two.
The following environment variables are made available to the Node.js runtime based on available Bazel [Make variables](https://bazel.build/reference/be/make-variables#predefined_variables):
* JS_BINARY__BINDIR: the WORKSPACE-relative Bazel bin directory; equivalent to the `$(BINDIR)` Make variable of the `js_binary` target
Expand Down
10 changes: 8 additions & 2 deletions js/private/js_binary.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ set -o pipefail -o errexit -o nounset

{{envs}}

# Set BAZEL as a comment env var for all rules_js programs to be able to detect
# they are being run under Bazel
if [ -z "${BAZEL:-}" ]; then
export BAZEL=1
fi

# ==============================================================================
# Prepare stdout capture, stderr capture && logging
# ==============================================================================
Expand All @@ -32,10 +38,10 @@ function logf_stderr {
local format_string="$1\n"
shift
if [ "${STDERR_CAPTURE:-}" ]; then
# shellcheck disable=SC2059
# shellcheck disable=SC2059,SC2046
echo -e $(printf "$format_string" "$@") >>"$STDERR_CAPTURE"
else
# shellcheck disable=SC2059
# shellcheck disable=SC2059,SC2046
echo -e $(printf "$format_string" "$@") >&2
fi
}
Expand Down
6 changes: 6 additions & 0 deletions js/private/test/env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//js:defs.bzl", "js_test")

js_test(
name = "env_test",
entry_point = "env_test.mjs",
)
79 changes: 79 additions & 0 deletions js/private/test/env/env_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Many of these env variables are set by Bazel. See https://bazel.build/reference/test-encyclopedia#initial-conditions.
const expected = {
BAZEL_TEST: '1',
BAZEL: '1',
JS_BINARY__BINDIR: /^bazel-out\/[a-z0-9\-]+\/bin$/,
JS_BINARY__BUILD_FILE_PATH: 'js/private/test/env/BUILD.bazel',
JS_BINARY__COMPILATION_MODE: /.+/,
JS_BINARY__COPY_DATA_TO_BIN: '1',
JS_BINARY__EXECROOT: /.+/,
JS_BINARY__FS_PATCH_ROOTS: /.+/,
JS_BINARY__LOG_ERROR: '1',
JS_BINARY__LOG_FATAL: '1',
JS_BINARY__LOG_PREFIX: 'aspect_rules_js[js_test]',
JS_BINARY__NODE_BINARY: /bin\/nodejs\/bin\/node$/,
JS_BINARY__NODE_PATCHES_DEPTH: '.',
JS_BINARY__NODE_PATCHES:
/bazel-out\/[a-z0-9\-]+\/bin\/js\/private\/test\/env\/env_test.sh.runfiles\/[a-z_]+\/js\/private\/node-patches\/register\.js$/,
JS_BINARY__NODE_WRAPPER:
/bazel-out\/[a-z0-9\-]+\/bin\/js\/private\/test\/env\/env_test.sh.runfiles\/[a-z_]+\/js\/private\/test\/env\/env_test_node_bin\/node$/,
JS_BINARY__PACKAGE: 'js/private/test/env',
JS_BINARY__PATCH_NODE_FS: '1',
JS_BINARY__RUNFILES:
/bazel-out\/[a-z0-9\-]+\/bin\/js\/private\/test\/env\/env_test\.sh\.runfiles$/,
JS_BINARY__TARGET_CPU: /.+/,
JS_BINARY__TARGET_NAME: 'env_test',
JS_BINARY__TARGET: '//js/private/test/env:env_test',
JS_BINARY__WORKSPACE: /[a-z_]+/,
RUNFILES_DIR:
/bazel-out\/[a-z0-9\-]+\/bin\/js\/private\/test\/env\/env_test\.sh\.runfiles$/,
RUNFILES:
/bazel-out\/[a-z0-9\-]+\/bin\/js\/private\/test\/env\/env_test\.sh\.runfiles$/,
TEST_BINARY: 'js/private/test/env/env_test.sh',
TEST_INFRASTRUCTURE_FAILURE_FILE:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.infrastructure_failure$/,
TEST_LOGSPLITTER_OUTPUT_FILE:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.raw_splitlogs\/test\.splitlogs$/,
TEST_PREMATURE_EXIT_FILE:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.exited_prematurely$/,
TEST_SIZE: 'medium',
TEST_SRCDIR:
/bazel-out\/[a-z0-9\-]+\/bin\/js\/private\/test\/env\/env_test\.sh\.runfiles$/,
TEST_TARGET: '//js/private/test/env:env_test',
TEST_TIMEOUT: /[0-9]+/,
TEST_TMPDIR: /.+/,
TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.outputs_manifest$/,
TEST_UNDECLARED_OUTPUTS_DIR:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.outputs$/,
TEST_UNUSED_RUNFILES_LOG_FILE:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.unused_runfiles_log$/,
TEST_WARNINGS_OUTPUT_FILE:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.warnings$/,
TEST_WORKSPACE: /[a-z_]+/,
XML_OUTPUT_FILE:
/bazel-out\/[a-z0-9\-]+\/testlogs\/js\/private\/test\/env\/env_test\/test\.xml$/,
}

let failed = false
for (const k of Object.keys(expected)) {
const v = expected[k]
if (typeof v === 'string') {
if (process.env[k] !== v) {
console.error(
`Expected environment variable ${k} to equal '${v}' but got '${process.env[k]}' instead`
)
failed = true
}
} else {
if (!process.env[k].match(v)) {
console.error(
`Expected environment variable ${k} to match regex ${v} but value '${process.env[k]}' does not match`
)
failed = true
}
}
}
if (failed) {
process.exit(1)
}
2 changes: 1 addition & 1 deletion js/private/test/image/structure/digests.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
b44c02668701d4db8430edae7d6f6ecede824e704d5c46226e2b32cc20633407
07c98625ac9e18c33bcf6d5562ab092abe470847437571e2a0b02af39932fc60
f68066745a57eff40fc49b4600cdde860dfa86ab231ebe4ed65097c98cc2df4f
10 changes: 8 additions & 2 deletions js/private/test/shellcheck_launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export JS_BINARY__COPY_DATA_TO_BIN="1"
if [[ -z "${JS_BINARY__LOG_FATAL:-}" ]]; then export JS_BINARY__LOG_FATAL="1"; fi
if [[ -z "${JS_BINARY__LOG_ERROR:-}" ]]; then export JS_BINARY__LOG_ERROR="1"; fi

# Set BAZEL as a comment env var for all rules_js programs to be able to detect
# they are being run under Bazel
if [ -z "${BAZEL:-}" ]; then
export BAZEL=1
fi

# ==============================================================================
# Prepare stdout capture, stderr capture && logging
# ==============================================================================
Expand All @@ -43,10 +49,10 @@ function logf_stderr {
local format_string="$1\n"
shift
if [ "${STDERR_CAPTURE:-}" ]; then
# shellcheck disable=SC2059
# shellcheck disable=SC2059,SC2046
echo -e $(printf "$format_string" "$@") >>"$STDERR_CAPTURE"
else
# shellcheck disable=SC2059
# shellcheck disable=SC2059,SC2046
echo -e $(printf "$format_string" "$@") >&2
fi
}
Expand Down

0 comments on commit 575aa48

Please sign in to comment.