Skip to content

Commit 1d650fb

Browse files
author
Lukas Holzer
authored
feat(builtin): use npm ci as default behaviour for installing node_modules (#2328)
To be more hermetic with the install of the dependencies use npm ci to install the exact version from the package-lock.json file. To update a dependency use the vendored npm binary with `bazel run @nodejs//:npm install <dep-name>`. Fixes #159
1 parent 2a4ba8f commit 1d650fb

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ npm_install(
220220
".json",
221221
".proto",
222222
],
223+
npm_command = "install",
223224
package_json = "//:tools/fine_grained_deps_npm/package.json",
224225
package_lock_json = "//:tools/fine_grained_deps_npm/package-lock.json",
225226
symlink_node_modules = False,

internal/bazel_integration_test/test_runner.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ if (config.bazelrcAppend) {
226226
workspaceContents =
227227
workspaceContents.replace(/(yarn_lock[\s\S]+?,)/gm, 'frozen_lockfile = False,\n $1')
228228

229+
// We have to use npm install in favour of npm ci as the package-lock.json would not match the
230+
// replaced version
231+
workspaceContents = workspaceContents.replace(
232+
/(package_lock_json[\s\S]+?,)/gm, 'npm_command = "install",\n $1')
233+
229234
if (!workspaceContents.includes(archiveFile)) {
230235
console.error(
231236
`bazel_integration_test: WORKSPACE replacement for repository ${repositoryKey} failed!`)

internal/npm_install/npm_install.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ def _npm_install_impl(repository_ctx):
206206
is_windows_host = is_windows_os(repository_ctx)
207207
node = repository_ctx.path(get_node_label(repository_ctx))
208208
npm = get_npm_label(repository_ctx)
209-
npm_args = ["install"] + repository_ctx.attr.args
209+
210+
# Set the base command (install or ci)
211+
npm_args = [repository_ctx.attr.npm_command]
212+
213+
npm_args.extend(repository_ctx.attr.args)
210214

211215
# If symlink_node_modules is true then run the package manager
212216
# in the package.json folder; otherwise, run it in the root of
@@ -303,6 +307,17 @@ npm_install = repository_rule(
303307
See npm CLI docs https://docs.npmjs.com/cli/install.html for complete list of supported arguments.""",
304308
default = [],
305309
),
310+
"npm_command": attr.string(
311+
default = "ci",
312+
doc = """The npm command to run, to install dependencies.
313+
314+
See npm docs <https://docs.npmjs.com/cli/v6/commands>
315+
316+
In particular, for "ci" it says:
317+
> If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock.
318+
""",
319+
values = ["ci", "install"],
320+
),
306321
"package_lock_json": attr.label(
307322
mandatory = True,
308323
allow_single_file = True,

0 commit comments

Comments
 (0)