Skip to content

Commit

Permalink
refactor: add option for optimized runfiles collection
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Jun 8, 2024
1 parent bc87775 commit 174bbb9
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 37 deletions.
7 changes: 5 additions & 2 deletions docs/js_binary.md

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

4 changes: 3 additions & 1 deletion docs/js_library.md

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

53 changes: 38 additions & 15 deletions js/private/js_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_
load("@aspect_bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher_script")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":bash.bzl", "BASH_INITIALIZE_RUNFILES")
load(":js_helpers.bzl", "LOG_LEVELS", "envs_for_log_level", "gather_runfiles")
load(":js_helpers.bzl", "LOG_LEVELS", "envs_for_log_level", "gather_runfiles", "gather_runfiles_optimized")

_DOC = """Execute a program in the Node.js runtime.
Expand Down Expand Up @@ -259,6 +259,16 @@ _ATTRS = {
""",
default = True,
),
"optimized_runfiles_collection": attr.bool(
doc = """Enable optimized runfiles collection where only default_runfiles
of `deps` & `data` targets are gathered.
Under the hood this disables the defense-in-depth against `data` & `deps` targets not supplying all required runfiles,
where runfiles collection also gathers the transitive sources & transitive npm sources from the `JsInfo` providers of
`data` & `deps` targets.
""",
default = True,
),
"include_npm": attr.bool(
doc = """When True, npm is included in the runfiles of the target.
Expand Down Expand Up @@ -523,20 +533,33 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [],
fail("include_npm requires a minimum @rules_nodejs version of 5.7.0")
launcher_files.extend(nodeinfo.npm_files)

runfiles = gather_runfiles(
ctx = ctx,
sources = [],
data = ctx.attr.data,
data_files = [entry_point] + ctx.files.data,
deps = [],
copy_data_files_to_bin = ctx.attr.copy_data_to_bin,
no_copy_to_bin = ctx.files.no_copy_to_bin,
include_sources = ctx.attr.include_sources,
include_types = ctx.attr.include_types,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_transitive_types = ctx.attr.include_transitive_types,
include_npm_sources = ctx.attr.include_npm_sources,
).merge(ctx.runfiles(
if ctx.attr.optimized_runfiles_collection:
runfiles = gather_runfiles_optimized(
ctx = ctx,
sources = depset(),
data = ctx.attr.data,
data_files = [entry_point] + ctx.files.data,
deps = [],
copy_data_files_to_bin = ctx.attr.copy_data_to_bin,
no_copy_to_bin = ctx.files.no_copy_to_bin,
)
else:
runfiles = gather_runfiles(
ctx = ctx,
sources = [],
data = ctx.attr.data,
data_files = [entry_point] + ctx.files.data,
deps = [],
copy_data_files_to_bin = ctx.attr.copy_data_to_bin,
no_copy_to_bin = ctx.files.no_copy_to_bin,
include_sources = ctx.attr.include_sources,
include_types = ctx.attr.include_types,
include_transitive_sources = ctx.attr.include_transitive_sources,
include_transitive_types = ctx.attr.include_transitive_types,
include_npm_sources = ctx.attr.include_npm_sources,
)

runfiles = runfiles.merge(ctx.runfiles(
files = launcher_files,
transitive_files = transitive_launcher_files,
))
Expand Down
Loading

0 comments on commit 174bbb9

Please sign in to comment.