Skip to content

Commit

Permalink
fix(): remove _repository_args from nodejs_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
dymart authored and gregmagolan committed Dec 20, 2021
1 parent b178092 commit 90c7fe0
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 75 deletions.
24 changes: 9 additions & 15 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ This is necessary to bootstrap Bazel to run the package manager to download othe

<pre>
node_repositories(<a href="#node_repositories-name">name</a>, <a href="#node_repositories-node_download_auth">node_download_auth</a>, <a href="#node_repositories-node_repositories">node_repositories</a>, <a href="#node_repositories-node_urls">node_urls</a>, <a href="#node_repositories-node_version">node_version</a>,
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-platform">platform</a>, <a href="#node_repositories-preserve_symlinks">preserve_symlinks</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-use_nvmrc">use_nvmrc</a>, <a href="#node_repositories-vendored_node">vendored_node</a>,
<a href="#node_repositories-vendored_yarn">vendored_yarn</a>, <a href="#node_repositories-yarn_download_auth">yarn_download_auth</a>, <a href="#node_repositories-yarn_repositories">yarn_repositories</a>, <a href="#node_repositories-yarn_urls">yarn_urls</a>, <a href="#node_repositories-yarn_version">yarn_version</a>)
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-platform">platform</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-use_nvmrc">use_nvmrc</a>, <a href="#node_repositories-vendored_node">vendored_node</a>, <a href="#node_repositories-vendored_yarn">vendored_yarn</a>,
<a href="#node_repositories-yarn_download_auth">yarn_download_auth</a>, <a href="#node_repositories-yarn_repositories">yarn_repositories</a>, <a href="#node_repositories-yarn_urls">yarn_urls</a>, <a href="#node_repositories-yarn_version">yarn_version</a>)
</pre>

To be run in user's WORKSPACE to install rules_nodejs dependencies.
Expand Down Expand Up @@ -184,18 +184,6 @@ Defaults to `[]`

Defaults to `""`

<h4 id="node_repositories-preserve_symlinks">preserve_symlinks</h4>

(*Boolean*): Turn on --node_options=--preserve-symlinks for nodejs_binary and nodejs_test rules.

When this option is turned on, node will preserve the symlinked path for resolves instead of the default
behavior of resolving to the real path. This means that all required files must be in be included in your
runfiles as it prevents the default behavior of potentially resolving outside of the runfiles. For example,
all required files need to be included in your node_modules filegroup. This option is desirable as it gives
a stronger guarantee of hermeticity which is required for remote execution.

Defaults to `True`

<h4 id="node_repositories-repo_mapping">repo_mapping</h4>

(*<a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a>, mandatory*): A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).
Expand Down Expand Up @@ -266,7 +254,9 @@ nodejs_binary(<a href="#nodejs_binary-name">name</a>, <a href="#nodejs_binary-ch
<a href="#nodejs_binary-link_workspace_root">link_workspace_root</a>, <a href="#nodejs_binary-templated_args">templated_args</a>, <a href="#nodejs_binary-toolchain">toolchain</a>)
</pre>

Runs some JavaScript code in NodeJS.
Runs some JavaScript code in NodeJS. You can also change the default args that are sent to nodejs. This can be done through a flag. The default is --preserve-symlinks while anything
can be passed. The flag is --@build_bazel_rules_nodejs//nodejs:default_args="" ex: bazel build --@build_bazel_rules_nodejs//nodejs:default_args="--preserve-symlinks --no-warnings" main
This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available node flags can be found here: https://nodejs.org/api/cli.html.

**ATTRIBUTES**

Expand Down Expand Up @@ -521,6 +511,10 @@ Now you can add `--config=debug` to any `bazel test` command line.
The runtime will pause before executing the program, allowing you to connect a
remote debugger.

You can also change the default args that are sent to nodejs. This can be done through a flag. The default is --preserve-symlinks while anything
can be passed. The flag is --@build_bazel_rules_nodejs//nodejs:default_args="" ex: bazel test --@build_bazel_rules_nodejs//nodejs:default_args="--preserve-symlinks --no-warnings" main
This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available node flags can be found here: https://nodejs.org/api/cli.html.


**ATTRIBUTES**

