Skip to content

Commit

Permalink
feat(builtin): add silent_on_success option to npm_package_bin (#3336)
Browse files Browse the repository at this point in the history
* feat(builtin): add silent_on_success option to npm_package_bin

* feat(builtin): add silent_on_success option to rollup_bundle
  • Loading branch information
ptarjan authored Mar 8, 2022
1 parent f3ccede commit 78aefa3
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 4 deletions.
9 changes: 8 additions & 1 deletion docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ Other attributes

<pre>
npm_package_bin(<a href="#npm_package_bin-tool">tool</a>, <a href="#npm_package_bin-package">package</a>, <a href="#npm_package_bin-package_bin">package_bin</a>, <a href="#npm_package_bin-data">data</a>, <a href="#npm_package_bin-env">env</a>, <a href="#npm_package_bin-outs">outs</a>, <a href="#npm_package_bin-args">args</a>, <a href="#npm_package_bin-stderr">stderr</a>, <a href="#npm_package_bin-stdout">stdout</a>, <a href="#npm_package_bin-exit_code_out">exit_code_out</a>,
<a href="#npm_package_bin-output_dir">output_dir</a>, <a href="#npm_package_bin-link_workspace_root">link_workspace_root</a>, <a href="#npm_package_bin-chdir">chdir</a>, <a href="#npm_package_bin-kwargs">kwargs</a>)
<a href="#npm_package_bin-output_dir">output_dir</a>, <a href="#npm_package_bin-link_workspace_root">link_workspace_root</a>, <a href="#npm_package_bin-chdir">chdir</a>, <a href="#npm_package_bin-silent_on_success">silent_on_success</a>, <a href="#npm_package_bin-kwargs">kwargs</a>)
</pre>

