Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ js/private/worker/src/package.json=1787033725
npm/private/lifecycle/package.json=804720835
npm/private/test/npm_package/package.json=-1991705133
npm/private/test/npm_package_publish/package.json=1501316979
npm/private/test/package.json=550137760
npm/private/test/package.json=-469322465
npm/private/test/subs/package.json=-897547334
npm/private/test/vendored/is-odd/package.json=1041695223
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
npm/private/test/vendored/semver-max/package.json=578664053
package.json=1856600154
pnpm-lock.yaml=-923227665
pnpm-lock.yaml=998574926
pnpm-workspace.yaml=1041196754
21 changes: 20 additions & 1 deletion .aspect/workflows/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ workspaces:
without: true
- bazel-9:
without: true
- bazel-9-no-execroot-entry-point:
without: true
e2e/pnpm_lockfiles:
icon: pnpm
tasks: *e2e_tasks
Expand All @@ -117,7 +119,17 @@ workspaces:
tasks: *e2e_tasks
e2e/repo_mapping:
icon: js
tasks: *e2e_tasks
tasks:
- test:
queue: aspect-medium
- format:
without: true
- buildifier:
without: true
# aspect_rules_js is given a different name in this repo, so we
# cannot use a flag beginning with --@aspect_rules_js//.
- bazel-9-no-execroot-entry-point:
without: true
e2e/output_paths:
icon: js
tasks: *e2e_tasks
Expand Down Expand Up @@ -167,6 +179,13 @@ tasks:
# TODO: change this back to 9.x once this bug is fixed:
# https://github.com/bazelbuild/bazel/issues/29393
USE_BAZEL_VERSION: '9.0.2'
- test:
name: Bazel 9 (no execroot entry point)
id: bazel-9-no-execroot-entry-point
bazel:
flags: ['--test_tag_filters=-skip-on-bazel9', '--@aspect_rules_js//js:use_execroot_entry_point=False']
env:
USE_BAZEL_VERSION: '9.0.2'
- format:
queue: aspect-medium
- buildifier:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest]
os: [windows-latest]
env:
ASPECT_RULES_JS_FROZEN_PNPM_LOCK: 1
steps:
Expand All @@ -147,7 +147,7 @@ jobs:
- name: bazel test //...
shell: bash
run: |
bazel test \
bazel test -k --verbose_failures \
--test_tag_filters=-skip-on-bazel7 \
--build_tag_filters=-skip-on-bazel7 \
//...
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bazel_dep(name = "rules_nodejs", version = "6.7.3")

# Changes ensured by rules_js:
# 3.2.2: https://github.com/bazel-contrib/bazel-lib/commit/cac2d7855949d1b222fa26888892fbbe1d31015d
bazel_dep(name = "bazel_lib", version = "3.2.2")
bazel_dep(name = "bazel_lib", version = "3.3.1")

# NB: LOWER BOUND on earliest BCR release of protobuf module, to avoid upgrading the root module by accident
bazel_dep(name = "protobuf", version = "3.19.6")
Expand Down
2 changes: 2 additions & 0 deletions contrib/nextjs/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def nextjs_build(name, config, srcs, next_js_binary, data = [], **kwargs):
chdir = native.package_name(),
mnemonic = "NextJs",
progress_message = "Compile Next.js app %{label}",
use_execroot_entry_point = True,
**kwargs
)

Expand Down Expand Up @@ -327,6 +328,7 @@ def nextjs_standalone_build(name, config, srcs, next_js_binary, data = [], **kwa
chdir = native.package_name(),
mnemonic = "NextJs",
progress_message = "Compile Next.js standalone app %{label}",
use_execroot_entry_point = True,
**kwargs
)

Expand Down
14 changes: 14 additions & 0 deletions js/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
"Public API"

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

# This flag controls the default behavior of the use_execroot_entry_point flag
# on js_run_binary.
bool_flag(
name = "use_execroot_entry_point",
build_setting_default = False,
visibility = ["//visibility:public"],
)

