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

Add patch_tool argument to npm_translate_lock #2048

Merged
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
5 changes: 3 additions & 2 deletions docs/npm_import.md

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

3 changes: 2 additions & 1 deletion docs/npm_translate_lock.md

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

2 changes: 2 additions & 0 deletions npm/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ WARNING: Cannot determine home directory in order to load home `.npmrc` file in
npm_auth_username = i.npm_auth_username,
package = i.package,
package_visibility = i.package_visibility,
patch_tool = i.patch_tool,
patch_args = i.patch_args,
patches = i.patches,
replace_package = i.replace_package,
Expand Down Expand Up @@ -188,6 +189,7 @@ def _npm_import_bzlmod(i):
npm_auth_password = i.npm_auth_password,
package = i.package,
package_visibility = i.package_visibility,
patch_tool = i.patch_tool,
patch_args = i.patch_args,
patches = i.patches,
replace_package = i.replace_package,
Expand Down
12 changes: 11 additions & 1 deletion npm/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,12 @@ def _npm_import_rule_impl(rctx):

# apply patches to the extracted package before reading the package.json incase
# the patch targets the package.json itself
patch(rctx, patch_args = rctx.attr.patch_args, patch_directory = _EXTRACT_TO_DIRNAME)
patch(
rctx,
patch_tool = rctx.path(rctx.attr.patch_tool) if rctx.attr.patch_tool else "patch",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider ALWAYS using a hermetic patch implementation. I recall we looked into the one Bazel itself uses, but it wasn't exposed for repository rules to use? Maybe @gregmagolan remembers. Can be in a later PR though.

patch_args = rctx.attr.patch_args,
patch_directory = _EXTRACT_TO_DIRNAME,
)

pkg_json = json.decode(rctx.read(_EXTRACT_TO_PACKAGE_JSON))

Expand Down Expand Up @@ -822,6 +827,7 @@ _ATTRS = dicts.add(_COMMON_ATTRS, {
"npm_auth_basic": attr.string(),
"npm_auth_password": attr.string(),
"npm_auth_username": attr.string(),
"patch_tool": attr.label(),
"patch_args": attr.string_list(),
"patches": attr.label_list(),
"url": attr.string(),
Expand Down Expand Up @@ -886,6 +892,7 @@ def npm_import(
commit = "",
replace_package = None,
package_visibility = ["//visibility:public"],
patch_tool = None,
patch_args = ["-p0"],
patches = [],
custom_postinstall = "",
Expand Down Expand Up @@ -1095,6 +1102,8 @@ def npm_import(

package_visibility: Visibility of generated node_module link targets.

patch_tool: The patch tool to use. If not specified, the `patch` from `PATH` is used.

patch_args: Arguments to pass to the patch tool.

`-p1` will usually be needed for patches generated by git.
Expand Down Expand Up @@ -1161,6 +1170,7 @@ def npm_import(
integrity = integrity,
url = url,
commit = commit,
patch_tool = patch_tool,
patch_args = patch_args,
patches = patches,
custom_postinstall = custom_postinstall,
Expand Down
5 changes: 5 additions & 0 deletions npm/private/npm_translate_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ _ATTRS = {
"npm_package_target_name": attr.string(),
"npmrc": attr.label(),
"package_visibility": attr.string_list_dict(),
"patch_tool": attr.label(),
"patch_args": attr.string_list_dict(),
"patches": attr.string_list_dict(),
"use_pnpm": attr.label(default = "@pnpm//:package/bin/pnpm.cjs"), # bzlmod pnpm extension
Expand Down Expand Up @@ -161,6 +162,7 @@ def npm_translate_lock(
use_home_npmrc = None,
data = [],
patches = {},
patch_tool = None,
patch_args = {"*": ["-p0"]},
custom_postinstalls = {},
package_visibility = {},
Expand Down Expand Up @@ -277,6 +279,8 @@ def npm_translate_lock(

Read more: [patching](/docs/pnpm.md#patching)

patch_tool: The patch tool to use. If not specified, the `patch` from `PATH` is used.

patch_args: A map of package names or package names with their version (e.g., "my-package" or "[email protected]")
to a label list arguments to pass to the patch tool. The most specific match wins.

Expand Down Expand Up @@ -569,6 +573,7 @@ def npm_translate_lock(
npmrc = npmrc,
use_home_npmrc = use_home_npmrc,
patches = patches,
patch_tool = patch_tool,
patch_args = patch_args,
custom_postinstalls = custom_postinstalls,
package_visibility = package_visibility,
Expand Down
5 changes: 4 additions & 1 deletion npm/private/npm_translate_lock_generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _NPM_IMPORT_TMPL = \
version = "{version}",
url = "{url}",
system_tar = "{system_tar}",
package_visibility = {package_visibility},{maybe_dev}{maybe_commit}{maybe_generate_bzl_library_targets}{maybe_integrity}{maybe_deps}{maybe_transitive_closure}{maybe_patches}{maybe_patch_args}{maybe_lifecycle_hooks}{maybe_custom_postinstall}{maybe_lifecycle_hooks_env}{maybe_lifecycle_hooks_execution_requirements}{maybe_bins}{maybe_npm_auth}{maybe_npm_auth_basic}{maybe_npm_auth_username}{maybe_npm_auth_password}{maybe_replace_package}{maybe_lifecycle_hooks_use_default_shell_env}
package_visibility = {package_visibility},{maybe_dev}{maybe_commit}{maybe_generate_bzl_library_targets}{maybe_integrity}{maybe_deps}{maybe_transitive_closure}{maybe_patches}{maybe_patch_tool}{maybe_patch_args}{maybe_lifecycle_hooks}{maybe_custom_postinstall}{maybe_lifecycle_hooks_env}{maybe_lifecycle_hooks_execution_requirements}{maybe_bins}{maybe_npm_auth}{maybe_npm_auth_basic}{maybe_npm_auth_username}{maybe_npm_auth_password}{maybe_replace_package}{maybe_lifecycle_hooks_use_default_shell_env}
)
"""

Expand Down Expand Up @@ -511,6 +511,8 @@ def _gen_npm_import(rctx, system_tar, _import, link_workspace):
deps = %s,""" % starlark_codegen_utils.to_dict_attr(_import.deps, 2)) if len(_import.deps) > 0 else ""
maybe_transitive_closure = ("""
transitive_closure = %s,""" % starlark_codegen_utils.to_dict_list_attr(_import.transitive_closure, 2)) if len(_import.transitive_closure) > 0 else ""
maybe_patch_tool = ("""
patch_tool = "%s",""" % _import.patch_tool) if _import.patch_tool else ""
maybe_patches = ("""
patches = %s,""" % _import.patches) if len(_import.patches) > 0 else ""
maybe_patch_args = ("""
Expand Down Expand Up @@ -562,6 +564,7 @@ def _gen_npm_import(rctx, system_tar, _import, link_workspace):
maybe_npm_auth_basic = maybe_npm_auth_basic,
maybe_npm_auth_password = maybe_npm_auth_password,
maybe_npm_auth_username = maybe_npm_auth_username,
maybe_patch_tool = maybe_patch_tool,
maybe_patch_args = maybe_patch_args,
maybe_patches = maybe_patches,
maybe_replace_package = maybe_replace_package,
Expand Down
1 change: 1 addition & 0 deletions npm/private/npm_translate_lock_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ ERROR: can not apply both `pnpm.patchedDependencies` and `npm_translate_lock(pat
name = repo_name,
package = name,
package_visibility = package_visibility,
patch_tool = attr.patch_tool,
patch_args = patch_args,
patches = patches,
root_package = root_package,
Expand Down