Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not support mixing pnpm.patchedDependencies and npm_translate_lock(patches) #1694

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {
"meaning-of-life@1.0.0": ["//examples/npm_deps:patches/meaning-of-life@1.0.0-after_pnpm.patch"],
},
pnpm_lock = "//:pnpm-lock.yaml",
public_hoist_packages = {
# Instructs the linker to hoist the ms@2.1.3 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 = {
"meaning-of-life@1.0.0": ["//examples/npm_deps:patches/meaning-of-life@1.0.0-after_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/meaning-of-life@1.0.0-pnpm.patch
// 42 => "forty two"
//
// Then by the the following patch in the `patches` attr:
// examples/npm_deps/patches/meaning-of-life@1.0.0-after_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!')
}

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/meaning-of-life@1.0.0-after_pnpm.patch
examples/npm_deps/patches/meaning-of-life@1.0.0-pnpm.patch
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"]
jbedard marked this conversation as resolved.
Show resolved Hide resolved
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.

Loading