config_setting(
name = "_use_execroot_entry_point_true",
flag_values = {"use_execroot_entry_point": "True"},
)

bzl_library(
name = "defs",
Expand Down
10 changes: 8 additions & 2 deletions js/private/js_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load("@bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS")
load("@bazel_lib//lib:directory_path.bzl", "DirectoryPathInfo")
load("@bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
load("@bazel_lib//lib:paths.bzl", "to_rlocation_path")
load("@bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher_script")
load(":bash.bzl", "BASH_INITIALIZE_RUNFILES")
load(":js_helpers.bzl", "LOG_LEVELS", "envs_for_log_level", "gather_files_from_js_infos", "gather_runfiles")
Expand Down Expand Up @@ -136,7 +137,7 @@ _ATTRS = {

Log levels: {}""".format(", ".join(LOG_LEVELS.keys())),
values = LOG_LEVELS.keys(),
default = "error",
default = "debug",
),
"patch_node_fs": attr.bool(
doc = """Patch the to Node.js `fs` API (https://nodejs.org/api/fs.html) for this node program
Expand Down Expand Up @@ -269,6 +270,9 @@ _ATTRS = {
allow_single_file = True,
default = Label("@aspect_rules_js//js/private/node-patches:register.cjs"),
),
"_bash_runfiles_lib": attr.label(
default = Label("@bazel_tools//tools/bash/runfiles"),
),
}

_ENV_SET = """export {var}={quoted_value}"""
Expand Down Expand Up @@ -320,6 +324,7 @@ def _bash_launcher(ctx, nodeinfo, entry_point_path, log_prefix_rule_set, log_pre
ctx.label.name,
),
"JS_BINARY__WORKSPACE": ctx.workspace_name,
"RUNFILES_LIB_DEBUG": "1",
}
if is_windows and not ctx.attr.enable_runfiles:
builtins["JS_BINARY__NO_RUNFILES"] = "1"
Expand Down Expand Up @@ -469,6 +474,7 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [],
entry_point = ctx.files.entry_point[0]
entry_point_path = entry_point.short_path

entry_point_path = entry_point_path.replace("/./", "/")
bash_launcher, toolchain_files = _bash_launcher(ctx, nodeinfo, entry_point_path, log_prefix_rule_set, log_prefix_rule, fixed_args, fixed_env, is_windows)
launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher

Expand Down Expand Up @@ -500,7 +506,7 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [],
include_transitive_types = ctx.attr.include_transitive_types,
include_npm_sources = ctx.attr.include_npm_sources,
),
))
)).merge(ctx.attr._bash_runfiles_lib[DefaultInfo].default_runfiles)

