Skip to content

Commit

Permalink
refactor: change npm_import(is_gnu_tar) to system_tar
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Jun 20, 2024
1 parent c802f21 commit 6cea32b
Show file tree
Hide file tree
Showing 7 changed files with 1,140 additions and 1,138 deletions.
200 changes: 100 additions & 100 deletions e2e/gyp_no_install_script/snapshots/wksp/repositories.bzl

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions e2e/npm_translate_lock/snapshots/wksp/repositories.bzl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions npm/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ load("//npm/private:npm_translate_lock_helpers.bzl", npm_translate_lock_helpers
load("//npm/private:npm_translate_lock_macro_helpers.bzl", macro_helpers = "helpers")
load("//npm/private:npm_translate_lock_state.bzl", "npm_translate_lock_state")
load("//npm/private:npmrc.bzl", "parse_npmrc")
load("//npm/private:tar.bzl", "check_is_gnu_tar")
load("//npm/private:tar.bzl", "get_system_tar")
load("//npm/private:transitive_closure.bzl", "translate_to_transitive_closure")

DEFAULT_PNPM_VERSION = _DEFAULT_PNPM_VERSION
Expand Down Expand Up @@ -134,7 +134,7 @@ WARNING: Cannot determine home directory in order to load home `.npmrc` file in
npm_auth = npm_auth,
)

is_gnu_tar = str(check_is_gnu_tar(module_ctx))
system_tar = get_system_tar(module_ctx)

for i in imports:
npm_import(
Expand Down Expand Up @@ -163,7 +163,7 @@ WARNING: Cannot determine home directory in order to load home `.npmrc` file in
replace_package = i.replace_package,
root_package = i.root_package,
transitive_closure = i.transitive_closure,
is_gnu_tar = is_gnu_tar,
system_tar = system_tar,
url = i.url,
version = i.version,
)
Expand Down
18 changes: 10 additions & 8 deletions npm/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ load(
_git_init = "init",
_git_reset = "reset",
)
load("//npm/private:tar.bzl", "check_is_gnu_tar")
load("//npm/private:tar.bzl", "get_system_tar")
load(":starlark_codegen_utils.bzl", "starlark_codegen_utils")
load(":utils.bzl", "utils")

Expand Down Expand Up @@ -451,8 +451,8 @@ def _download_and_extract_archive(rctx, package_json_only):
# so we use tar here which takes a --strip-components N argument instead of rctx.download_and_extract
tar_args = ["tar", "-xf", _TARBALL_FILENAME, "--strip-components", "1", "-C", _EXTRACT_TO_DIRNAME, "--no-same-owner", "--no-same-permissions"]

is_gnu_tar = rctx.attr.is_gnu_tar == "True" or (rctx.attr.is_gnu_tar == "" and check_is_gnu_tar(rctx))
if is_gnu_tar:
system_tar = get_system_tar(rctx) if rctx.attr.system_tar == "auto" else rctx.attr.system_tar
if system_tar == "gnu":
# Some packages have directory permissions missing the executable bit, which prevents GNU tar from
# extracting files into the directory. Delay permission restoration for directories until all files
# have been extracted.
Expand Down Expand Up @@ -812,9 +812,11 @@ _ATTRS = dicts.add(_COMMON_ATTRS, {
"patch_args": attr.string_list(),
"patches": attr.label_list(),
"url": attr.string(),
"is_gnu_tar": attr.string(
# is_gnu_tar can be precomputed for performance, or left blank if unknown
values = ["True", "False", ""],
"system_tar": attr.string(
# The system tar type can be precomputed for performance, or "auto" to
# determine at rule execution time.
values = ["gnu", "non-gnu", "auto"],
default = "auto",
),
})

Expand Down Expand Up @@ -1129,7 +1131,7 @@ def npm_import(

generate_bzl_library_targets = kwargs.pop("generate_bzl_library_targets", None)
extract_full_archive = kwargs.pop("extract_full_archive", None)
is_gnu_tar = str(kwargs.pop("is_gnu_tar", ""))
system_tar = kwargs.pop("system_tar", "auto")
if len(kwargs):
msg = "Invalid npm_import parameter '{}'".format(kwargs.keys()[0])
fail(msg)
Expand Down Expand Up @@ -1159,7 +1161,7 @@ def npm_import(
),
generate_bzl_library_targets = generate_bzl_library_targets,
extract_full_archive = extract_full_archive,
is_gnu_tar = is_gnu_tar,
system_tar = system_tar,
)

has_custom_postinstall = not (not custom_postinstall)
Expand Down
12 changes: 6 additions & 6 deletions npm/private/npm_translate_lock_generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//npm/private:tar.bzl", "check_is_gnu_tar")
load("//npm/private:tar.bzl", "get_system_tar")
load(":npm_translate_lock_helpers.bzl", "helpers")
load(":starlark_codegen_utils.bzl", "starlark_codegen_utils")
load(":utils.bzl", "utils")
Expand All @@ -17,7 +17,7 @@ _NPM_IMPORT_TMPL = \
package = "{package}",
version = "{version}",
url = "{url}",
is_gnu_tar = {is_gnu_tar},
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}
)
"""
Expand Down Expand Up @@ -478,14 +478,14 @@ def _generate_repositories(rctx, npm_imports, pnpm_lock_label, link_workspace):
repositories_bzl.append(" pass")
repositories_bzl.append("")

is_gnu_tar = check_is_gnu_tar(rctx)
system_tar = get_system_tar(rctx)

for _, _import in enumerate(npm_imports):
repositories_bzl.append(_gen_npm_import(rctx, is_gnu_tar, _import, link_workspace))
repositories_bzl.append(_gen_npm_import(rctx, system_tar, _import, link_workspace))

return repositories_bzl

def _gen_npm_import(rctx, is_gnu_tar, _import, link_workspace):
def _gen_npm_import(rctx, system_tar, _import, link_workspace):
maybe_integrity = ("""
integrity = "%s",""" % _import.integrity) if _import.integrity else ""
maybe_deps = ("""
Expand Down Expand Up @@ -551,7 +551,7 @@ def _gen_npm_import(rctx, is_gnu_tar, _import, link_workspace):
package = _import.package,
package_visibility = _import.package_visibility,
root_package = _import.root_package,
is_gnu_tar = is_gnu_tar,
system_tar = system_tar,
url = _import.url,
version = _import.version,
)
6 changes: 3 additions & 3 deletions npm/private/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load("@aspect_bazel_lib//lib:repo_utils.bzl", "repo_utils")

# TODO: use a hermetic tar from aspect_bazel_lib and remove this.
def check_is_gnu_tar(rctx):
def get_system_tar(rctx):
"""Check if the host tar command is GNU tar.
Args:
Expand All @@ -14,12 +14,12 @@ def check_is_gnu_tar(rctx):

# We assume that any linux platform is using GNU tar.
if repo_utils.is_linux(rctx):
return True
return "gnu"

tar_args = ["tar", "--version"]
result = rctx.execute(tar_args)
if result.return_code:
msg = "Failed to determine tar version. '{}' exited with {}: \nSTDOUT:\n{}\nSTDERR:\n{}".format(" ".join(tar_args), result.return_code, result.stdout, result.stderr)
fail(msg)

return "GNU tar" in result.stdout
return "gnu" if "GNU tar" in result.stdout else "non-gnu"
Loading

0 comments on commit 6cea32b

Please sign in to comment.