Skip to content

Commit

Permalink
fix(npm): use known version numbers when available for linked file: p…
Browse files Browse the repository at this point in the history
…ackages
  • Loading branch information
jbedard committed Dec 6, 2024
1 parent cde84d5 commit 1daae84
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
9 changes: 7 additions & 2 deletions e2e/pnpm_lockfiles/v54/snapshots/defs.bzl

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

9 changes: 7 additions & 2 deletions e2e/pnpm_lockfiles/v60/snapshots/defs.bzl

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

9 changes: 7 additions & 2 deletions e2e/pnpm_lockfiles/v61/snapshots/defs.bzl

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

4 changes: 2 additions & 2 deletions npm/private/npm_package_store.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("@aspect_bazel_lib//lib:copy_directory.bzl", "copy_directory_bin_action")
load("//js/private:js_info.bzl", "JsInfo", "js_info")
load(":npm_package_info.bzl", "NpmPackageInfo")
load(":npm_package_store_info.bzl", "NpmPackageStoreInfo")
load(":utils.bzl", "utils")
load(":utils.bzl", "DEFAULT_LINKED_VERSION", "utils")

_DOC = """Defines a npm package that is linked into a node_modules tree.
Expand Down Expand Up @@ -160,7 +160,7 @@ def _npm_package_store_impl(ctx):
msg = "Expected package to be specified in '{}' when src '{}' provides a JsInfo".format(ctx.label, ctx.attr.src[JsInfo].target)
fail(msg)
package = ctx.attr.package
version = ctx.attr.version if ctx.attr.version else "0.0.0"
version = ctx.attr.version if ctx.attr.version else DEFAULT_LINKED_VERSION
else:
msg = "Expected src of '{}' to provide either NpmPackageInfo or JsInfo".format(ctx.label)
fail(msg)
Expand Down
33 changes: 29 additions & 4 deletions npm/private/npm_translate_lock_generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
load("//npm/private:tar.bzl", "detect_system_tar")
load(":npm_translate_lock_helpers.bzl", "helpers")
load(":starlark_codegen_utils.bzl", "starlark_codegen_utils")
load(":utils.bzl", "utils")
load(":utils.bzl", "DEFAULT_LINKED_VERSION", "utils")

################################################################################
_NPM_IMPORT_TMPL = \
Expand Down Expand Up @@ -35,11 +35,17 @@ _FP_STORE_TMPL = \
name = "{package_store_root}/{{}}/{package_store_name}".format(name),
src = "{npm_package_target}",
package = "{package}",
version = "0.0.0",
version = "{version}",
deps = {deps},
visibility = ["//visibility:public"],
tags = ["manual"],
)"""
_FP_DEFAULT_VERSION_ALIAS_TMPL = \
"""
native.alias(
name = "{package_store_root}/{{}}/{default_version_name}".format(name),
actual = "{package_store_root}/{{}}/{package_store_name}".format(name),
)"""

_FP_DIRECT_TMPL = \
"""
Expand Down Expand Up @@ -144,11 +150,17 @@ sh_binary(
# collapse link aliases lists into to acomma separated strings
for dep_store_target in transitive_deps.keys():
transitive_deps[dep_store_target] = ",".join(transitive_deps[dep_store_target])

# Some file links may have a version specified
friendly_version = package_info.get("friendly_version", DEFAULT_LINKED_VERSION)
friendly_version = DEFAULT_LINKED_VERSION if friendly_version.startswith("file:") else friendly_version

fp_links[dep_key] = {
"package": name,
"path": dep_path,
"link_packages": {},
"deps": transitive_deps,
"version": friendly_version,
}

# Look for first-party links in importers
Expand Down Expand Up @@ -372,6 +384,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):

for fp_link in fp_links.values():
fp_package = fp_link.get("package")
fp_version = fp_link.get("version", DEFAULT_LINKED_VERSION)
fp_path = fp_link.get("path")
fp_link_packages = fp_link.get("link_packages").keys()
fp_deps = fp_link.get("deps")
Expand All @@ -384,10 +397,22 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
deps = starlark_codegen_utils.to_dict_attr(fp_deps, 3, quote_key = False),
npm_package_target = fp_target,
package = fp_package,
package_store_name = utils.package_store_name(fp_package, "0.0.0"),
version = fp_version,
package_store_name = utils.package_store_name(fp_package, fp_version),
package_store_root = utils.package_store_root,
))

# References to this package may be linked use file: or link: and not know
# the version number when the dependency is declared in the package.json.
if fp_version != DEFAULT_LINKED_VERSION:
npm_link_all_packages_bzl.append(_FP_DEFAULT_VERSION_ALIAS_TMPL.format(
package = fp_package,
version = fp_version,
default_version_name = utils.package_store_name(fp_package, DEFAULT_LINKED_VERSION),
package_store_name = utils.package_store_name(fp_package, fp_version),
package_store_root = utils.package_store_root,
))

package_visibility, _ = helpers.gather_values_from_matching_names(True, rctx.attr.package_visibility, "*", fp_package)
if len(package_visibility) == 0:
package_visibility = ["//visibility:public"]
Expand All @@ -399,7 +424,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
pkg = fp_package,
package_directory_output_group = utils.package_directory_output_group,
root_package = root_package,
package_store_name = utils.package_store_name(fp_package, "0.0.0"),
package_store_name = utils.package_store_name(fp_package, fp_version),
package_store_root = utils.package_store_root,
))

Expand Down
3 changes: 2 additions & 1 deletion npm/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DEFAULT_REGISTRY_DOMAIN = "registry.npmjs.org"
DEFAULT_REGISTRY_DOMAIN_SLASH = "{}/".format(DEFAULT_REGISTRY_DOMAIN)
DEFAULT_REGISTRY_PROTOCOL = "https"
DEFAULT_EXTERNAL_REPOSITORY_ACTION_CACHE = ".aspect/rules/external_repository_action_cache"
DEFAULT_LINKED_VERSION = "0.0.0"

def _sorted_map(m):
result = dict()
Expand Down Expand Up @@ -50,7 +51,7 @@ def _package_store_name(pnpm_name, pnpm_version):

if pnpm_version.startswith("link:") or pnpm_version.startswith("file:"):
name = pnpm_name
version = "0.0.0"
version = DEFAULT_LINKED_VERSION
elif pnpm_version.startswith("npm:"):
name, version = pnpm_version[4:].rsplit("@", 1)
else:
Expand Down

0 comments on commit 1daae84

Please sign in to comment.