Skip to content

Commit 550d591

Browse files
authored
fix(pnpm): Parse integrity hash from package.json packageManager if provided (#2596)
When specifying `pnpm_version` and `pnpm_version_integrity` directly in MODULE.bazel, both version and integrity were used correctly. However, when using `pnpm_version_from` to read from package.json's `packageManager` field, the integrity hash was being discarded. The `packageManager` field is the standard way to pin package manager versions in the Node.js ecosystem. This fix ensures that projects using the standard `packageManager` format can use the same package.json with both local Node.js tooling and Bazel tooling. Changes: - Modified the `packageManager` parsing logic to extract and store the integrity hash when present. The hash format is converted from +sha512.xxx (npm/corepack format) to sha512-xxx - Updated `from_package_json_with_hash_test` to verify hash extraction, as `from_package_json_simple_test` covers the case without it
1 parent 1481d42 commit 550d591

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

npm/private/pnpm_extension.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ def resolve_pnpm_repositories(mctx):
5656
if not package_manager.startswith("pnpm@"):
5757
fail("packageManager field must specify pnpm, got: " + package_manager)
5858

59-
# Extract version from "[email protected]" format
59+
# Extract version and optional integrity from "[email protected]+sha512.<hash>" format
6060
v = package_manager[5:] # Remove "pnpm@" prefix
61-
v = v.rsplit("+sha512.")[0] # Remove optional "+sha512.<hash>" suffix
61+
if "+sha512." in v:
62+
parts = v.rsplit("+sha512.", 1)
63+
v = parts[0]
64+
65+
# Store the integrity hash (prepend "sha512-" as that's the expected format)
66+
integrity[v] = "sha512-" + parts[1]
6267

6368
elif attr.pnpm_version == "latest":
6469
v = LATEST_PNPM_VERSION

npm/private/test/pnpm_test.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def _basic(ctx):
4848
)
4949

5050
def _from_package_json_simple(ctx):
51+
# Test reading pnpm version from package.json without integrity hash.
52+
# packageManager: "[email protected]" -> version only, no integrity tuple
5153
return _resolve_test(
5254
ctx,
5355
repositories = {"pnpm": "1.2.3"},
@@ -58,9 +60,11 @@ def _from_package_json_simple(ctx):
5860
)
5961

6062
def _from_package_json_with_hash(ctx):
63+
# Test reading pnpm version from package.json with integrity hash.
64+
# packageManager: "[email protected]+sha512.xxx" -> (version, integrity) tuple
6165
return _resolve_test(
6266
ctx,
63-
repositories = {"pnpm": "1.2.3"},
67+
repositories = {"pnpm": ("1.2.3", "sha512-97462997561378b6f52ac5c614f3a3b923a652ad5ac987100286e4aa2d84a6a0642e9e45f3d01d30c46b12b20beb0f86aeb790bf9a82bc59db42b67fe69d1a25")},
6468
modules = [
6569
_fake_mod(True, _fake_pnpm_tag(pnpm_version_from = "//:package.json")),
6670
],

0 commit comments

Comments
 (0)