Skip to content

Commit

Permalink
fix: do not support mixing pnpm.patchedDependencies and npm_translate…
Browse files Browse the repository at this point in the history
…_lock(patches) (#1694)

Fix #1427
Close #1693
Close #837
  • Loading branch information
jbedard authored May 9, 2024
1 parent ccc9fe3 commit b66dad1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 63 deletions.
6 changes: 0 additions & 6 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,6 @@ npm.npm_translate_lock(
"unused": ["//visibility:private"],
"@mycorp/pkg-a": ["//examples:__subpackages__"],
},
patch_args = {
"*": ["-p1"],
},
patches = {
"[email protected]": ["//examples/npm_deps:patches/[email protected]_pnpm.patch"],
},
pnpm_lock = "//:pnpm-lock.yaml",
public_hoist_packages = {
# Instructs the linker to hoist the [email protected] npm package to `node_modules/ms` in the `examples/npm_deps` package.
Expand Down
6 changes: 0 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ npm_translate_lock(
"unused": ["//visibility:private"],
"@mycorp/pkg-a": ["//examples:__subpackages__"],
},
patch_args = {
"*": ["-p1"],
},
patches = {
"[email protected]": ["//examples/npm_deps:patches/[email protected]_pnpm.patch"],
},
pnpm_lock = "//:pnpm-lock.yaml",
# Use a version that's not vendored into rules_js by providing a (version, integrity) tuple.
# curl --silent https://registry.npmjs.org/pnpm | jq '.versions["8.6.11"].dist.integrity'
Expand Down
17 changes: 5 additions & 12 deletions examples/npm_deps/patched-dependencies-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const meaningOfLife = require('meaning-of-life')

// meaning-of-life should have been patched twice:
//
// First, by the `pnpm.patchedDependencies` patch:
// Verify meaning-of-life was patched with
// examples/npm_deps/patches/[email protected]
// 42 => "forty two"
//
// Then by the the following patch in the `patches` attr:
// examples/npm_deps/patches/[email protected]_pnpm.patch
// "forty two" => 32

if (meaningOfLife === 42) {
throw new Error('Patches were not applied!')
} else if (meaningOfLife === 'forty two') {
throw new Error('Only `pnpm.patchedDependencies` patch was applied!')
} else if (meaningOfLife !== 32) {
throw new Error('Patch in `patches` was not applied!')
}

if (meaningOfLife !== 'forty two') {
throw new Error('`pnpm.patchedDependencies` was not applied!')
}
7 changes: 0 additions & 7 deletions examples/npm_deps/patches/[email protected]_pnpm.patch

This file was deleted.

1 change: 0 additions & 1 deletion examples/npm_deps/patches/patches
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
examples/npm_deps/patches/[email protected]_pnpm.patch
examples/npm_deps/patches/[email protected]
50 changes: 20 additions & 30 deletions npm/private/npm_translate_lock_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,30 @@ def _get_npm_imports(importers, packages, patched_dependencies, root_package, rc
unfriendly_name = None

# gather patches & patch args
patches, patches_keys = _gather_values_from_matching_names(True, attr.patches, name, friendly_name, unfriendly_name)
patches = []
patch_args = []

translate_patches, patches_keys = _gather_values_from_matching_names(True, attr.patches, name, friendly_name, unfriendly_name)

if len(translate_patches) > 0 and pnpm_patched:
msg = """\
ERROR: can not apply both `pnpm.patchedDependencies` and `npm_translate_lock(patches)` to the same package {pkg}.
""".format(
pkg = name,
)
fail(msg)

# Apply patch from `pnpm.patchedDependencies` first
if pnpm_patched:
patch_path = "//%s:%s" % (attr.pnpm_lock.package, patched_dependencies.get(friendly_name).get("path"))
patches.insert(0, patch_path)
patches.append(patch_path)

# pnpm patches are always applied with -p1
patch_args = ["-p1"]
elif len(translate_patches) > 0:
patches = translate_patches
patch_args, _ = _gather_values_from_matching_names(False, attr.patch_args, "*", name, friendly_name, unfriendly_name)
patches_used.extend(patches_keys)

# Resolve string patch labels relative to the root respository rather than relative to rules_js.
# https://docs.google.com/document/d/1N81qfCa8oskCk5LqTW-LNthy6EBrDot7bdUsjz6JFC4/
Expand All @@ -341,25 +359,6 @@ def _get_npm_imports(importers, packages, patched_dependencies, root_package, rc
# that checked in repositories.bzl files don't fail diff tests when run under multiple versions of Bazel.
patches = [("@" if patch.startswith("//") else "") + patch for patch in patches]

patch_args, _ = _gather_values_from_matching_names(False, attr.patch_args, "*", name, friendly_name, unfriendly_name)

# Patches in `pnpm.patchedDependencies` must have the -p1 format. Therefore any
# patches applied via `patches` must also use -p1 since we don't support different
# patch args for different patches.
if pnpm_patched and not _has_strip_prefix_arg(patch_args, 1):
if _has_strip_prefix_arg(patch_args):
msg = """\
ERROR: patch_args for package {package} contains a strip prefix that is incompatible with a patch applied via `pnpm.patchedDependencies`.
`pnpm.patchedDependencies` requires a strip prefix of `-p1`. All applied patches must use the same strip prefix.
""".format(package = friendly_name)
fail(msg)
patch_args = patch_args[:]
patch_args.append("-p1")

patches_used.extend(patches_keys)

# gather replace packages
replace_packages, _ = _gather_values_from_matching_names(True, attr.replace_packages, name, friendly_name, unfriendly_name)
if len(replace_packages) > 1:
Expand Down Expand Up @@ -500,15 +499,6 @@ def _link_package(root_package, import_path, rel_path = "."):
def _is_url(url):
return url.find("://") != -1

################################################################################
def _has_strip_prefix_arg(patch_args, strip_num = None):
if strip_num != None:
return "-p%d" % strip_num in patch_args or "--strip=%d" % strip_num in patch_args
for arg in patch_args:
if arg.startswith("-p") or arg.startswith("--strip="):
return True
return False

################################################################################
def _to_apparent_repo_name(canonical_name):
return canonical_name[canonical_name.rfind("~") + 1:]
Expand Down
2 changes: 1 addition & 1 deletion npm/private/test/snapshots/wksp/repositories.bzl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b66dad1

Please sign in to comment.