js_run_binary
Runs a js_binary as a build action.
This macro wraps Aspect bazel-lib's run_binary (https://github.com/aspect-build/bazel-lib/blob/main/lib/run_binary.bzl) and adds attributes and features specific to rules_js's js_binary.
Load this with,
load("@aspect_rules_js//js:defs.bzl", "js_run_binary")
Macros and Functions
js_run_binary
Wrapper around @aspect_bazel_lib run_binary
that adds convenience attributes for using a js_binary
tool.
This rule does not require Bash native.genrule
.
The following environment variables are made available to the Node.js runtime based on available Bazel Make variables:
- BAZEL_BINDIR: the WORKSPACE-relative bazel bin directory; equivalent to the
$(BINDIR)
Make variable of thejs_run_binary
target - BAZEL_COMPILATION_MODE: One of
fastbuild
,dbg
, oropt
as set by--compilation_mode
; equivalent to$(COMPILATION_MODE)
Make variable of thejs_run_binary
target - BAZEL_TARGET_CPU: the target cpu architecture; equivalent to
$(TARGET_CPU)
Make variable of thejs_run_binary
target
The following environment variables are made available to the Node.js runtime based on the rule context:
- BAZEL_BUILD_FILE_PATH: the WORKSPACE-relative path to the BUILD file of the bazel target being run; equivalent to
ctx.build_file_path
of thejs_run_binary
target's rule context - BAZEL_PACKAGE: the package of the bazel target being run; equivalent to
ctx.label.package
of thejs_run_binary
target's rule context - BAZEL_TARGET_NAME: the full label of the bazel target being run; a stringified version of
ctx.label
of thejs_run_binary
target's rule context - BAZEL_TARGET: the name of the bazel target being run; equivalent to
ctx.label.name
of thejs_run_binary
target's rule context - BAZEL_WORKSPACE: the bazel workspace name; equivalent to
ctx.workspace_name
of thejs_run_binary
target's rule context
Example usage (generated):
load("@aspect_rules_js//js:defs.bzl", "js_run_binary")
js_run_binary(
# Target name
name = "",
# The tool to run in the action
tool = None,
)
name
Required.
Target name
tool
Required.
The tool to run in the action.
Should be a js_binary
rule. Use Aspect bazel-lib's run_binary
(https://github.com/aspect-build/bazel-lib/blob/main/lib/run_binary.bzl)
for other *_binary rule types.
env
Optional. Default: {}
Environment variables of the action.
Subject to $(location)
and make variable expansion.
srcs
Optional. Default: []
Additional inputs of the action.
These labels are available for $(location)
expansion in args
and env
.
outs
Optional. Default: []
Output files generated by the action.
These labels are available for $(location)
expansion in args
and env
.
out_dirs
Optional. Default: []
Output directories generated by the action.
These labels are not available for $(location)
expansion in args
and env
since
they are not pre-declared labels created via attr.output_list(). Output directories are
declared instead by ctx.actions.declare_directory
.
args
Optional. Default: []
Command line arguments of the binary.
Subject to $(location)
and make variable expansion.
chdir
Optional. Default: None
Working directory to run the build action in.
This overrides the chdir value if set on the js_binary
tool target.
By default, js_binary
tools run in the root of the output tree. For more context on why, please read the
aspect_rules_js README
https://github.com/aspect-build/rules_js/tree/dbb5af0d2a9a2bb50e4cf4a96dbc582b27567155#running-nodejs-programs.
To run in the directory containing the js_run_binary in the output tree, use
chdir = package_name()
(or if you're in a macro, use native.package_name()
).
WARNING: this will affect other paths passed to the program, either as arguments or in configuration files, which are workspace-relative.
You may need ../../
segments to re-relativize such paths to the new working directory.
stdout
Optional. Default: None
Output file to capture the stdout of the binary.
This can later be used as an input to another target subject to the same semantics as outs
.
If the binary creates outputs and these are declared, they must still be created.
stderr
Optional. Default: None
Output file to capture the stderr of the binary to.
This can later be used as an input to another target subject to the same semantics as outs
.
If the binary creates outputs and these are declared, they must still be created.
exit_code_out
Optional. Default: None
Output file to capture the exit code of the binary to.
This can later be used as an input to another target subject to the same semantics as outs
. Note that
setting this will force the binary to exit 0.
If the binary creates outputs and these are declared, they must still be created.
silent_on_success
Optional. Default: True
produce no output on stdout nor stderr when program exits with status code 0.
This makes node binaries match the expected bazel paradigm.
use_execroot_entry_point
Optional. Default: True
Use the entry_point
script of the js_binary
tool
that is in the execroot output tree
instead of the copy that is in runfiles.
Runfiles of tool
are all hoisted to srcs
of the underlying run_binary
so they are included as execroot
inputs to the action.
Using the entry point script that is in the execroot output tree means that there will be no conflicting
runfiles node_modules
in the node_modules resolution path which can confuse npm packages such as next and
react that don't like being resolved in multiple node_modules trees. This more closely emulates the
environment that tools such as Next.js see when they are run outside of Bazel.
When True, the js_binary
tool must have copy_data_to_bin
set to True (the default) so that all data files
needed by the binary are available in the execroot output tree. This requirement can be turned off with by
setting allow_execroot_entry_point_with_no_copy_data_to_bin
to True.
copy_srcs_to_bin
Optional. Default: True
When True, all srcs files are copied to the output tree that are not already there.
include_sources
Optional. Default: True
see js_info_files
documentation
include_types
Optional. Default: False
see js_info_files
documentation
include_transitive_sources
Optional. Default: True
see js_info_files
documentation
include_transitive_types
Optional. Default: False
see js_info_files
documentation
include_npm_sources
Optional. Default: True
see js_info_files
documentation
log_level
Optional. Default: None
Set the logging level of the js_binary
tool.
This overrides the log level set on the js_binary
tool target.
mnemonic
Optional. Default: "JsRunBinary"
A one-word description of the action, for example, CppCompile or GoLink.
progress_message
Optional. Default: None
Progress message to show to the user during the build, for example, "Compiling foo.cc to create foo.o". The message may contain %{label}, %{input}, or %{output} patterns, which are substituted with label string, first input, or output's path, respectively. Prefer to use patterns instead of static strings, because the former are more efficient.
execution_requirements
Optional. Default: None
Information for scheduling the action.
For example,
execution_requirements = {
"no-cache": "1",
},
See https://docs.bazel.build/versions/main/be/common-definitions.html#common.tags for useful keys.
stamp
Optional. Default: 0
Whether to include build status files as inputs to the tool. Possible values:
stamp = 0 (default)
: Never include build status files as inputs to the tool. This gives good build result caching. Most tools don't use the status files, so including them in--stamp
builds makes those builds have many needless cache misses. (Note: this default is different from most rules with an integer-typedstamp
attribute.)stamp = 1
: Always include build status files as inputs to the tool, even in --nostamp builds. This setting should be avoided, since it is non-deterministic. It potentially causes remote cache misses for the target and any downstream actions that depend on the result.stamp = -1
: Inclusion of build status files as inputs is controlled by the --[no]stamp flag. Stamped targets are not rebuilt unless their dependencies change.
Default value is 0
since the majority of js_run_binary targets in a build graph typically do not use build
status files and including them for all js_run_binary actions whenever --stamp
is set would result in
invalidating the entire graph and would prevent cache hits. Stamping is typically done in terminal targets
when building release artifacts and stamp should typically be set explicitly in these targets to -1
so it
is enabled when the --stamp
flag is set.
When stamping is enabled, an additional two environment variables will be set for the action:
- `BAZEL_STABLE_STATUS_FILE`
- `BAZEL_VOLATILE_STATUS_FILE`
These files can be read and parsed by the action, for example to pass some values to a bundler.
patch_node_fs
Optional. Default: True
Patch the to Node.js fs
API (https://nodejs.org/api/fs.html) for this node program
to prevent the program from following symlinks out of the execroot, runfiles and the sandbox.
When enabled, js_binary
patches the Node.js sync and async fs
API functions lstat
,
readlink
, realpath
, readdir
and opendir
so that the node program being
run cannot resolve symlinks out of the execroot and the runfiles tree. When in the sandbox,
these patches prevent the program being run from resolving symlinks out of the sandbox.
When disabled, node programs can leave the execroot, runfiles and sandbox by following symlinks which can lead to non-hermetic behavior.
allow_execroot_entry_point_with_no_copy_data_to_bin
Optional. Default: False
Turn off validation that the js_binary
tool
has copy_data_to_bin
set to True when use_execroot_entry_point
is set to True.
See use_execroot_entry_point
doc for more info.
use_default_shell_env
Optional. Default: None
If set, passed to the underlying run_binary.
May introduce non-determinism when True; use with care! See e.g. https://github.com/bazelbuild/bazel/issues/4912
Requires a minimum of aspect_bazel_lib v1.40.3 or v2.4.2.
Refer to https://bazel.build/rules/lib/builtins/actions#run for more details.
kwargs
Optional.
Additional arguments