Skip to content

Commit ef88540

Browse files
committed
Stop reading pnpm_lock from label_store
1 parent e7beb10 commit ef88540

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

npm/private/npm_translate_lock.bzl

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Advanced users may want to directly fetch a package from npm rather than start f
2727

2828
load("@aspect_bazel_lib//lib:utils.bzl", bazel_lib_utils = "utils")
2929
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
30-
load("@bazel_skylib//lib:paths.bzl", "paths")
3130
load(":exclude_package_contents_default.bzl", "exclude_package_contents_default")
3231
load(":list_sources.bzl", "list_sources")
3332
load(":npm_translate_lock_generate.bzl", "generate_repository_files")
@@ -139,7 +138,7 @@ def _npm_translate_lock_impl(rctx):
139138
INFO: {} file updated. Please run your build again.
140139
141140
See https://github.com/aspect-build/rules_js/issues/1445
142-
""".format(state.label_store.relative_path("pnpm_lock"))
141+
""".format(rctx.path(rctx.attr.pnpm_lock))
143142
fail(msg)
144143

145144
helpers.verify_node_modules_ignored(rctx, state.importers(), state.root_package())
@@ -148,7 +147,7 @@ See https://github.com/aspect-build/rules_js/issues/1445
148147

149148
helpers.verify_lifecycle_hooks_specified(rctx, state)
150149

151-
rctx.report_progress("Translating {}".format(state.label_store.relative_path("pnpm_lock")))
150+
rctx.report_progress("Translating %s" % str(rctx.path(rctx.attr.pnpm_lock)))
152151