Run an arbitrary npm package binary (e.g. a program under node_modules/.bin/*) under Bazel.
Expand Down Expand Up @@ -2028,6 +2028,13 @@ npm_package_bin(

Defaults to `None`

<h4 id="npm_package_bin-silent_on_success">silent_on_success</h4>

produce no output on stdout nor stderr when program exits with status code 0.
This makes node binaries match the expected bazel paradigm.

Defaults to `False`

<h4 id="npm_package_bin-kwargs">kwargs</h4>

additional undocumented keyword args
Expand Down
13 changes: 11 additions & 2 deletions docs/Rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ rollup_bundle(

<pre>
rollup_bundle(<a href="#rollup_bundle-name">name</a>, <a href="#rollup_bundle-args">args</a>, <a href="#rollup_bundle-config_file">config_file</a>, <a href="#rollup_bundle-deps">deps</a>, <a href="#rollup_bundle-entry_point">entry_point</a>, <a href="#rollup_bundle-entry_points">entry_points</a>, <a href="#rollup_bundle-format">format</a>, <a href="#rollup_bundle-link_workspace_root">link_workspace_root</a>,
<a href="#rollup_bundle-output_dir">output_dir</a>, <a href="#rollup_bundle-rollup_bin">rollup_bin</a>, <a href="#rollup_bundle-rollup_worker_bin">rollup_worker_bin</a>, <a href="#rollup_bundle-silent">silent</a>, <a href="#rollup_bundle-sourcemap">sourcemap</a>, <a href="#rollup_bundle-srcs">srcs</a>, <a href="#rollup_bundle-stamp">stamp</a>,
<a href="#rollup_bundle-supports_workers">supports_workers</a>)
<a href="#rollup_bundle-output_dir">output_dir</a>, <a href="#rollup_bundle-rollup_bin">rollup_bin</a>, <a href="#rollup_bundle-rollup_worker_bin">rollup_worker_bin</a>, <a href="#rollup_bundle-silent">silent</a>, <a href="#rollup_bundle-silent_on_success">silent_on_success</a>, <a href="#rollup_bundle-sourcemap">sourcemap</a>, <a href="#rollup_bundle-srcs">srcs</a>,
<a href="#rollup_bundle-stamp">stamp</a>, <a href="#rollup_bundle-supports_workers">supports_workers</a>)
</pre>

Runs the rollup.js CLI under Bazel.
Expand Down Expand Up @@ -353,6 +353,15 @@ is a more Bazel-idiomatic experience, however could cause rollup to drop importa

Defaults to `False`

<h4 id="rollup_bundle-silent_on_success">silent_on_success</h4>

(*Boolean*): Even stronger than --silent, defaults to False.

Since the build still emits some texted, even when passed --silent, this uses the same flag as npm_package_bin to
supress all output on sucess.

Defaults to `False`

<h4 id="rollup_bundle-sourcemap">sourcemap</h4>

(*String*): Whether to produce sourcemaps.
Expand Down
2 changes: 1 addition & 1 deletion docs/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ for more details about the trade-offs between the two rules.

Some TypeScript options affect which files are emitted, and Bazel wants to know these ahead-of-time.
So several options from the tsconfig file must be mirrored as attributes to ts_project.
See https://www.typescriptlang.org/tsconfig for a listing of the TypeScript options.
See https://www.typescriptlang.org/v2/en/tsconfig for a listing of the TypeScript options.

Any code that works with `tsc` should work with `ts_project` with a few caveats:

Expand Down
31 changes: 31 additions & 0 deletions internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ for ARG in ${ALL_ARGS[@]+"${ALL_ARGS[@]}"}; do
--bazel_capture_stderr=*) STDERR_CAPTURE="${PWD}/${ARG#--bazel_capture_stderr=}" ;;
# Captures the exit code of the node process to the file specified
--bazel_capture_exit_code=*) EXIT_CODE_CAPTURE="${PWD}/${ARG#--bazel_capture_exit_code=}" ;;
# Outputs nothing on success
--bazel_silent_on_success=*) SILENT_ON_SUCCESS=true ;;
# Disable the node_loader.js monkey patches for require()
# Note that this means you need an explicit runfiles helper library
# This flag is now a no-op since the default is also false
Expand Down Expand Up @@ -338,6 +340,17 @@ else
fi
fi

if [ "${SILENT_ON_SUCCESS:-}" = true ]; then
if [[ -z "${STDOUT_CAPTURE}" ]]; then
STDOUT_CAPTURE_IS_NOT_AN_OUTPUT=true
STDOUT_CAPTURE=$(mktemp)
fi
if [[ -z "${STDERR_CAPTURE}" ]]; then
STDERR_CAPTURE_IS_NOT_AN_OUTPUT=true
STDERR_CAPTURE=$(mktemp)
fi
fi

# 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.
Expand All @@ -362,6 +375,23 @@ _int() {
kill -INT "${child}" 2>/dev/null
}

_exit() {
EXIT_CODE=$?

if [[ "$EXIT_CODE" != 0 ]]; then
if [ ${STDOUT_CAPTURE_IS_NOT_AN_OUTPUT} = true ]; then
cat "$STDOUT_CAPTURE"
rm "$STDOUT_CAPTURE"
fi
if [ ${STDERR_CAPTURE_IS_NOT_AN_OUTPUT} = true ]; then
cat "$STDERR_CAPTURE"
rm "$STDERR_CAPTURE"
fi
fi

exit $EXIT_CODE
}

# Execute the main program
if [[ -n "$NODE_WORKING_DIR" ]]; then
cd "$NODE_WORKING_DIR"
Expand All @@ -380,6 +410,7 @@ fi
readonly child=$!
trap _term SIGTERM
trap _int SIGINT
trap _exit EXIT
wait "${child}"
# Remove trap after first signal has been receieved and wait for child to exit
# (first wait returns immediatel if SIGTERM is received while waiting). Second
Expand Down
6 changes: 6 additions & 0 deletions internal/node/npm_package_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _ATTRS = {
"link_workspace_root": attr.bool(),
"output_dir": attr.bool(),
"outs": attr.output_list(),
"silent_on_success": attr.bool(),
"stderr": attr.output(),
"stdout": attr.output(),
"tool": attr.label(
Expand Down Expand Up @@ -91,6 +92,7 @@ def _impl(ctx):
stdout = ctx.outputs.stdout,
stderr = ctx.outputs.stderr,
exit_code_out = ctx.outputs.exit_code_out,
silent_on_success = ctx.attr.silent_on_success,
link_workspace_root = ctx.attr.link_workspace_root,
)
files = outputs + tool_outputs
Expand Down Expand Up @@ -118,6 +120,7 @@ def npm_package_bin(
output_dir = False,
link_workspace_root = False,
chdir = None,
silent_on_success = False,
**kwargs):
"""Run an arbitrary npm package binary (e.g. a program under node_modules/.bin/*) under Bazel.
Expand Down Expand Up @@ -146,6 +149,8 @@ def npm_package_bin(
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`. 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: produce no output on stdout nor stderr when program exits with status code 0.
This makes node binaries match the expected bazel paradigm.
args: Command-line arguments to the tool.
Expand Down Expand Up @@ -239,5 +244,6 @@ def npm_package_bin(
output_dir = output_dir,
tool = tool,
link_workspace_root = link_workspace_root,
silent_on_success = silent_on_success,
**kwargs
)
15 changes: 15 additions & 0 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ generated_file_test(
generated = ":minified.stdout.js",
)

# target that shouldn't print anything on stdout nor stderr when built even through the binary does
npm_package_bin(
name = "loud_binary",
args = [
"$(execpath terser_input_with_diagnostics.js)",
"--compress",
"--verbose",
"--warn",
],
data = ["terser_input_with_diagnostics.js"],
output_dir = True,
package = "terser",
silent_on_success = True,
)

# 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
Expand Down
4 changes: 4 additions & 0 deletions internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def run_node(ctx, inputs, arguments, executable, chdir = None, **kwargs):
add_arg(arguments, "--bazel_capture_exit_code=%s" % exit_code_file.path)
outputs = outputs + [exit_code_file]

silent_on_success = kwargs.pop("silent_on_success", False)
if silent_on_success:
add_arg(arguments, "--bazel_silent_on_success=1")

if chdir:
add_arg(arguments, "--bazel_node_working_dir=" + chdir)

Expand Down
8 changes: 8 additions & 0 deletions packages/rollup/rollup_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ Otherwise, the outputs are assumed to be a single file.
Using --silent can cause rollup to [ignore errors/warnings](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#onwarn)
which are only surfaced via logging. Since bazel expects printing nothing on success, setting silent to True
is a more Bazel-idiomatic experience, however could cause rollup to drop important warnings.
""",
),
"silent_on_success": attr.bool(
doc = """Even stronger than --silent, defaults to False.
Since the build still emits some texted, even when passed --silent, this uses the same flag as npm_package_bin to
supress all output on sucess.
""",
),
"sourcemap": attr.string(
Expand Down Expand Up @@ -345,6 +352,7 @@ def _rollup_bundle(ctx):
execution_requirements = execution_requirements,
env = {"COMPILATION_MODE": ctx.var["COMPILATION_MODE"]},
link_workspace_root = ctx.attr.link_workspace_root,
silent_on_success = ctx.attr.silent_on_success,
)

outputs_depset = depset(outputs)
Expand Down
15 changes: 15 additions & 0 deletions packages/rollup/test/silent/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("//packages/rollup:index.bzl", "rollup_bundle")

rollup_bundle(
name = "silent",
srcs = ["input.js"],
entry_points = {"input.js": "silent"},
silent = True,
)

rollup_bundle(
name = "silent_on_success",
srcs = ["input.js"],
entry_points = {"input.js": "silent_on_success"},
silent_on_success = True,
)
1 change: 1 addition & 0 deletions packages/rollup/test/silent/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello");

0 comments on commit 78aefa3

Please sign in to comment.