Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add option for optimized runfiles collection #1781

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "include legacy runfiles" and False by default? Something to make it sound old/bad/deprecated...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps... tho "legacy" doesn't quite describe it in my mind. It is an overbroad, defence-in-depth runfiles collection mode that some users may need to use if they are using deps from other rule sets or custom rules that don't define their runfiles correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I mean to to highlight the "don't define their runfiles correctly" part of it...

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
Loading