-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathnode_runtime_deps_info.bzl
69 lines (58 loc) · 2.89 KB
/
node_runtime_deps_info.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Custom provider that mimics the Runfiles, but doesn't incur the expense of creating the runfiles symlink tree"""
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "add_arg", "register_node_modules_linker")
NodeRuntimeDepsInfo = provider(
doc = """Stores runtime dependencies of a nodejs_binary or nodejs_test
These are files that need to be found by the node module resolver at runtime.
Historically these files were passed using the Runfiles mechanism.
However runfiles has a big performance penalty of creating a symlink forest
with FS API calls for every file in node_modules.
It also causes there to be separate node_modules trees under each binary. This
prevents user-contributed modules passed as deps[] to a particular action from
being found by node module resolver, which expects everything in one tree.
In node, this resolution is done dynamically by assuming a node_modules
tree will exist on disk, so we assume node actions/binary/test executions will
do the same.
""",
fields = {
"pkgs": "list of labels of packages that provide NpmPackageInfo",
"deps": "depset of runtime dependency labels",
},
)
def run_node(ctx, inputs, arguments, executable, **kwargs):
"""Helper to replace ctx.actions.run
This calls node programs with a node_modules directory in place"""
if (type(executable) != "string"):
fail("""run_node requires that executable be provided as a string,
eg. my_executable rather than ctx.executable.my_executable
got %s""" % type(executable))
exec_attr = getattr(ctx.attr, executable)
exec_exec = getattr(ctx.executable, executable)
extra_inputs = []
link_data = []
if (NodeRuntimeDepsInfo in exec_attr):
extra_inputs = exec_attr[NodeRuntimeDepsInfo].deps.to_list()
link_data = exec_attr[NodeRuntimeDepsInfo].pkgs
register_node_modules_linker(ctx, arguments, inputs, link_data)
# By using the run_node helper, you suggest that your program
# doesn't implicitly use runfiles to require() things
# To access runfiles, youu must use a runfiles helper in the program instead
add_arg(arguments, "--nobazel_patch_module_resolver")
ctx.actions.run(
inputs = inputs + extra_inputs,
arguments = arguments,
executable = exec_exec,
**kwargs
)