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

feat(builtin): split node_loader into node_patcher #1003

Closed
wants to merge 3 commits into from
Closed
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
46 changes: 41 additions & 5 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,28 @@ def _write_loader_script(ctx):
template = ctx.file._loader_template,
output = ctx.outputs.loader,
substitutions = {
"TEMPLATED_bin_dir": ctx.bin_dir.path,
"TEMPLATED_bootstrap": "\n " + ",\n ".join(
["\"" + d + "\"" for d in ctx.attr.bootstrap],
),
"TEMPLATED_entry_point": entry_point_path,
"TEMPLATED_gen_dir": ctx.genfiles_dir.path,
"TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(),
"TEMPLATED_node_modules_root": node_modules_root,
"TEMPLATED_target": str(ctx.label),
},
is_executable = True,
)

ctx.actions.expand_template(
template = ctx.file._patcher_template,
output = ctx.outputs.patcher,
substitutions = {
"TEMPLATED_bin_dir": ctx.bin_dir.path,
"TEMPLATED_gen_dir": ctx.genfiles_dir.path,
"TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings),
"TEMPLATED_node_modules_root": node_modules_root,
"TEMPLATED_target": str(ctx.label),
"TEMPLATED_user_workspace_name": ctx.workspace_name,
},
is_executable = True,
)

def _short_path_to_manifest_path(ctx, short_path):
Expand Down Expand Up @@ -149,11 +158,16 @@ def _nodejs_binary_impl(ctx):
# Avoid writing non-normalized paths (workspace/../other_workspace/path)
if ctx.outputs.loader.short_path.startswith("../"):
script_path = ctx.outputs.loader.short_path[len("../"):]
patcher_path = ctx.outputs.patcher.short_path[len("../"):]
else:
script_path = "/".join([
ctx.workspace_name,
ctx.outputs.loader.short_path,
])
patcher_path = "/".join([
ctx.workspace_name,
ctx.outputs.patcher.short_path,
])
env_vars = "export BAZEL_TARGET=%s\n" % ctx.label
for k in ctx.attr.configuration_env_vars:
if k in ctx.var.keys():
Expand Down Expand Up @@ -183,7 +197,7 @@ def _nodejs_binary_impl(ctx):
]),
"TEMPLATED_env_vars": env_vars,
"TEMPLATED_expected_exit_code": str(expected_exit_code),
"TEMPLATED_node": node_tool,
"TEMPLATED_node_proxy_path": _short_path_to_manifest_path(ctx, ctx.outputs.node_proxy.short_path),
"TEMPLATED_repository_args": _short_path_to_manifest_path(ctx, ctx.file._repository_args.short_path),
"TEMPLATED_script_path": script_path,
}
Expand All @@ -194,7 +208,17 @@ def _nodejs_binary_impl(ctx):
is_executable = True,
)

runfiles = depset(node_tool_files + [ctx.outputs.loader, ctx.file._repository_args], transitive = [sources, node_modules])
ctx.actions.expand_template(
template = ctx.file._node_proxy_template,
output = ctx.outputs.node_proxy,
substitutions = {
"TEMPLATED_node": node_tool,
"TEMPLATED_patcher_path": patcher_path,
},
is_executable = True,
)

runfiles = depset(node_tool_files + [ctx.outputs.loader, ctx.outputs.patcher, ctx.file._repository_args], transitive = [sources, node_modules])

# entry point is only needed in runfiles if it is a .js file
if ctx.file.entry_point.extension == "js":
Expand All @@ -206,6 +230,8 @@ def _nodejs_binary_impl(ctx):
transitive_files = runfiles,
files = node_tool_files + [
ctx.outputs.loader,
ctx.outputs.patcher,
ctx.outputs.node_proxy,
] + ctx.files._source_map_support_files +

# We need this call to the list of Files.
Expand Down Expand Up @@ -384,6 +410,14 @@ _NODEJS_EXECUTABLE_ATTRS = {
default = Label("//internal/node:node_loader.js"),
allow_single_file = True,
),
"_node_proxy_template": attr.label(
default = Label("//internal/node:node_proxy.sh"),
allow_single_file = True,
),
"_patcher_template": attr.label(
default = Label("//internal/node:node_patcher.js"),
allow_single_file = True,
),
"_repository_args": attr.label(
default = Label("@nodejs//:bin/node_repo_args.sh"),
allow_single_file = True,
Expand All @@ -400,6 +434,8 @@ _NODEJS_EXECUTABLE_ATTRS = {

_NODEJS_EXECUTABLE_OUTPUTS = {
"loader": "%{name}_loader.js",
"node_proxy": "%{name}_node_proxy/node",
"patcher": "%{name}_patcher.js",
"script": "%{name}.sh",
}

Expand Down
2 changes: 1 addition & 1 deletion internal/node/node_launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TEMPLATED_env_vars
# This redirects to stderr so it doesn't interfere with Bazel's worker protocol
# find . -name thingImLookingFor 1>&2

readonly node=$(rlocation "TEMPLATED_node")
readonly node=$(rlocation "TEMPLATED_node_proxy_path")
readonly repository_args=$(rlocation "TEMPLATED_repository_args")
readonly script=$(rlocation "TEMPLATED_script_path")

Expand Down
Loading