diff --git a/docs/use_execroot_entry_point.md b/docs/use_execroot_entry_point.md index 84f8bf3d4..3794911c4 100644 --- a/docs/use_execroot_entry_point.md +++ b/docs/use_execroot_entry_point.md @@ -104,7 +104,7 @@ bin.rspack( fixed_args = [ "build", "--config", - "$$RUNFILES_DIR/$(rlocationpath :rspack_config)", + "$(rlocation :rspack_config)", ], use_execroot_entry_point = False, ) @@ -128,10 +128,7 @@ Key points: `__dirname`, for specifying the output path. This is key, because the config file (and therefore `__dirname`) will be in the runfiles directory and not the output tree. -- We refer to `"$$RUNFILES_DIR/$(rlocationpath :rspack_config)"` in - `fixed_args`. `$(rlocationpath ...)` is evaluated at analysis time and - determines the path to the config file within the runfiles directory. This - argument must go in `fixed_args` rather than `args`, to allow `$RUNFILES_DIR` - to be evaluated at run time. Note the double dollar sign (`$$`) to prevent - the `js_binary` implementation from attempting to evaluate that variable at - analysis time. +- We refer to `"$(rlocation :rspack_config)"` in `fixed_args`. The `js_binary` + implementation expands this to the absolute path to the config file in the + runfiles directory. Note that this must go in `fixed_args` rather than + `args`. diff --git a/examples/rspack/BUILD.bazel b/examples/rspack/BUILD.bazel index e61fe6c7e..79f307fd8 100644 --- a/examples/rspack/BUILD.bazel +++ b/examples/rspack/BUILD.bazel @@ -27,7 +27,7 @@ bin.rspack( fixed_args = [ "build", "--config", - "$$RUNFILES_DIR/$(rlocationpath :rspack_config)", + "$(rlocation :rspack_config)", ], use_execroot_entry_point = False, ) diff --git a/examples/vite3/BUILD.bazel b/examples/vite3/BUILD.bazel index 8503520ab..2d632b67b 100644 --- a/examples/vite3/BUILD.bazel +++ b/examples/vite3/BUILD.bazel @@ -30,7 +30,7 @@ vite_bin.vite( fixed_args = [ "build", "--config", - "$$RUNFILES_DIR/$(rlocationpath :vite-config)", + "$(rlocation :vite-config)", ], out_dirs = ["build"], ) diff --git a/examples/vite6/BUILD.bazel b/examples/vite6/BUILD.bazel index 86f48bfb5..0b649dc6f 100644 --- a/examples/vite6/BUILD.bazel +++ b/examples/vite6/BUILD.bazel @@ -30,7 +30,7 @@ vite_bin.vite( fixed_args = [ "build", "--config", - "$$RUNFILES_DIR/$(rlocationpath :vite-config)", + "$(rlocation :vite-config)", ], out_dirs = ["build"], ) diff --git a/examples/webpack_cli/BUILD.bazel b/examples/webpack_cli/BUILD.bazel index 733d8d93e..5f9e744c9 100644 --- a/examples/webpack_cli/BUILD.bazel +++ b/examples/webpack_cli/BUILD.bazel @@ -35,7 +35,7 @@ bin.webpack_cli( data = [":webpack-config"], fixed_args = [ "--config", - "$$RUNFILES_DIR/$(rlocationpath :webpack-config)", + "$(rlocation :webpack-config)", ], log_level = "debug", ) diff --git a/js/libs.bzl b/js/libs.bzl index abd332b21..e9e1ca6d1 100644 --- a/js/libs.bzl +++ b/js/libs.bzl @@ -8,6 +8,7 @@ load( "//js/private:js_helpers.bzl", _LOG_LEVELS = "LOG_LEVELS", _envs_for_log_level = "envs_for_log_level", + _expand_rlocation_refs = "expand_rlocation_refs", _gather_files_from_js_infos = "gather_files_from_js_infos", _gather_npm_package_store_infos = "gather_npm_package_store_infos", _gather_npm_sources = "gather_npm_sources", @@ -25,6 +26,7 @@ js_library_lib = _js_library_lib js_lib_helpers = struct( envs_for_log_level = _envs_for_log_level, + expand_rlocation_refs = _expand_rlocation_refs, gather_files_from_js_infos = _gather_files_from_js_infos, gather_npm_sources = _gather_npm_sources, gather_npm_package_store_infos = _gather_npm_package_store_infos, diff --git a/js/private/coverage/bundle/BUILD.bazel b/js/private/coverage/bundle/BUILD.bazel index 34ce8bb5f..7eafe931f 100644 --- a/js/private/coverage/bundle/BUILD.bazel +++ b/js/private/coverage/bundle/BUILD.bazel @@ -27,7 +27,7 @@ rollup_bin.rollup( fixed_args = [ "c8.js", "--config", - "$$RUNFILES_DIR/$(rlocationpath :config)", + "$(rlocation :config)", "--format", "cjs", "--file", diff --git a/js/private/devserver/src/BUILD.bazel b/js/private/devserver/src/BUILD.bazel index a8ae232f1..27d48a5bc 100644 --- a/js/private/devserver/src/BUILD.bazel +++ b/js/private/devserver/src/BUILD.bazel @@ -22,7 +22,7 @@ rollup_bin.rollup( fixed_args = [ "js_run_devserver.mjs", "--config", - "$$RUNFILES_DIR/$(rlocationpath :config)", + "$(rlocation :config)", "--format", "es", "--file", diff --git a/js/private/js_binary.bzl b/js/private/js_binary.bzl index 3e864f8fd..7a85ec33a 100644 --- a/js/private/js_binary.bzl +++ b/js/private/js_binary.bzl @@ -5,7 +5,7 @@ load("@bazel_lib//lib:directory_path.bzl", "DirectoryPathInfo") load("@bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables") 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") +load(":js_helpers.bzl", "LOG_LEVELS", "envs_for_log_level", "expand_rlocation_refs", "gather_files_from_js_infos", "gather_runfiles") _ATTRS = { "chdir": attr.string( @@ -78,8 +78,9 @@ _ATTRS = { ), "expand_args": attr.bool( default = True, - doc = """Enables [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) - and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution for `fixed_args`. + doc = """Enables [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables), + ["Make variable"](https://bazel.build/reference/be/make-variables), and our own custom `$(rlocation)` + substitution for `fixed_args`. This comes at some analysis-time cost even for a set of args that does not have any expansions.""", ), @@ -94,8 +95,14 @@ _ATTRS = { doc = """Fixed command line arguments to pass to the Node.js when this binary target is executed. - Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) - and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution if `expand_args` is set to True. + Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables), + ["Make variable"](https://bazel.build/reference/be/make-variables), and `$(rlocation)` substitution + if `expand_args` is set to True. + + The `$(rlocation :label)` syntax is unique to `js_binary` and expands to the + absolute path to `:label` in the runfiles. This is supported only in `fixed_args`, + because part of the evaluation has to occur at run time, inside the wrapper + script. Unlike the built-in `args`, which are only passed to the target when it is executed either by the `bazel run` command or as a test, `fixed_args` are baked @@ -374,7 +381,7 @@ def _bash_launcher(ctx, nodeinfo, entry_point_path, log_prefix_rule_set, log_pre node_options.append(_NODE_OPTION.format(value = "--preserve-symlinks-main")) if ctx.attr.expand_args: - fixed_args = [expand_variables(ctx, expand_locations(ctx, fixed_arg, ctx.attr.data)) for fixed_arg in fixed_args] + fixed_args = [expand_variables(ctx, expand_locations(ctx, expand_rlocation_refs(fixed_arg), ctx.attr.data)) for fixed_arg in fixed_args] toolchain_files = [] if is_windows: diff --git a/js/private/js_helpers.bzl b/js/private/js_helpers.bzl index 3613839b8..355e9aecf 100644 --- a/js/private/js_helpers.bzl +++ b/js/private/js_helpers.bzl @@ -190,6 +190,36 @@ def gather_runfiles( for target in deps ]) +def expand_rlocation_refs(value): + """Pre-processes $(rlocation