Skip to content

Commit 8708de9

Browse files
authored
refactor: only generate package_json.bzl once per package instead of per link (#2433)
### Changes are visible to end-users: no ### Test plan - Covered by existing test cases
1 parent 23a7fba commit 8708de9

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

npm/private/npm_import.bzl

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,6 @@ def _npm_import_rule_impl(rctx):
641641
patch_directory = _EXTRACT_TO_DIRNAME,
642642
)
643643

644-
pkg_json = json.decode(rctx.read(_EXTRACT_TO_PACKAGE_JSON))
645-
646-
bins = _get_bin_entries(pkg_json, rctx.attr.package)
647-
648644
generated_by_prefix = _make_generated_by_prefix(rctx.attr.package, rctx.attr.version)
649645

650646
rctx_files = {
@@ -663,11 +659,17 @@ def _npm_import_rule_impl(rctx):
663659
if rctx.attr.extra_build_content:
664660
rctx_files["BUILD.bazel"].append("\n" + rctx.attr.extra_build_content)
665661

666-
if bins:
667-
package_store_name = utils.package_store_name(rctx.attr.package, rctx.attr.version)
668-
package_name_no_scope = rctx.attr.package.rsplit("/", 1)[-1]
662+
# If this package has binaries and is linked into any other packages:
663+
# - generate the bin bzl and build files
664+
# - write ^ into each package linking to this package.
665+
if rctx.attr.link_packages:
666+
pkg_json = json.decode(rctx.read(_EXTRACT_TO_PACKAGE_JSON))
667+
bins = _get_bin_entries(pkg_json, rctx.attr.package)
668+
669+
if bins:
670+
package_store_name = utils.package_store_name(rctx.attr.package, rctx.attr.version)
671+
package_name_no_scope = rctx.attr.package.rsplit("/", 1)[-1]
669672

670-
for link_package in rctx.attr.link_packages.keys():
671673
bin_bzl = [
672674
generated_by_prefix,
673675
"""load("@aspect_rules_js//npm/private:npm_import.bzl", "bin_binary_internal", "bin_internal", "bin_test_internal")""",
@@ -711,18 +713,20 @@ bin = bin_factory("node_modules")
711713
bin_struct_fields = "\n".join(bin_struct_fields),
712714
))
713715

714-
rctx_files["{}/{}".format(link_package, _PACKAGE_JSON_BZL_FILENAME) if link_package else _PACKAGE_JSON_BZL_FILENAME] = bin_bzl
715-
716-
build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel"
717-
if build_file not in rctx_files:
718-
rctx_files[build_file] = [generated_by_prefix]
719-
if rctx.attr.generate_bzl_library_targets:
720-
rctx_files[build_file].append("""load("@bazel_skylib//:bzl_library.bzl", "bzl_library")""")
721-
rctx_files[build_file].append(_BZL_LIBRARY_TMPL.format(
722-
name = link_package[link_package.rfind("/") + 1] if link_package else package_name_no_scope,
723-
src = _PACKAGE_JSON_BZL_FILENAME,
724-
))
725-
rctx_files[build_file].append("""exports_files(["{}", "{}"])""".format(_PACKAGE_JSON_BZL_FILENAME, package_src))
716+
# Duplicate the bzl+BUILD into each linking package
717+
for link_package in rctx.attr.link_packages.keys():
718+
rctx_files["{}/{}".format(link_package, _PACKAGE_JSON_BZL_FILENAME) if link_package else _PACKAGE_JSON_BZL_FILENAME] = bin_bzl
719+
720+
build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel"
721+
if build_file not in rctx_files:
722+
rctx_files[build_file] = [generated_by_prefix]
723+
if rctx.attr.generate_bzl_library_targets:
724+
rctx_files[build_file].append("""load("@bazel_skylib//:bzl_library.bzl", "bzl_library")""")
725+
rctx_files[build_file].append(_BZL_LIBRARY_TMPL.format(
726+
name = link_package[link_package.rfind("/") + 1] if link_package else package_name_no_scope,
727+
src = _PACKAGE_JSON_BZL_FILENAME,
728+
))
729+
rctx_files[build_file].append("""exports_files(["{}", "{}"])""".format(_PACKAGE_JSON_BZL_FILENAME, package_src))
726730

727731
rules_js_metadata = {}
728732
if rctx.attr.lifecycle_hooks:

0 commit comments

Comments
 (0)