Skip to content

Commit 44740df

Browse files
feat(builtin): add support for using a local .nvmrc file for providing a node version (#2911)
Add `use_nvmrc` attribute on `node_repositories` to provide a Label pointing to the local .nvmrc file in the repository.
1 parent 055d6ae commit 44740df

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

docs/Built-ins.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ This is necessary to bootstrap Bazel to run the package manager to download othe
1919

2020
<pre>
2121
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>,
22-
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-preserve_symlinks">preserve_symlinks</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-vendored_node">vendored_node</a>, <a href="#node_repositories-vendored_yarn">vendored_yarn</a>,
23-
<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>)
22+
<a href="#node_repositories-package_json">package_json</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>,
23+
<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>)
2424
</pre>
2525

2626
To be run in user's WORKSPACE to install rules_nodejs dependencies.
@@ -195,6 +195,14 @@ Defaults to `True`
195195
(*<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`).
196196

197197

198+
<h4 id="node_repositories-use_nvmrc">use_nvmrc</h4>
199+
200+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): the local path of the .nvmrc file containing the version of node
201+
202+
If set then also set node_version to the version found in the .nvmrc file.
203+
204+
Defaults to `None`
205+
198206
<h4 id="node_repositories-vendored_node">vendored_node</h4>
199207

200208
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): the local path to a pre-installed NodeJS runtime.

internal/node/node_repositories.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ and `{filename}` with the matching entry from the `node_repositories` attribute.
177177
default = _DEFAULT_NODE_VERSION,
178178
doc = "the specific version of NodeJS to install or, if vendored_node is specified, the vendored version of node",
179179
),
180+
"use_nvmrc": attr.label(
181+
allow_single_file = True,
182+
default = None,
183+
doc = """the local path of the .nvmrc file containing the version of node
184+
185+
If set then also set node_version to the version found in the .nvmrc file.""",
186+
),
180187
"package_json": attr.label_list(
181188
doc = """(ADVANCED, not recommended)
182189
a list of labels, which indicate the package.json files that will be installed
@@ -281,6 +288,11 @@ def _download_node(repository_ctx):
281288

282289
node_version = repository_ctx.attr.node_version
283290

291+
if repository_ctx.attr.use_nvmrc:
292+
node_version = str(repository_ctx.read(repository_ctx.attr.use_nvmrc)).strip()
293+
294+
_verify_version_is_valid(node_version)
295+
284296
# Skip the download if we know it will fail
285297
if not node_exists_for_os(node_version, host_os):
286298
return
@@ -813,3 +825,8 @@ def node_repositories(**kwargs):
813825
def _maybe(repo_rule, name, **kwargs):
814826
if name not in native.existing_rules():
815827
repo_rule(name = name, **kwargs)
828+
829+
def _verify_version_is_valid(version):
830+
major, minor, patch = (version.split(".") + [None, None, None])[:3]
831+
if not major.isdigit() or not minor.isdigit() or not patch.isdigit():
832+
fail("Invalid node version: %s" % version)

0 commit comments

Comments
 (0)