diff --git a/e2e/pnpm_lockfiles/lockfile-test.bzl b/e2e/pnpm_lockfiles/lockfile-test.bzl index cd741852af..7701daece4 100644 --- a/e2e/pnpm_lockfiles/lockfile-test.bzl +++ b/e2e/pnpm_lockfiles/lockfile-test.bzl @@ -21,6 +21,7 @@ BZL_FILES = { # resolved.json reference "is-odd_resolved.json": "@REPO_NAME//VERSION:is-odd/resolved.json", + "is-odd-v0_resolved.json": "@REPO_NAME//VERSION:is-odd-v0/resolved.json", # hasBin, optional deps, deps "rollup_links_defs.bzl": "@REPO_NAME__rollup__2.14.0__links//:defs.bzl", diff --git a/e2e/pnpm_lockfiles/v54/snapshots/is-odd-v0_resolved.json b/e2e/pnpm_lockfiles/v54/snapshots/is-odd-v0_resolved.json new file mode 100644 index 0000000000..d39e89cb3c --- /dev/null +++ b/e2e/pnpm_lockfiles/v54/snapshots/is-odd-v0_resolved.json @@ -0,0 +1 @@ +{"$schema":"https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock","integrity":"sha512-3RKAybrJxq3zCUC+TJ5Ao0sBsbacAT3OBeNVcCbsQsHUC70qWK2R4JsIvax4OTjeGWnB8FumAWATUtS1jd+KYw==","version":"0.1.0"} \ No newline at end of file diff --git a/e2e/pnpm_lockfiles/v60/snapshots/is-odd-v0_resolved.json b/e2e/pnpm_lockfiles/v60/snapshots/is-odd-v0_resolved.json new file mode 100644 index 0000000000..d39e89cb3c --- /dev/null +++ b/e2e/pnpm_lockfiles/v60/snapshots/is-odd-v0_resolved.json @@ -0,0 +1 @@ +{"$schema":"https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock","integrity":"sha512-3RKAybrJxq3zCUC+TJ5Ao0sBsbacAT3OBeNVcCbsQsHUC70qWK2R4JsIvax4OTjeGWnB8FumAWATUtS1jd+KYw==","version":"0.1.0"} \ No newline at end of file diff --git a/e2e/pnpm_lockfiles/v61/snapshots/is-odd-v0_resolved.json b/e2e/pnpm_lockfiles/v61/snapshots/is-odd-v0_resolved.json new file mode 100644 index 0000000000..d39e89cb3c --- /dev/null +++ b/e2e/pnpm_lockfiles/v61/snapshots/is-odd-v0_resolved.json @@ -0,0 +1 @@ +{"$schema":"https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock","integrity":"sha512-3RKAybrJxq3zCUC+TJ5Ao0sBsbacAT3OBeNVcCbsQsHUC70qWK2R4JsIvax4OTjeGWnB8FumAWATUtS1jd+KYw==","version":"0.1.0"} \ No newline at end of file diff --git a/e2e/pnpm_lockfiles/v90/snapshots/is-odd-v0_resolved.json b/e2e/pnpm_lockfiles/v90/snapshots/is-odd-v0_resolved.json new file mode 100644 index 0000000000..d39e89cb3c --- /dev/null +++ b/e2e/pnpm_lockfiles/v90/snapshots/is-odd-v0_resolved.json @@ -0,0 +1 @@ +{"$schema":"https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock","integrity":"sha512-3RKAybrJxq3zCUC+TJ5Ao0sBsbacAT3OBeNVcCbsQsHUC70qWK2R4JsIvax4OTjeGWnB8FumAWATUtS1jd+KYw==","version":"0.1.0"} \ No newline at end of file diff --git a/npm/private/npm_translate_lock_generate.bzl b/npm/private/npm_translate_lock_generate.bzl index 29f7684609..397bd4620a 100644 --- a/npm/private/npm_translate_lock_generate.bzl +++ b/npm/private/npm_translate_lock_generate.bzl @@ -265,15 +265,27 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): )) for link_package, _link_aliases in _import.link_packages.items(): link_aliases = _link_aliases or [_import.package] + + # the build file for the package being linked + build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel" + if build_file not in rctx_files: + rctx_files[build_file] = [] + + # the bzl files for the package being linked + if link_package not in links_bzl: + links_bzl[link_package] = [] + if link_package not in links_targets_bzl: + links_targets_bzl[link_package] = [] + + # for each alias of this package for link_alias in link_aliases: - if link_package not in links_bzl: - links_bzl[link_package] = [] - if link_package not in links_targets_bzl: - links_targets_bzl[link_package] = [] + # link the alias to the underlying package links_bzl[link_package].append(""" link_{i}(name = "{{}}/{name}".format(name))""".format( i = i, name = link_alias, )) + + # expose the alias if public if "//visibility:public" in _import.package_visibility: add_to_link_targets = """ link_targets.append("//{{}}:{{}}/{name}".format(bazel_package, name))""".format(name = link_alias) links_bzl[link_package].append(add_to_link_targets) @@ -282,19 +294,20 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): package_scope = link_alias.split("/", 1)[0] add_to_scoped_targets = """ scope_targets["{package_scope}"] = scope_targets["{package_scope}"] + [link_targets[-1]] if "{package_scope}" in scope_targets else [link_targets[-1]]""".format(package_scope = package_scope) links_bzl[link_package].append(add_to_scoped_targets) - for link_package in _import.link_packages.keys(): - build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel" - if build_file not in rctx_files: - rctx_files[build_file] = [] - resolved_json_rel_path = "{}/{}".format(_import.package, _RESOLVED_JSON_FILENAME) if _import.package else _RESOLVED_JSON_FILENAME - resolved_json_file_path = "{}/{}".format(link_package, resolved_json_rel_path) if link_package else resolved_json_rel_path - rctx.file(resolved_json_file_path, json.encode({ - # Allow consumers to auto-detect this filetype - "$schema": "https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock", - "version": _import.version, - "integrity": _import.integrity, - })) - rctx_files[build_file].append("exports_files([\"{}\"])".format(resolved_json_rel_path)) + + # the resolved.json for this alias of the package + resolved_json_rel_path = "{}/{}".format(link_alias, _RESOLVED_JSON_FILENAME) + resolved_json_file_path = "{}/{}".format(link_package, resolved_json_rel_path) if link_package else resolved_json_rel_path + + rctx.file(resolved_json_file_path, json.encode({ + # Allow consumers to auto-detect this filetype + "$schema": "https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock", + "version": _import.version, + "integrity": _import.integrity, + })) + rctx_files[build_file].append("exports_files([\"{}\"])".format(resolved_json_rel_path)) + + # the package_json.bzl for this package if _import.package_info.get("has_bin"): if rctx.attr.generate_bzl_library_targets: rctx_files[build_file].append("""load("@bazel_skylib//:bzl_library.bzl", "bzl_library")""")