Skip to content

Commit

Permalink
refactor: always read pnpm.patchedDependencies from lockfile (#1700)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored and gregmagolan committed May 13, 2024
1 parent d82cf3d commit 7736430
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions npm/private/npm_translate_lock_state.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ WARNING: `update_pnpm_lock` attribute in `npm_translate_lock(name = "{rctx_name}

_init_root_package(priv, rctx, label_store)

root_package_json_declared = label_store.has("package_json_root")
if root_package_json_declared and _should_update_pnpm_lock(priv):
# If we need to read the patches list from the package.json since
# update_pnpm_lock is enabled and the user has declared the root package.json
# as an input file then we can read it right away.
_init_patched_dependencies_labels(priv, rctx, label_store)
priv["patched_dependencies_labels_initialized"] = True

if _should_update_pnpm_lock(priv) or not rctx.attr.pnpm_lock:
# labels only needed when updating or bootstrapping the pnpm lock file
_init_pnpm_labels(label_store, rctx)
Expand All @@ -62,12 +54,6 @@ WARNING: `update_pnpm_lock` attribute in `npm_translate_lock(name = "{rctx_name}
pnpm_lock_exists = is_windows or utils.exists(rctx, label_store.path("pnpm_lock"))
if pnpm_lock_exists:
_load_lockfile(priv, rctx, label_store)

if not priv["patched_dependencies_labels_initialized"]:
# If we didn't initialize the patched dependency labels above then try again now. We'll either
# read the list of patches from the lock file now that it has been read or, if update_pnpm_lock
# is enabled, we can derive the existance of a root package.json by looking for a `.` importer
# in the `importers` list.
_init_patched_dependencies_labels(priv, rctx, label_store)

if _should_update_pnpm_lock(priv):
Expand Down Expand Up @@ -180,19 +166,13 @@ def _init_patches_labels(priv, rctx, label_store):
label_store.add("patches_{}".format(i), d)

priv["num_patches"] = len(patches)
priv["patched_dependencies_labels_initialized"] = False

################################################################################
def _init_patched_dependencies_labels(priv, rctx, label_store):
if rctx.attr.update_pnpm_lock:
# Read patches from package.json `pnpm.patchedDependencies`
root_package_json = _root_package_json(priv)
patches = ["//%s:%s" % (label_store.label("pnpm_lock").package, patch) for patch in root_package_json.get("pnpm", {}).get("patchedDependencies", {}).values()]
else:
# Read patches from pnpm-lock.yaml `patchedDependencies`
patches = []
for patch_info in priv["patched_dependencies"].values():
patches.append("//%s:%s" % (label_store.label("pnpm_lock").package, patch_info.get("path")))
# Read patches from pnpm-lock.yaml `patchedDependencies`
patches = []
for patch_info in priv["patched_dependencies"].values():
patches.append("//%s:%s" % (label_store.label("pnpm_lock").package, patch_info.get("path")))

# Convert patch label strings to labels
patches = [rctx.attr.pnpm_lock.relative(p) for p in patches]
Expand Down Expand Up @@ -351,15 +331,10 @@ WARNING: Implicitly using package.json file `{package_json}` since the `{pnpm_lo
fail(msg)
_copy_input_file(priv, rctx, label_store, package_json_key)

if rctx.attr.update_pnpm_lock:
# Read patches from package.json `pnpm.patchedDependencies`
root_package_json = _root_package_json(priv)
pnpm_patches = root_package_json.get("pnpm", {}).get("patchedDependencies", {}).values()
else:
# Read patches from pnpm-lock.yaml `patchedDependencies`
pnpm_patches = []
for patch_info in priv["patched_dependencies"].values():
pnpm_patches.append(patch_info.get("path"))
# Read patches from pnpm-lock.yaml `patchedDependencies`
pnpm_patches = []
for patch_info in priv["patched_dependencies"].values():
pnpm_patches.append(patch_info.get("path"))

num_patches = priv["num_patches"]

Expand Down Expand Up @@ -572,9 +547,6 @@ def _npm_auth(priv):
def _root_package(priv):
return priv["root_package"]

def _root_package_json(priv):
return priv["root_package_json"]

################################################################################
def _new(rctx):
label_store = repository_label_store.new(rctx.path)
Expand Down

0 comments on commit 7736430

Please sign in to comment.