Expand Down
4 changes: 4 additions & 0 deletions docs/Cypress.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Now you can add `--config=debug` to any `bazel test` command line.
The runtime will pause before executing the program, allowing you to connect a
remote debugger.

You can also change the default args that are sent to nodejs. This can be done through a flag. The default is --preserve-symlinks while anything
can be passed. The flag is --@build_bazel_rules_nodejs//nodejs:default_args="" ex: bazel test --@build_bazel_rules_nodejs//nodejs:default_args="--preserve-symlinks --no-warnings" main
This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available node flags can be found here: https://nodejs.org/api/cli.html.


**ATTRIBUTES**

Expand Down
2 changes: 1 addition & 1 deletion e2e/node_loader_no_preserve_symlinks/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rules_nodejs_dependencies()

load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")

node_repositories(preserve_symlinks = False)
node_repositories()

yarn_install(
name = "npm",
Expand Down
57 changes: 56 additions & 1 deletion e2e/nodejs_image/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@io_bazel_rules_docker//contrib:test.bzl", "container_test")
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

nodejs_binary(
name = "main",
Expand All @@ -11,14 +12,68 @@ nodejs_binary(
entry_point = "main.js",
)

# bazel run --platforms=@rules_nodejs//nodejs:linux_amd64 //:nodejs_image
# bazel run --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //:nodejs_image
nodejs_image(
name = "nodejs_image",
binary = ":main",
include_node_repo_args = False,
)

container_test(
name = "nodejs_image_test",
configs = [":nodejs_image.yaml"],
image = ":nodejs_image",
)

# this creates:
# bazel run main_node15_linux_amd64
# bazel run main_node16_linux_amd64
# bazel run nodejs_image_node15_linux_amd64
# bazel run nodejs_image_node16_linux_amd64
# bazel test nodejs_image_test_node15_linux_amd64
# bazel test nodejs_image_test_node16_linux_amd64
[
[
# Trivial test fixture: a nodejs program that writes to a file
write_file(
name = "test_" + id,
out = "image_%s.yaml" % id,
content = [
"schemaVersion: 2.0.0",
"metadataTest:",
" entrypoint: ['/app//main_%s']" % id,
""" workdir: "/app//main_%s.runfiles/e2e_nodejs_image" """ % id,
],
),
nodejs_binary(
name = "main_" + id,
data = [
"//foolib",
"@npm//date-fns",
],
entry_point = "binary_version.js",
toolchain = toolchain,
),
nodejs_image(
name = "nodejs_image_" + id,
binary = ":main_" + id,
include_node_repo_args = False,
node_repository_name = id,
),
container_test(
name = "nodejs_image_test_" + id,
configs = ["test_" + id],
image = ":nodejs_image_" + id,
),
]
for id, toolchain in zip(
[
"node15_linux_amd64",
"node16_linux_amd64",
],
[
"@node15_linux_amd64//:node_toolchain",
"@node16_linux_amd64//:node_toolchain",
],
)
]
20 changes: 17 additions & 3 deletions e2e/nodejs_image/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ yarn_install(

http_archive(
name = "io_bazel_rules_docker",
sha256 = "20dfc1d9bc6a251455c4f306d3dea030b323ea3f22e0600698fab9665eb273b8",
strip_prefix = "rules_docker-0086a29d19b124b04a864e3913cfa7a2cba46349",
urls = ["https://github.com/bazelbuild/rules_docker/archive/0086a29d19b124b04a864e3913cfa7a2cba46349.tar.gz"],
sha256 = "db427107e4c36a3713c24daf5fcdf7719fe664e0c36e089b5b16d15cf331ce60",
strip_prefix = "rules_docker-8a4f73fb29a64ba813087220b200f49a1ca10faa",
urls = ["https://github.com/bazelbuild/rules_docker/archive/8a4f73fb29a64ba813087220b200f49a1ca10faa.tar.gz"],
)

load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
Expand All @@ -59,3 +59,17 @@ container_deps()
load("@io_bazel_rules_docker//nodejs:image.bzl", nodejs_image_repositories = "repositories")

nodejs_image_repositories()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

# The order matters because Bazel will provide the first registered toolchain when a rule asks Bazel to select it
# This applies to the resolved_toolchain
nodejs_register_toolchains(
name = "node16",
node_version = "16.5.0",
)

nodejs_register_toolchains(
name = "node15",
node_version = "15.14.0",
)
1 change: 1 addition & 0 deletions e2e/nodejs_image/binary_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(process.version)
6 changes: 3 additions & 3 deletions examples/angular/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ http_archive(

http_archive(
name = "io_bazel_rules_docker",
sha256 = "7863f4fb51e4801b91942ab3cf00a673c53ed555d27a56f4287c2619d13830eb",
strip_prefix = "rules_docker-86c54e5a3a2a4ae9c0cc39b3af7d94c04ac689ab",
urls = ["https://github.com/bazelbuild/rules_docker/archive/86c54e5a3a2a4ae9c0cc39b3af7d94c04ac689ab.tar.gz"],
sha256 = "db427107e4c36a3713c24daf5fcdf7719fe664e0c36e089b5b16d15cf331ce60",
strip_prefix = "rules_docker-8a4f73fb29a64ba813087220b200f49a1ca10faa",
urls = ["https://github.com/bazelbuild/rules_docker/archive/8a4f73fb29a64ba813087220b200f49a1ca10faa.tar.gz"],
)

load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
Expand Down
1 change: 1 addition & 0 deletions examples/angular/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ history_server(
nodejs_image(
name = "nodejs_image",
binary = ":prodserver",
include_node_repo_args = False,
# Actions created by this rule are I/O-bound,
# so there is no benefit to running them remotely
tags = ["local"],
Expand Down
6 changes: 3 additions & 3 deletions examples/nestjs/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ yarn_install(

http_archive(
name = "io_bazel_rules_docker",
sha256 = "7863f4fb51e4801b91942ab3cf00a673c53ed555d27a56f4287c2619d13830eb",
strip_prefix = "rules_docker-86c54e5a3a2a4ae9c0cc39b3af7d94c04ac689ab",
urls = ["https://github.com/bazelbuild/rules_docker/archive/86c54e5a3a2a4ae9c0cc39b3af7d94c04ac689ab.tar.gz"],
sha256 = "db427107e4c36a3713c24daf5fcdf7719fe664e0c36e089b5b16d15cf331ce60",
strip_prefix = "rules_docker-8a4f73fb29a64ba813087220b200f49a1ca10faa",
urls = ["https://github.com/bazelbuild/rules_docker/archive/8a4f73fb29a64ba813087220b200f49a1ca10faa.tar.gz"],
)

load(
Expand Down
1 change: 1 addition & 0 deletions examples/nestjs/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ jasmine_node_test(
nodejs_image(
name = "docker",
binary = ":server",
include_node_repo_args = False,
)
1 change: 0 additions & 1 deletion internal/bazel_integration_test/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ for (const bazelCommand of config.bazelCommands) {
'GTEST_TMP_DIR',
'INIT_CWD',
'JAVA_RUNFILES',
'NODE_REPOSITORY_ARGS',
'OLDPWD',
'PYTHON_RUNFILES',
'RUN_UNDER_RUNFILES',
Expand Down
7 changes: 2 additions & 5 deletions internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,11 @@ if [[ "${BAZEL_NODE_PATCH_REQUIRE}" != /* ]] && [[ ! "${BAZEL_NODE_PATCH_REQUIRE
export BAZEL_NODE_PATCH_REQUIRE=$(pwd)/${BAZEL_NODE_PATCH_REQUIRE}
fi

readonly repository_args=$(rlocation "TEMPLATED_repository_args")
readonly lcov_merger_script=$(rlocation "TEMPLATED_lcov_merger_script")

source $repository_args

ARGS=()
LAUNCHER_NODE_OPTIONS=($NODE_REPOSITORY_ARGS)
NODE_ARGS=("TEMPLATED_node_args")
LAUNCHER_NODE_OPTIONS=($NODE_ARGS)
USER_NODE_OPTIONS=()
ALL_ARGS=(TEMPLATED_args "$@")
STDOUT_CAPTURE=""
Expand Down Expand Up @@ -390,7 +388,6 @@ if [[ -n "$NODE_WORKING_DIR" ]]; then
cd "$NODE_WORKING_DIR"
fi
set +e

if [[ -n "${STDOUT_CAPTURE}" ]] && [[ -n "${STDERR_CAPTURE}" ]]; then
"${node}" ${LAUNCHER_NODE_OPTIONS[@]+"${LAUNCHER_NODE_OPTIONS[@]}"} ${USER_NODE_OPTIONS[@]+"${USER_NODE_OPTIONS[@]}"} "${MAIN}" ${ARGS[@]+"${ARGS[@]}"} <&0 >$STDOUT_CAPTURE 2>$STDERR_CAPTURE &
elif [[ -n "${STDOUT_CAPTURE}" ]]; then
Expand Down
17 changes: 10 additions & 7 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ load("//internal/common:path_utils.bzl", "strip_external")
load("//internal/common:preserve_legacy_templated_args.bzl", "preserve_legacy_templated_args")
load("//internal/common:windows_utils.bzl", "create_windows_native_launcher_script", "is_windows")
load("//internal/linker:link_node_modules.bzl", "LinkerPackageMappingInfo", "module_mappings_aspect", "write_node_modules_manifest")
load("//nodejs:providers.bzl", "UserBuildSettingInfo")

def _trim_package_node_modules(package_name):
# trim a package name down to its path prior to a node_modules
Expand Down Expand Up @@ -267,7 +268,6 @@ fi
runfiles.extend(ctx.files._bash_runfile_helper)
runfiles.append(ctx.outputs.loader_script)
runfiles.append(ctx.outputs.require_patch_script)
runfiles.append(ctx.file._repository_args)

# First replace any instances of "$(rlocation " with "$$(rlocation " to preserve
# legacy uses of "$(rlocation"
Expand Down Expand Up @@ -316,10 +316,10 @@ fi
"TEMPLATED_loader_script": _to_manifest_path(ctx, ctx.outputs.loader_script),
"TEMPLATED_modules_manifest": _to_manifest_path(ctx, node_modules_manifest),
"TEMPLATED_node_patches_script": _to_manifest_path(ctx, ctx.file._node_patches_script),
"TEMPLATED_repository_args": _to_manifest_path(ctx, ctx.file._repository_args),
"TEMPLATED_require_patch_script": _to_manifest_path(ctx, ctx.outputs.require_patch_script),
"TEMPLATED_runfiles_helper_script": _to_manifest_path(ctx, ctx.file._runfile_helpers_main),
"TEMPLATED_vendored_node": strip_external(node_toolchain.nodeinfo.target_tool_path),
"TEMPLATED_node_args": ctx.attr._node_args[UserBuildSettingInfo].value,
}

# TODO when we have "link_all_bins" we will only need to look in one place for the entry point
Expand Down Expand Up @@ -594,10 +594,7 @@ Predefined genrule variables are not supported in this context.
allow_single_file = True,
),
"toolchain": attr.label(),
"_repository_args": attr.label(
default = Label("@nodejs//:bin/node_repo_args.sh"),
allow_single_file = True,
),
"_node_args": attr.label(default = "//nodejs:default_args"),
"_node_patches_script": attr.label(
default = Label("//internal/node:node_patches.js"),
allow_single_file = True,
Expand Down Expand Up @@ -632,7 +629,9 @@ _NODEJS_EXECUTABLE_OUTPUTS = {

nodejs_binary_kwargs = {
"attrs": _NODEJS_EXECUTABLE_ATTRS,
"doc": "Runs some JavaScript code in NodeJS.",
"doc": """Runs some JavaScript code in NodeJS. You can also change the default args that are sent to nodejs. This can be done through a flag. The default is --preserve-symlinks while anything
can be passed. The flag is --@build_bazel_rules_nodejs//nodejs:default_args="" ex: bazel build --@build_bazel_rules_nodejs//nodejs:default_args="--preserve-symlinks --no-warnings" main
This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available node flags can be found here: https://nodejs.org/api/cli.html.""",
"executable": True,
"implementation": _nodejs_binary_impl,
"outputs": _NODEJS_EXECUTABLE_OUTPUTS,
Expand Down Expand Up @@ -695,6 +694,10 @@ test:debug --test_arg=--node_options=--inspect-brk --test_output=streamed --test
Now you can add `--config=debug` to any `bazel test` command line.
The runtime will pause before executing the program, allowing you to connect a
remote debugger.
You can also change the default args that are sent to nodejs. This can be done through a flag. The default is --preserve-symlinks while anything
can be passed. The flag is --@build_bazel_rules_nodejs//nodejs:default_args="" ex: bazel test --@build_bazel_rules_nodejs//nodejs:default_args="--preserve-symlinks --no-warnings" main
This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available node flags can be found here: https://nodejs.org/api/cli.html.
""",
test = True,
)
Expand Down
6 changes: 3 additions & 3 deletions internal/node/node_patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ const patcher = (requireScriptName, nodeDir) => {
fs__default['default'].writeFileSync(nodeEntry, `@if not defined DEBUG_HELPER @ECHO OFF
set NP_SUBPROCESS_NODE_DIR=${nodeDir}
set Path=${nodeDir};%Path%
"${process.execPath}" ${process.env.NODE_REPOSITORY_ARGS} --require "${requireScriptName}" %*
"${process.execPath}" --require "${requireScriptName}" %*
`);
}
}
Expand All @@ -581,9 +581,9 @@ set Path=${nodeDir};%Path%
export NP_SUBPROCESS_NODE_DIR="${nodeDir}"
export PATH="${nodeDir}":\$PATH
if [[ ! "\${@}" =~ "${file}" ]]; then
exec ${process.execPath} ${process.env.NODE_REPOSITORY_ARGS} --require "${requireScriptName}" "$@"
exec ${process.execPath} --require "${requireScriptName}" "$@"
else
exec ${process.execPath} ${process.env.NODE_REPOSITORY_ARGS} "$@"
exec ${process.execPath} "$@"
fi
`, { mode: 0o777 });
}
Expand Down
13 changes: 13 additions & 0 deletions nodejs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//nodejs/private:toolchains_repo.bzl", "PLATFORMS")
load("//nodejs/private:user_build_settings.bzl", "user_args")

filegroup(
name = "package_contents",
Expand Down Expand Up @@ -36,3 +37,15 @@ toolchain_type(
)
for key, values in PLATFORMS.items()
]

# User set flags that are passed to the build
# you need to make sure that you pass the correct repo name infront for this flag to work
# if you are using rules from build_bazel_rules_nodejs that should be the @ in the flag
# ex: bazel build --@build_bazel_rules_nodejs//nodejs:default_args="test" main
# use rules_nodejs when you use things from that repo
# bazel test --//nodejs:default_args="" //...
user_args(
name = "default_args",
build_setting_default = "--preserve-symlinks",
visibility = ["//visibility:public"],
)
1 change: 0 additions & 1 deletion nodejs/private/nodejs_repo_host_os_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package(default_visibility = ["//visibility:public"])
# aliases for exports_files
alias(name = "run_npm.sh.template", actual = "@{node_repository}_{os_name}//:run_npm.sh.template")
alias(name = "run_npm.bat.template", actual = "@{node_repository}_{os_name}//:run_npm.bat.template")
alias(name = "bin/node_repo_args.sh", actual = "@{node_repository}_{os_name}//:bin/node_repo_args.sh")
# aliases for other aliases
alias(name = "node_bin", actual = "@{node_repository}_{os_name}//:node_bin")
Expand Down
3 changes: 3 additions & 0 deletions nodejs/private/providers/user_build_settings.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""To pass information about the user defined flags
"""
UserBuildSettingInfo = provider(fields = ["value"])
20 changes: 20 additions & 0 deletions nodejs/private/user_build_settings.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Function for user args to be set on the command line
"""

load("//nodejs/private/providers:user_build_settings.bzl", "UserBuildSettingInfo")

def _impl(ctx):
# keeping this as a function to make it easier to
# check flags or add logic in the future

# Returns a provider like a normal rule
return UserBuildSettingInfo(value = ctx.build_setting_value)

user_args = rule(
implementation = _impl,
# This line separates a build setting from a regular target, by using
# the `build_setting` atttribute, you mark this rule as a build setting
# including what raw type it is and if it can be used on the command
# line or not (if yes, you must set `flag = True`)
build_setting = config.string(flag = True),
)

0 comments on commit 90c7fe0

Please sign in to comment.