return struct(
executable = launcher,
Expand Down
33 changes: 29 additions & 4 deletions js/private/js_binary.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

set -o pipefail -o errexit -o nounset

# Re-exec with an absolute $0 so that rlocation works correctly after directory
# changes. runfiles_current_repository (called by rlocation) uses $BASH_SOURCE
# to locate the calling script and makes its path absolute via cd $(dirname ...),
# which fails when $0 is relative and the working directory has since changed.
# if [[ "$0" != /* ]]; then
# exec "$PWD/$0" "$@"
# fi

{{envs}}

# ==============================================================================
Expand Down Expand Up @@ -150,6 +158,18 @@ trap _exit EXIT
JS_BINARY__RUNFILES="$RUNFILES"
export JS_BINARY__RUNFILES

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

# ==============================================================================
# Prepare to run main program
# ==============================================================================
Expand Down Expand Up @@ -236,7 +256,9 @@ To disable this validation you can set allow_execroot_entry_point_with_no_copy_d
fi
fi

if [ "${JS_BINARY__USE_EXECROOT_ENTRY_POINT:-}" ] || [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
if [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
entry_point="$(rlocation {{workspace_name}}/{{entry_point_path}})"
elif [ "${JS_BINARY__USE_EXECROOT_ENTRY_POINT:-}" ] || [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
entry_point=$(resolve_execroot_bin_path "{{entry_point_path}}")
else
entry_point="$JS_BINARY__RUNFILES/{{workspace_name}}/{{entry_point_path}}"
Expand All @@ -247,6 +269,9 @@ if [ ! -f "$entry_point" ]; then
fi

node="$(_normalize_path "{{node}}")"
if [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
node="$(rlocation ${node:3})"
fi
if [ "${node:0:1}" = "/" ]; then
# A user may specify an absolute path to node using target_tool_path in node_toolchain
export JS_BINARY__NODE_BINARY="$node"
Expand Down Expand Up @@ -284,7 +309,7 @@ if [ "$npm" ]; then
else
if [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
export JS_BINARY__NPM_BINARY
JS_BINARY__NPM_BINARY=$(resolve_execroot_src_path "{{npm}}")
JS_BINARY__NPM_BINARY=$(rlocation "{{workspace_name}}/{{npm}}")
else
export JS_BINARY__NPM_BINARY="$JS_BINARY__RUNFILES/{{workspace_name}}/{{npm}}"
fi
Expand All @@ -301,7 +326,7 @@ fi

if [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
export JS_BINARY__NODE_WRAPPER
JS_BINARY__NODE_WRAPPER=$(resolve_execroot_bin_path "{{node_wrapper}}")
JS_BINARY__NODE_WRAPPER=$(rlocation "{{workspace_name}}/{{node_wrapper}}")
else
export JS_BINARY__NODE_WRAPPER="$JS_BINARY__RUNFILES/{{workspace_name}}/{{node_wrapper}}"
fi
Expand All @@ -316,7 +341,7 @@ fi

if [ "${JS_BINARY__NO_RUNFILES:-}" ]; then
export JS_BINARY__NODE_PATCHES
JS_BINARY__NODE_PATCHES=$(resolve_execroot_src_path "{{node_patches}}")
JS_BINARY__NODE_PATCHES=$(rlocation "{{workspace_name}}/{{node_patches}}")
else
export JS_BINARY__NODE_PATCHES="$JS_BINARY__RUNFILES/{{workspace_name}}/{{node_patches}}"
fi
Expand Down
36 changes: 28 additions & 8 deletions js/private/js_run_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def js_run_binary(
stderr = None,
exit_code_out = None,
silent_on_success = True,
use_execroot_entry_point = True,
use_execroot_entry_point = None,
copy_srcs_to_bin = True,
include_sources = True,
include_types = False,
Expand Down Expand Up @@ -149,6 +149,9 @@ def js_run_binary(
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.

If `None` (the default), the behavior is controlled by the `//js:use_execroot_entry_point` build flag,
which defaults to `True`.

copy_srcs_to_bin: When True, all srcs files are copied to the output tree that are not already there.

include_sources: see `js_info_files` documentation
Expand Down Expand Up @@ -356,10 +359,10 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi
name = bazel_lib_utils.to_label(name),
))

# Configure run from execroot
if use_execroot_entry_point:
fixed_env["JS_BINARY__USE_EXECROOT_ENTRY_POINT"] = "1"

# Configure run from execroot.
# When use_execroot_entry_point is None, behavior is controlled by the //js:use_execroot_entry_point flag.
execroot_extra_srcs = []
if use_execroot_entry_point != False:
# hoist all runfiles to srcs when running from execroot
js_runfiles_lib_name = "{}_runfiles_lib".format(name)
_js_library(
Expand All @@ -380,16 +383,33 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi
# Always propagate the testonly attribute
testonly = kwargs.get("testonly", False),
)
extra_srcs.append(":{}".format(js_runfiles_name))

if use_execroot_entry_point == True:
fixed_env["JS_BINARY__USE_EXECROOT_ENTRY_POINT"] = "1"
execroot_extra_srcs = [":{}".format(js_runfiles_name)]
else:
# None: use the flag to decide at analysis time
execroot_extra_srcs = select({
Label("//js:_use_execroot_entry_point_true"): [":{}".format(js_runfiles_name)],
"//conditions:default": [],
})

if allow_execroot_entry_point_with_no_copy_data_to_bin:
fixed_env["JS_BINARY__ALLOW_EXECROOT_ENTRY_POINT_WITH_NO_COPY_DATA_TO_BIN"] = "1"

# When use_execroot_entry_point is None, the env var is resolved at analysis time via the flag.
# We use a select() only for the conditional portion and merge with | outside of it, so that
# a user-supplied env that is itself a select() remains valid on the right-hand side.
execroot_env = select({
Label("//js:_use_execroot_entry_point_true"): {"JS_BINARY__USE_EXECROOT_ENTRY_POINT": "1"},
"//conditions:default": {},
}) if use_execroot_entry_point == None else {}

_run_binary(
name = name,
tool = tool,
env = fixed_env | env,
srcs = srcs + extra_srcs,
env = fixed_env | execroot_env | env,
srcs = srcs + extra_srcs + execroot_extra_srcs,
outs = outs + extra_outs,
out_dirs = out_dirs,
args = args,
Expand Down
11 changes: 10 additions & 1 deletion js/private/js_run_devserver.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def js_run_devserver(
tool = None,
command = None,
grant_sandbox_write_permissions = False,
use_execroot_entry_point = True,
use_execroot_entry_point = None,
allow_execroot_entry_point_with_no_copy_data_to_bin = False,
**kwargs):
"""Runs a devserver via binary target or command.
Expand Down Expand Up @@ -253,6 +253,9 @@ def js_run_devserver(
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.

If `None` (the default), the behavior is controlled by the `//js:use_execroot_entry_point` build flag,
which defaults to `True`.

allow_execroot_entry_point_with_no_copy_data_to_bin: 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.

Expand All @@ -267,6 +270,12 @@ def js_run_devserver(
if kwargs.get("entry_point", None):
fail("`entry_point` is set implicitly by `js_run_devserver` and cannot be overridden.")

if use_execroot_entry_point == None:
use_execroot_entry_point = select({
Label("//js:_use_execroot_entry_point_true"): True,
"//conditions:default": False,
})

# Allow the js_run_devserver rule to execute to be overridden for tests
rule_to_execute = kwargs.pop("rule_to_execute", _js_run_devserver)

Expand Down
5 changes: 5 additions & 0 deletions js/private/test/data/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ js_test(
# TEST: js_test(data = js_run_binary(srcs)) ---------------
js_run_binary(
name = "run-write-srcs",
srcs = ["data.json"],
outs = ["rb0.json"],
args = [
"rb0.json",
Expand All @@ -178,6 +179,7 @@ js_test(
# TEST: js_test(data = js_run_binary(srcs = js_library(srcs))) ----------------
js_run_binary(
name = "run-write-js_library-srcs",
srcs = ["data.json"],
outs = ["rb1.json"],
args = [
"rb1.json",
Expand Down Expand Up @@ -210,6 +212,7 @@ js_binary(

js_run_binary(
name = "run-write-js_library-data",
srcs = ["data.json"],
outs = ["rb2.json"],
args = [
"rb2.json",
Expand All @@ -235,6 +238,7 @@ js_test(
# TEST: js_test(data = js_run_binary(srcs = genrule())) -----------------------
js_run_binary(
name = "run-write-generated",
srcs = ["data-generated.json"],
outs = ["rb3.json"],
args = [
"rb3.json",
Expand All @@ -260,6 +264,7 @@ js_test(
# TEST: js_test(data = js_run_binary(srcs = genrule(srcs = filegroup))) -------
js_run_binary(
name = "run-write-generated-copied",
srcs = ["data-copied.json"],
outs = ["rb4.json"],
args = [
"rb4.json",
Expand Down
Loading
Loading