Skip to content

Commit

Permalink
feat(builtin): split node_loader into node_patcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles committed Aug 24, 2019
1 parent 968b3a3 commit 14c7af3
Show file tree
Hide file tree
Showing 14 changed files with 543 additions and 427 deletions.
29 changes: 25 additions & 4 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 @@ -184,6 +198,7 @@ def _nodejs_binary_impl(ctx):
"TEMPLATED_env_vars": env_vars,
"TEMPLATED_expected_exit_code": str(expected_exit_code),
"TEMPLATED_node": node_tool,
"TEMPLATED_patcher_path": patcher_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 +209,7 @@ def _nodejs_binary_impl(ctx):
is_executable = True,
)

runfiles = depset(node_tool_files + [ctx.outputs.loader, ctx.file._repository_args], transitive = [sources, node_modules])
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 +221,7 @@ def _nodejs_binary_impl(ctx):
transitive_files = runfiles,
files = node_tool_files + [
ctx.outputs.loader,
ctx.outputs.patcher,
] + ctx.files._source_map_support_files +

# We need this call to the list of Files.
Expand Down Expand Up @@ -384,6 +400,10 @@ _NODEJS_EXECUTABLE_ATTRS = {
default = Label("//internal/node:node_loader.js"),
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 +420,7 @@ _NODEJS_EXECUTABLE_ATTRS = {

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

Expand Down
4 changes: 4 additions & 0 deletions internal/node/node_launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ TEMPLATED_env_vars
readonly node=$(rlocation "TEMPLATED_node")
readonly repository_args=$(rlocation "TEMPLATED_repository_args")
readonly script=$(rlocation "TEMPLATED_script_path")
readonly patcher=$(rlocation "TEMPLATED_patcher_path")

source $repository_args

Expand All @@ -130,6 +131,9 @@ for ARG in "${ALL_ARGS[@]}"; do
esac
done

# TODO: what happens if the user passed a require flag?
NODE_OPTIONS+=( "--require=./$patcher" )

# The EXPECTED_EXIT_CODE lets us write bazel tests which assert that
# a binary fails to run. Otherwise any failure would make such a test
# fail before we could assert that we expected that failure.
Expand Down
Loading

0 comments on commit 14c7af3

Please sign in to comment.