153152
importers, packages = translate_to_transitive_closure(
154153
state.importers(),
@@ -162,7 +161,6 @@ See https://github.com/aspect-build/rules_js/issues/1445
162161

163162
generate_repository_files(
164163
rctx,
165-
state.label_store.label("pnpm_lock"),
166164
importers,
167165
packages,
168166
state.patched_dependencies(),
@@ -693,12 +691,12 @@ def list_patches(name, out = None, include_patterns = ["*.diff", "*.patch"], exc
693691

694692
################################################################################
695693
def _bootstrap_import(rctx, state):
696-
pnpm_lock_label = state.label_store.label("pnpm_lock")
697-
pnpm_lock_path = state.label_store.path("pnpm_lock")
694+
pnpm_lock_label = rctx.attr.pnpm_lock
695+
pnpm_lock_path = rctx.path(pnpm_lock_label)
698696

699697
# Check if the pnpm lock file already exists and copy it over if it does.
700698
# When we do this, warn the user that we do.
701-
if utils.exists(rctx, pnpm_lock_path):
699+
if pnpm_lock_path.exists:
702700
# buildifier: disable=print
703701
print("""
704702
WARNING: Implicitly using pnpm-lock.yaml file `{pnpm_lock}` that is expected to be the result of running `pnpm import` on the `{lock}` lock file.
@@ -711,7 +709,7 @@ WARNING: Implicitly using pnpm-lock.yaml file `{pnpm_lock}` that is expected to
711709
# because at this point the user has likely not added all package.json and data files that
712710
# pnpm import depends on to `npm_translate_lock`. In order to get a complete initial pnpm lock
713711
# file with all workspace package imports listed we likely need to run in the source tree.
714-
bootstrap_working_directory = paths.dirname(pnpm_lock_path)
712+
bootstrap_working_directory = pnpm_lock_path.dirname
715713

716714
if not rctx.attr.quiet:
717715
# buildifier: disable=print
@@ -729,7 +727,7 @@ INFO: Running initial `pnpm import` in `{wd}` to bootstrap the pnpm-lock.yaml fi
729727
state.label_store.path("pnpm_entry"),
730728
"import",
731729
],
732-
working_directory = bootstrap_working_directory,
730+
working_directory = str(bootstrap_working_directory),
733731
quiet = rctx.attr.quiet,
734732
)
735733
if result.return_code:
@@ -741,7 +739,7 @@ STDERR:
741739
""".format(status = result.return_code, stdout = result.stdout, stderr = result.stderr)
742740
fail(msg)
743741

744-
if not utils.exists(rctx, pnpm_lock_path):
742+
if not pnpm_lock_path.exists:
745743
msg = """
746744
747745
ERROR: Running `pnpm import` did not generate the {path} file.
@@ -802,11 +800,10 @@ STDERR:
802800
def _update_pnpm_lock(rctx, state):
803801
_execute_preupdate_scripts(rctx, state)
804802

805-
pnpm_lock_label = state.label_store.label("pnpm_lock")
806-
pnpm_lock_relative_path = state.label_store.relative_path("pnpm_lock")
803+
pnpm_lock_relative_path = str(rctx.path(rctx.attr.pnpm_lock))
807804

808805
update_cmd = ["import"] if rctx.attr.npm_package_lock or rctx.attr.yarn_lock else ["install", "--lockfile-only"]
809-
update_working_directory = paths.dirname(state.label_store.repository_path("pnpm_lock"))
806+
update_working_directory = rctx.workspace_root.get_child(pnpm_lock_relative_path).dirname
810807

811808
pnpm_cmd = " ".join(update_cmd)
812809

@@ -833,7 +830,7 @@ INFO: Updating `{pnpm_lock}` file as its inputs have changed since the last upda
833830
# to be specified. This requirement means that if any data file changes then the update command will be
834831
# re-run. For cases where all data files cannot be specified a user can simply turn off auto-updates
835832
# by setting update_pnpm_lock to False and update their pnpm-lock.yaml file manually.
836-
working_directory = update_working_directory,
833+
working_directory = str(update_working_directory),
837834
quiet = rctx.attr.quiet,
838835
)
839836
if result.return_code:
@@ -863,15 +860,15 @@ STDERR:
863860

864861
lockfile_changed = False
865862
if state.set_input_hash(
866-
state.label_store.relative_path("pnpm_lock"),
867-
utils.hash(rctx.read(state.label_store.repository_path("pnpm_lock"))),
863+
pnpm_lock_relative_path,
864+
utils.hash(rctx.read(rctx.attr.pnpm_lock)),
868865
):
869866
# The lock file has changed
870867
if not rctx.attr.quiet:
871868
# buildifier: disable=print
872869
print("""
873-
INFO: {} file has changed""".format(pnpm_lock_relative_path))
874-
utils.reverse_force_copy(rctx, pnpm_lock_label)
870+
INFO: %s file has changed""" % pnpm_lock_relative_path)
871+
utils.reverse_force_copy(rctx, rctx.attr.pnpm_lock)
875872
lockfile_changed = True
876873

877874
state.write_action_cache()
@@ -893,7 +890,7 @@ ERROR: `{action_cache}` is out of date. `{pnpm_lock}` may require an update. To
893890
894891
""".format(
895892
action_cache = state.label_store.relative_path("action_cache"),
896-
pnpm_lock = state.label_store.relative_path("pnpm_lock"),
893+
pnpm_lock = str(rctx.path(rctx.attr.pnpm_lock)),
897894
repo_reference_symbol = repo_reference_symbol,
898895
rctx_name = rctx.name,
899896
))

npm/private/npm_translate_lock_generate.bzl

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

9595
# buildifier: disable=function-docstring
96-
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):
96+
def generate_repository_files(rctx, importers, packages, patched_dependencies, only_built_dependencies, root_package, default_registry, npm_registries, npm_auth, link_workspace):
9797
# empty line after bzl docstring since buildifier expects this if this file is vendored in
98-
generated_by_prefix = "\"\"\"@generated by npm_translate_lock(name = \"{}\", pnpm_lock = \"{}\")\"\"\"\n".format(helpers.to_apparent_repo_name(rctx.name), str(pnpm_lock_label))
98+
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))
9999

100100
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)
101101

@@ -248,7 +248,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
248248
defs_bzl_file = "@{}//:{}".format(rctx.name, rctx.attr.defs_bzl_filename),
249249
link_packages_comma_separated = "\"'\" + \"', '\".join(_LINK_PACKAGES) + \"'\"" if len(link_packages) else "\"\"",
250250
root_package = root_package,
251-
pnpm_lock_label = pnpm_lock_label,
251+
pnpm_lock_label = rctx.attr.pnpm_lock,
252252
),
253253
]
254254

@@ -480,21 +480,20 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
480480
rctx_files[rctx.attr.repositories_bzl_filename] = _generate_repositories(
481481
rctx,
482482
npm_imports,
483-
pnpm_lock_label,
484483
link_workspace,
485484
)
486485

487486
for filename, contents in rctx_files.items():
488487
rctx.file(filename, generated_by_prefix + "\n" + "\n".join(contents))
489488

490-
def _generate_repositories(rctx, npm_imports, pnpm_lock_label, link_workspace):
489+
def _generate_repositories(rctx, npm_imports, link_workspace):
491490
repositories_bzl = []
492491

493492
if len(npm_imports) > 0:
494493
repositories_bzl.append("""load("@aspect_rules_js//npm:repositories.bzl", "npm_import")""")
495494
repositories_bzl.append("")
496495

497-
repositories_bzl.append("# Generated npm_import repository rules corresponding to npm packages in {}".format(str(pnpm_lock_label)))
496+
repositories_bzl.append("# Generated npm_import repository rules corresponding to npm packages in {}".format(str(rctx.attr.pnpm_lock)))
498497
repositories_bzl.append("# buildifier: disable=function-docstring")
499498
repositories_bzl.append("def npm_repositories():")
500499
if len(npm_imports) == 0:

npm/private/npm_translate_lock_state.bzl

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

49-
_init_link_workspace(priv, rctx, attr, label_store)
49+
_init_link_workspace(priv, attr)
5050

5151
# parse the pnpm lock file incase since we need the importers list for additional init
5252
# TODO(windows): utils.exists is not yet support on Windows
@@ -134,7 +134,7 @@ def _init_pnpm_labels(priv, rctx, attr, label_store):
134134

135135
################################################################################
136136
def _init_update_labels(priv, _, attr, label_store):
137-
pnpm_lock_label = label_store.label("pnpm_lock")
137+
pnpm_lock_label = attr.pnpm_lock
138138
pnpm_lock_label_str = "//{}:{}".format(pnpm_lock_label.package, pnpm_lock_label.name)
139139
action_cache_path = paths.join(
140140
priv["external_repository_action_cache"],
@@ -170,7 +170,7 @@ def _init_patched_dependencies_labels(priv, _, attr, label_store):
170170
# Read patches from pnpm-lock.yaml `patchedDependencies`
171171
patches = []
172172
for patch_info in priv["patched_dependencies"].values():
173-
patches.append("//%s:%s" % (label_store.label("pnpm_lock").package, patch_info.get("path")))
173+
patches.append("//%s:%s" % (attr.pnpm_lock.package, patch_info.get("path")))
174174

175175
# Convert patch label strings to labels
176176
patches = [attr.pnpm_lock.relative(p) for p in patches]
@@ -186,9 +186,9 @@ def _init_importer_labels(priv, label_store):
186186
label_store.add_sibling("lock", "package_json_{}".format(i), paths.join(p, PACKAGE_JSON_FILENAME))
187187

188188
################################################################################
189-
def _init_link_workspace(priv, _, attr, label_store):
189+
def _init_link_workspace(priv, attr):
190190
# initialize link_workspace either from pnpm_lock label or from override
191-
priv["link_workspace"] = attr.link_workspace if attr.link_workspace else label_store.label("pnpm_lock").repo_name
191+
priv["link_workspace"] = attr.link_workspace if attr.link_workspace else attr.pnpm_lock.repo_name
192192

193193
################################################################################
194194
def _init_external_repository_action_cache(priv, attr):
@@ -197,7 +197,7 @@ def _init_external_repository_action_cache(priv, attr):
197197

198198
################################################################################
199199
def _init_root_package(priv, rctx, attr, label_store):
200-
pnpm_lock_label = label_store.label("pnpm_lock")
200+
pnpm_lock_label = attr.pnpm_lock
201201

202202
# use the directory of the pnpm_lock file as the root_package unless overridden by the root_package attribute
203203
if attr.root_package == DEFAULT_ROOT_PACKAGE:
@@ -284,7 +284,7 @@ def _copy_update_input_files(priv, rctx, attr, label_store):
284284
################################################################################
285285
# we can derive input files that should be specified but are not and copy these over; we warn the user when we do this
286286
def _copy_unspecified_input_files(priv, rctx, attr, label_store):
287-
pnpm_lock_label = label_store.label("pnpm_lock")
287+
pnpm_lock_label = attr.pnpm_lock
288288

289289
# pnpm-workspace.yaml
290290
pnpm_workspace_key = "pnpm_workspace"
@@ -480,7 +480,7 @@ WARNING: Cannot determine home directory in order to load home `.npmrc` file in
480480
_load_npmrc(priv, rctx, home_npmrc_path)
481481

482482
################################################################################
483-
def _load_lockfile(priv, rctx, _, label_store):
483+
def _load_lockfile(priv, rctx, attr, label_store):
484484
importers = {}
485485
packages = {}
486486
patched_dependencies = {}
@@ -489,7 +489,7 @@ def _load_lockfile(priv, rctx, _, label_store):
489489

490490
yq_args = [
491491
str(label_store.path("host_yq")),
492-
str(label_store.path("pnpm_lock")),
492+
attr.pnpm_lock,
493493
"-o=json",
494494
]
495495
result = rctx.execute(yq_args)

0 commit comments

Comments
 (0)