Skip to content

Commit f0ec5f7

Browse files
authored
refactor: stop reading pnpm_lock from label_store (#2395)
### Changes are visible to end-users: no ### Test plan - Covered by existing test cases
1 parent adb3077 commit f0ec5f7

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

npm/private/npm_translate_lock.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ See https://github.com/aspect-build/rules_js/issues/1445
162162

163163
generate_repository_files(
164164
rctx,
165-
state.label_store.label("pnpm_lock"),
166165
importers,
167166
packages,
168167
state.patched_dependencies(),

npm/private/npm_translate_lock_generate.bzl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ _PACKAGE_JSON_BZL_FILENAME = "package_json.bzl"
8282
_RESOLVED_JSON_FILENAME = "resolved.json"
8383

8484
# buildifier: disable=function-docstring
85-
def generate_repository_files(rctx, pnpm_lock_label, importers, packages, patched_dependencies, only_built_dependencies, root_package, default_registry, npm_registries, npm_auth, link_workspace):
85+
def generate_repository_files(rctx, importers, packages, patched_dependencies, only_built_dependencies, root_package, default_registry, npm_registries, npm_auth, link_workspace):
8686
# empty line after bzl docstring since buildifier expects this if this file is vendored in
87-
generated_by_prefix = "\"\"\"@generated by npm_translate_lock(name = \"{}\", pnpm_lock = \"{}\")\"\"\"\n".format(helpers.to_apparent_repo_name(rctx.name), str(pnpm_lock_label))
87+
generated_by_prefix = "\"\"\"@generated by npm_translate_lock(name = \"{}\", pnpm_lock = \"{}\")\"\"\"\n".format(helpers.to_apparent_repo_name(rctx.name), str(rctx.attr.pnpm_lock))
8888

8989
npm_imports = helpers.get_npm_imports(importers, packages, patched_dependencies, only_built_dependencies, root_package, rctx.name, rctx.attr, rctx.attr.lifecycle_hooks, rctx.attr.lifecycle_hooks_execution_requirements, rctx.attr.lifecycle_hooks_use_default_shell_env, npm_registries, default_registry, npm_auth)
9090

@@ -270,7 +270,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = [], prod = Tru
270270
defs_bzl_file = "@{}//:{}".format(rctx.name, rctx.attr.defs_bzl_filename),
271271
link_packages_comma_separated = "\"'\" + \"', '\".join(_LINK_PACKAGES) + \"'\"" if len(link_packages) else "\"\"",
272272
root_package = root_package,
273-
pnpm_lock_label = pnpm_lock_label,
273+
pnpm_lock_label = rctx.attr.pnpm_lock,
274274
),
275275
]
276276

@@ -589,21 +589,20 @@ def npm_link_all_packages(name = "node_modules", imported_links = [], prod = Tru
589589
rctx_files[rctx.attr.repositories_bzl_filename] = _generate_repositories(
590590
rctx,
591591
npm_imports,
592-
pnpm_lock_label,
593592
link_workspace,
594593
)
595594

596595
for filename, contents in rctx_files.items():
597596
rctx.file(filename, generated_by_prefix + "\n" + "\n".join(contents))
598597

599-
def _generate_repositories(rctx, npm_imports, pnpm_lock_label, link_workspace):
598+
def _generate_repositories(rctx, npm_imports, link_workspace):
600599
repositories_bzl = []
601600

602601
if len(npm_imports) > 0:
603602
repositories_bzl.append("""load("@aspect_rules_js//npm:repositories.bzl", "npm_import")""")
604603
repositories_bzl.append("")
605604

606-
repositories_bzl.append("# Generated npm_import repository rules corresponding to npm packages in {}".format(str(pnpm_lock_label)))
605+
repositories_bzl.append("# Generated npm_import repository rules corresponding to npm packages in {}".format(str(rctx.attr.pnpm_lock)))
607606
repositories_bzl.append("# buildifier: disable=function-docstring")
608607
repositories_bzl.append("def npm_repositories():")
609608
if len(npm_imports) == 0:

npm/private/npm_translate_lock_state.bzl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ WARNING: `update_pnpm_lock` attribute in `npm_translate_lock(name = "{rctx_name}
4545
# labels only needed when updating the pnpm lock file
4646
_init_update_labels(priv, rctx, attr, label_store)
4747

48-
_init_link_workspace(priv, rctx, attr, label_store)
48+
_init_link_workspace(priv, attr, label_store)
4949

5050
# parse the pnpm lock file incase since we need the importers list for additional init
5151
# TODO(windows): utils.exists is not yet support on Windows
5252
pnpm_lock_exists = is_windows or utils.exists(rctx, label_store.path("pnpm_lock"))
5353
if pnpm_lock_exists:
54-
_load_lockfile(priv, rctx, attr, label_store)
54+
_load_lockfile(priv, rctx, label_store)
5555
_init_patched_dependencies_labels(priv, rctx, attr, label_store)
5656

5757
# May depend on lockfile state
@@ -167,12 +167,10 @@ def _init_patches_labels(priv, _, attr, label_store):
167167
################################################################################
168168
def _init_patched_dependencies_labels(priv, _, attr, label_store):
169169
# Read patches from pnpm-lock.yaml `patchedDependencies`
170-
patches = []
171-
for patch_info in priv["patched_dependencies"].values():
172-
patches.append("//%s:%s" % (label_store.label("pnpm_lock").package, patch_info.get("path")))
173-
174-
# Convert patch label strings to labels
175-
patches = [attr.pnpm_lock.relative(p) for p in patches]
170+
patches = [
171+
attr.pnpm_lock.same_package_label(patch_info["path"])
172+
for patch_info in priv["patched_dependencies"].values()
173+
]
176174

177175
for i, d in enumerate(patches):
178176
label_store.add("patches_{}".format(i + priv["num_patches"]), d)
@@ -185,7 +183,7 @@ def _init_importer_labels(priv, label_store):
185183
label_store.add_sibling("lock", "package_json_{}".format(i), paths.join(p, PACKAGE_JSON_FILENAME))
186184

187185
################################################################################
188-
def _init_link_workspace(priv, _, attr, label_store):
186+
def _init_link_workspace(priv, attr, label_store):
189187
# initialize link_workspace either from pnpm_lock label or from override
190188
priv["link_workspace"] = attr.link_workspace if attr.link_workspace else label_store.label("pnpm_lock").repo_name
191189

@@ -283,7 +281,7 @@ def _copy_update_input_files(priv, rctx, attr, label_store):
283281
################################################################################
284282
# we can derive input files that should be specified but are not and copy these over; we warn the user when we do this
285283
def _copy_unspecified_input_files(priv, rctx, attr, label_store):
286-
pnpm_lock_label = label_store.label("pnpm_lock")
284+
pnpm_lock_label = attr.pnpm_lock
287285

288286
# pnpm-workspace.yaml
289287
pnpm_workspace_key = "pnpm_workspace"
@@ -479,7 +477,7 @@ WARNING: Cannot determine home directory in order to load home `.npmrc` file in
479477
_load_npmrc(priv, rctx, home_npmrc_path)
480478

481479
################################################################################
482-
def _load_lockfile(priv, rctx, _, label_store):
480+
def _load_lockfile(priv, rctx, label_store):
483481
importers = {}
484482
packages = {}
485483
patched_dependencies = {}

0 commit comments

Comments
 (0)