Skip to content

Commit bb17273

Browse files
authored
refactor: extricate lockfile verification from npm_translate_lock repo rule (#2478)
After this we should be able to do the work in module extension pretty easily. I'm not sure about the bootstrap step, may need a hack. let's see how it goes! --- ### Changes are visible to end-users: no ### Test plan - Covered by existing test cases
1 parent 4f2c4b7 commit bb17273

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

npm/extensions.bzl

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,23 @@ def _npm_extension_impl(module_ctx):
6969
# Process npm_translate_lock and npm_import tags
7070
for mod in module_ctx.modules:
7171
for attr in mod.tags.npm_translate_lock:
72-
_npm_translate_lock_bzlmod(attr, exclude_package_contents_config, replace_packages)
72+
state = npm_translate_lock_state.new(attr.name, module_ctx, attr)
73+
74+
importers, packages = translate_to_transitive_closure(
75+
state.importers(),
76+
state.packages(),
77+
attr.prod,
78+
attr.dev,
79+
attr.no_optional,
80+
)
81+
82+
_npm_translate_lock_bzlmod(attr, state, importers, packages, exclude_package_contents_config, replace_packages)
7383

7484
# We cannot read the pnpm_lock file before it has been bootstrapped.
7585
# See comment in e2e/update_pnpm_lock_with_import/test.sh.
7686
if attr.pnpm_lock:
7787
module_ctx.watch(attr.pnpm_lock)
78-
_npm_lock_imports_bzlmod(module_ctx, attr, exclude_package_contents_config, replace_packages)
88+
_npm_lock_imports_bzlmod(module_ctx, attr, state, importers, packages, exclude_package_contents_config, replace_packages)
7989

8090
for i in mod.tags.npm_import:
8191
_npm_import_bzlmod(i)
@@ -108,7 +118,7 @@ def _build_exclude_package_contents_config(module_ctx):
108118

109119
return exclusions
110120

111-
def _npm_translate_lock_bzlmod(attr, exclude_package_contents_config, replace_packages):
121+
def _npm_translate_lock_bzlmod(attr, state, importers, packages, exclude_package_contents_config, replace_packages):
112122
# TODO(3.0): remove this warning when replace_packages attribute is removed
113123
if attr.replace_packages:
114124
# buildifier: disable=print
@@ -132,6 +142,10 @@ The 'replace_packages' attribute will be removed in rules_js version 3.0.
132142
fail("Package replacement conflict: {} specified in both replace_packages attribute and npm_replace_package tag".format(package))
133143
replace_packages[package] = replacement
134144

145+
# TODO(zbarsky): Use these
146+
# buildifier: disable=unused-variable
147+
_ = state, importers, packages
148+
135149
npm_translate_lock_rule(
136150
name = attr.name,
137151
bins = attr.bins,
@@ -165,17 +179,7 @@ The 'replace_packages' attribute will be removed in rules_js version 3.0.
165179
additional_file_contents = attr.additional_file_contents,
166180
)
167181

168-
def _npm_lock_imports_bzlmod(module_ctx, attr, exclude_package_contents_config, replace_packages):
169-
state = npm_translate_lock_state.new(attr.name, module_ctx, attr)
170-
171-
importers, packages = translate_to_transitive_closure(
172-
state.importers(),
173-
state.packages(),
174-
attr.prod,
175-
attr.dev,
176-
attr.no_optional,
177-
)
178-
182+
def _npm_lock_imports_bzlmod(module_ctx, attr, state, importers, packages, exclude_package_contents_config, replace_packages):
179183
registries = {}
180184
npm_auth = {}
181185
if attr.npmrc:

npm/private/npm_translate_lock.bzl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,14 @@ npm_translate_lock_lib = struct(
110110
attrs = _ATTRS,
111111
)
112112

113-
################################################################################
114-
def _npm_translate_lock_impl(rctx):
115-
rctx.report_progress("Initializing")
113+
def parse_and_verify_lock(rctx):
114+
"""Helper to parse and validate the lockfile
115+
116+
Args:
117+
rctx: repository context
118+
Returns:
119+
state, importers, and packages
120+
"""
116121

117122
state = npm_translate_lock_state.new(rctx.name, rctx, rctx.attr)
118123

@@ -153,6 +158,14 @@ See https://github.com/aspect-build/rules_js/issues/1445
153158
rctx.attr.no_optional,
154159
)
155160

161+
return state, importers, packages
162+
163+
################################################################################
164+
def _npm_translate_lock_impl(rctx):
165+
rctx.report_progress("Initializing")
166+
167+
state, importers, packages = parse_and_verify_lock(rctx)
168+
156169
rctx.report_progress("Generating starlark for npm dependencies")
157170

158171
files = generate_repository_files(

0 commit comments

Comments
 (0)