Skip to content

Commit 7ca0688

Browse files
authored
feat: add npm binary & files to toolchain provider (#3570)
1 parent 36e049d commit 7ca0688

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

docs/Core.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ Defaults to `None`
185185
**USAGE**
186186

187187
<pre>
188-
node_toolchain(<a href="#node_toolchain-name">name</a>, <a href="#node_toolchain-run_npm">run_npm</a>, <a href="#node_toolchain-target_tool">target_tool</a>, <a href="#node_toolchain-target_tool_path">target_tool_path</a>)
188+
node_toolchain(<a href="#node_toolchain-name">name</a>, <a href="#node_toolchain-npm">npm</a>, <a href="#node_toolchain-npm_files">npm_files</a>, <a href="#node_toolchain-npm_path">npm_path</a>, <a href="#node_toolchain-run_npm">run_npm</a>, <a href="#node_toolchain-target_tool">target_tool</a>, <a href="#node_toolchain-target_tool_path">target_tool_path</a>)
189189
</pre>
190190

191-
Defines a node toolchain.
191+
Defines a node toolchain for a platform.
192192

193193
You can use this to refer to a vendored nodejs binary in your repository,
194194
or even to compile nodejs from sources using rules_foreign_cc or other rules.
@@ -204,7 +204,9 @@ node_toolchain(
204204
)
205205
```
206206

207-
Next, declare which execution platforms or target platforms the toolchain should be selected for:
207+
Next, declare which execution platforms or target platforms the toolchain should be selected for
208+
based on constraints.
209+
208210
```starlark
209211
toolchain(
210212
name = "my_nodejs",
@@ -217,6 +219,9 @@ toolchain(
217219
)
218220
```
219221

222+
See https://bazel.build/extending/toolchains#toolchain-resolution for more information on toolchain
223+
resolution.
224+
220225
Finally in your `WORKSPACE`, register it with `register_toolchains("//:my_nodejs")`
221226

222227
For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains.
@@ -231,6 +236,24 @@ You can use the `--toolchain_resolution_debug` flag to `bazel` to help diagnose
231236
(*<a href="https://bazel.build/docs/build-ref.html#name">Name</a>, mandatory*): A unique name for this target.
232237

233238

239+
<h4 id="node_toolchain-npm">npm</h4>
240+
241+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): A hermetically downloaded npm executable target for this target's platform.
242+
243+
Defaults to `None`
244+
245+
<h4 id="node_toolchain-npm_files">npm_files</h4>
246+
247+
(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Files required in runfiles to run npm.
248+
249+
Defaults to `[]`
250+
251+
<h4 id="node_toolchain-npm_path">npm_path</h4>
252+
253+
(*String*): Path to an existing npm executable for this target's platform.
254+
255+
Defaults to `""`
256+
234257
<h4 id="node_toolchain-run_npm">run_npm</h4>
235258

236259
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): A template file that allows us to execute npm
@@ -239,13 +262,13 @@ Defaults to `None`
239262

240263
<h4 id="node_toolchain-target_tool">target_tool</h4>
241264

242-
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): A hermetically downloaded nodejs executable target for the target platform.
265+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): A hermetically downloaded nodejs executable target for this target's platform.
243266

244267
Defaults to `None`
245268

246269
<h4 id="node_toolchain-target_tool_path">target_tool_path</h4>
247270

248-
(*String*): Path to an existing nodejs executable for the target platform.
271+
(*String*): Path to an existing nodejs executable for this target's platform.
249272

250273
Defaults to `""`
251274

nodejs/repositories.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ load("@rules_nodejs//nodejs:toolchain.bzl", "node_toolchain")
341341
node_toolchain(
342342
name = "node_toolchain",
343343
target_tool = ":node_bin",
344+
npm = ":npm",
345+
npm_files = [":npm_files"],
344346
run_npm = ":run_npm.template",
345347
)
346348
"""

nodejs/toolchain.bzl

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ See /examples/toolchain for usage example.
2020
NodeInfo = provider(
2121
doc = "Information about how to invoke the node executable.",
2222
fields = {
23-
"target_tool_path": "Path to the nodejs executable for the target platform.",
23+
"target_tool_path": "Path to the nodejs executable for this target's platform.",
2424
"tool_files": """Files required in runfiles to make the nodejs executable available.
2525
2626
May be empty if the target_tool_path points to a locally installed node binary.""",
27+
"npm_path": "Path to the npm executable for this target's platform.",
28+
"npm_files": """Files required in runfiles to make the npm executable available.
29+
30+
May be empty if the npm_path points to a locally installed npm binary.""",
2731
"run_npm": """A template for a script that wraps npm.
2832
On Windows, this is a Batch script, otherwise it uses Bash.""",
2933
},
@@ -41,18 +45,28 @@ def _node_toolchain_impl(ctx):
4145
fail("Can only set one of target_tool or target_tool_path but both were set.")
4246
if not ctx.attr.target_tool and not ctx.attr.target_tool_path:
4347
fail("Must set one of target_tool or target_tool_path.")
48+
if ctx.attr.npm and ctx.attr.npm_path:
49+
fail("Can only set one of npm or npm_path but both were set.")
4450

4551
tool_files = []
4652
target_tool_path = ctx.attr.target_tool_path
4753

4854
if ctx.attr.target_tool:
49-
tool_files = ctx.attr.target_tool.files.to_list()
50-
target_tool_path = _to_manifest_path(ctx, tool_files[0])
55+
tool_files = [ctx.file.target_tool]
56+
target_tool_path = _to_manifest_path(ctx, ctx.file.target_tool)
57+
58+
npm_files = []
59+
npm_path = ctx.attr.npm_path
60+
61+
if ctx.attr.npm:
62+
npm_files = depset([ctx.file.npm] + ctx.files.npm_files).to_list()
63+
npm_path = _to_manifest_path(ctx, ctx.file.npm)
5164

5265
# Make the $(NODE_PATH) variable available in places like genrules.
5366
# See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables
5467
template_variables = platform_common.TemplateVariableInfo({
5568
"NODE_PATH": target_tool_path,
69+
"NPM_PATH": npm_path,
5670
})
5771
default = DefaultInfo(
5872
files = depset(tool_files),
@@ -61,6 +75,8 @@ def _node_toolchain_impl(ctx):
6175
nodeinfo = NodeInfo(
6276
target_tool_path = target_tool_path,
6377
tool_files = tool_files,
78+
npm_path = npm_path,
79+
npm_files = npm_files,
6480
run_npm = ctx.file.run_npm,
6581
)
6682

@@ -81,20 +97,33 @@ node_toolchain = rule(
8197
implementation = _node_toolchain_impl,
8298
attrs = {
8399
"target_tool": attr.label(
84-
doc = "A hermetically downloaded nodejs executable target for the target platform.",
100+
doc = "A hermetically downloaded nodejs executable target for this target's platform.",
85101
mandatory = False,
86102
allow_single_file = True,
87103
),
88104
"target_tool_path": attr.string(
89-
doc = "Path to an existing nodejs executable for the target platform.",
105+
doc = "Path to an existing nodejs executable for this target's platform.",
106+
mandatory = False,
107+
),
108+
"npm": attr.label(
109+
doc = "A hermetically downloaded npm executable target for this target's platform.",
110+
mandatory = False,
111+
allow_single_file = True,
112+
),
113+
"npm_path": attr.string(
114+
doc = "Path to an existing npm executable for this target's platform.",
115+
mandatory = False,
116+
),
117+
"npm_files": attr.label_list(
118+
doc = "Files required in runfiles to run npm.",
90119
mandatory = False,
91120
),
92121
"run_npm": attr.label(
93122
doc = "A template file that allows us to execute npm",
94123
allow_single_file = True,
95124
),
96125
},
97-
doc = """Defines a node toolchain.
126+
doc = """Defines a node toolchain for a platform.
98127
99128
You can use this to refer to a vendored nodejs binary in your repository,
100129
or even to compile nodejs from sources using rules_foreign_cc or other rules.
@@ -110,7 +139,9 @@ node_toolchain(
110139
)
111140
```
112141
113-
Next, declare which execution platforms or target platforms the toolchain should be selected for:
142+
Next, declare which execution platforms or target platforms the toolchain should be selected for
143+
based on constraints.
144+
114145
```starlark
115146
toolchain(
116147
name = "my_nodejs",
@@ -123,6 +154,9 @@ toolchain(
123154
)
124155
```
125156
157+
See https://bazel.build/extending/toolchains#toolchain-resolution for more information on toolchain
158+
resolution.
159+
126160
Finally in your `WORKSPACE`, register it with `register_toolchains("//:my_nodejs")`
127161
128162
For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains.

0 commit comments

Comments
 (0)