Skip to content
Open
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
35 changes: 16 additions & 19 deletions npm/private/npm_translate_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Advanced users may want to directly fetch a package from npm rather than start f

load("@aspect_bazel_lib//lib:utils.bzl", bazel_lib_utils = "utils")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":exclude_package_contents_default.bzl", "exclude_package_contents_default")
load(":list_sources.bzl", "list_sources")
load(":npm_translate_lock_generate.bzl", "generate_repository_files")
Expand Down Expand Up @@ -139,7 +138,7 @@ def _npm_translate_lock_impl(rctx):
INFO: {} file updated. Please run your build again.

See https://github.com/aspect-build/rules_js/issues/1445
""".format(state.label_store.relative_path("pnpm_lock"))
""".format(rctx.path(rctx.attr.pnpm_lock))
fail(msg)

helpers.verify_node_modules_ignored(rctx, state.importers(), state.root_package())
Expand All @@ -148,7 +147,7 @@ See https://github.com/aspect-build/rules_js/issues/1445

helpers.verify_lifecycle_hooks_specified(rctx, state)

rctx.report_progress("Translating {}".format(state.label_store.relative_path("pnpm_lock")))
rctx.report_progress("Translating %s" % str(rctx.path(rctx.attr.pnpm_lock)))

importers, packages = translate_to_transitive_closure(
state.importers(),
Expand All @@ -162,7 +161,6 @@ See https://github.com/aspect-build/rules_js/issues/1445

generate_repository_files(
rctx,
state.label_store.label("pnpm_lock"),
importers,
packages,
state.patched_dependencies(),
Expand Down Expand Up @@ -693,12 +691,12 @@ def list_patches(name, out = None, include_patterns = ["*.diff", "*.patch"], exc

################################################################################
def _bootstrap_import(rctx, state):
pnpm_lock_label = state.label_store.label("pnpm_lock")
pnpm_lock_path = state.label_store.path("pnpm_lock")
pnpm_lock_label = rctx.attr.pnpm_lock
pnpm_lock_path = rctx.path(pnpm_lock_label)

# Check if the pnpm lock file already exists and copy it over if it does.
# When we do this, warn the user that we do.
if utils.exists(rctx, pnpm_lock_path):
if pnpm_lock_path.exists:
# buildifier: disable=print
print("""
WARNING: Implicitly using pnpm-lock.yaml file `{pnpm_lock}` that is expected to be the result of running `pnpm import` on the `{lock}` lock file.
Expand All @@ -711,7 +709,7 @@ WARNING: Implicitly using pnpm-lock.yaml file `{pnpm_lock}` that is expected to
# because at this point the user has likely not added all package.json and data files that
# pnpm import depends on to `npm_translate_lock`. In order to get a complete initial pnpm lock
# file with all workspace package imports listed we likely need to run in the source tree.
bootstrap_working_directory = paths.dirname(pnpm_lock_path)
bootstrap_working_directory = pnpm_lock_path.dirname

if not rctx.attr.quiet:
# buildifier: disable=print
Expand All @@ -729,7 +727,7 @@ INFO: Running initial `pnpm import` in `{wd}` to bootstrap the pnpm-lock.yaml fi
state.label_store.path("pnpm_entry"),
"import",
],
working_directory = bootstrap_working_directory,
working_directory = str(bootstrap_working_directory),
quiet = rctx.attr.quiet,
)
if result.return_code:
Expand All @@ -741,7 +739,7 @@ STDERR:
""".format(status = result.return_code, stdout = result.stdout, stderr = result.stderr)
fail(msg)

if not utils.exists(rctx, pnpm_lock_path):
if not pnpm_lock_path.exists:
msg = """

ERROR: Running `pnpm import` did not generate the {path} file.
Expand Down Expand Up @@ -802,11 +800,10 @@ STDERR:
def _update_pnpm_lock(rctx, state):
_execute_preupdate_scripts(rctx, state)

pnpm_lock_label = state.label_store.label("pnpm_lock")
pnpm_lock_relative_path = state.label_store.relative_path("pnpm_lock")
pnpm_lock_relative_path = str(rctx.path(rctx.attr.pnpm_lock))

update_cmd = ["import"] if rctx.attr.npm_package_lock or rctx.attr.yarn_lock else ["install", "--lockfile-only"]
update_working_directory = paths.dirname(state.label_store.repository_path("pnpm_lock"))
update_working_directory = rctx.workspace_root.get_child(pnpm_lock_relative_path).dirname

pnpm_cmd = " ".join(update_cmd)

Expand All @@ -833,7 +830,7 @@ INFO: Updating `{pnpm_lock}` file as its inputs have changed since the last upda
# to be specified. This requirement means that if any data file changes then the update command will be
# re-run. For cases where all data files cannot be specified a user can simply turn off auto-updates
# by setting update_pnpm_lock to False and update their pnpm-lock.yaml file manually.
working_directory = update_working_directory,
working_directory = str(update_working_directory),
quiet = rctx.attr.quiet,
)
if result.return_code:
Expand Down Expand Up @@ -863,15 +860,15 @@ STDERR:

lockfile_changed = False
if state.set_input_hash(
state.label_store.relative_path("pnpm_lock"),
utils.hash(rctx.read(state.label_store.repository_path("pnpm_lock"))),
pnpm_lock_relative_path,
utils.hash(rctx.read(rctx.attr.pnpm_lock)),
):
# The lock file has changed
if not rctx.attr.quiet:
# buildifier: disable=print
print("""
INFO: {} file has changed""".format(pnpm_lock_relative_path))
utils.reverse_force_copy(rctx, pnpm_lock_label)
INFO: %s file has changed""" % pnpm_lock_relative_path)
utils.reverse_force_copy(rctx, rctx.attr.pnpm_lock)
lockfile_changed = True

state.write_action_cache()
Expand All @@ -893,7 +890,7 @@ ERROR: `{action_cache}` is out of date. `{pnpm_lock}` may require an update. To

""".format(
action_cache = state.label_store.relative_path("action_cache"),
pnpm_lock = state.label_store.relative_path("pnpm_lock"),
pnpm_lock = str(rctx.path(rctx.attr.pnpm_lock)),
repo_reference_symbol = repo_reference_symbol,
rctx_name = rctx.name,
))
11 changes: 5 additions & 6 deletions npm/private/npm_translate_lock_generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ _PACKAGE_JSON_BZL_FILENAME = "package_json.bzl"
_RESOLVED_JSON_FILENAME = "resolved.json"

# buildifier: disable=function-docstring
def generate_repository_files(rctx, pnpm_lock_label, importers, packages, patched_dependencies, only_built_dependencies, root_package, default_registry, npm_registries, npm_auth, link_workspace):
def generate_repository_files(rctx, importers, packages, patched_dependencies, only_built_dependencies, root_package, default_registry, npm_registries, npm_auth, link_workspace):
# empty line after bzl docstring since buildifier expects this if this file is vendored in
generated_by_prefix = "\"\"\"@generated by npm_translate_lock(name = \"{}\", pnpm_lock = \"{}\")\"\"\"\n".format(helpers.to_apparent_repo_name(rctx.name), str(pnpm_lock_label))
generated_by_prefix = "\"\"\"@generated by npm_translate_lock(name = \"{}\", pnpm_lock = \"{}\")\"\"\"\n".format(helpers.to_apparent_repo_name(rctx.name), str(rctx.attr.pnpm_lock))

npm_imports = helpers.get_npm_imports(importers, packages, patched_dependencies, only_built_dependencies, root_package, rctx.name, rctx.attr, rctx.attr.lifecycle_hooks, rctx.attr.lifecycle_hooks_execution_requirements, rctx.attr.lifecycle_hooks_use_default_shell_env, npm_registries, default_registry, npm_auth)

Expand Down Expand Up @@ -248,7 +248,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
defs_bzl_file = "@{}//:{}".format(rctx.name, rctx.attr.defs_bzl_filename),
link_packages_comma_separated = "\"'\" + \"', '\".join(_LINK_PACKAGES) + \"'\"" if len(link_packages) else "\"\"",
root_package = root_package,
pnpm_lock_label = pnpm_lock_label,
pnpm_lock_label = rctx.attr.pnpm_lock,
),
]

Expand Down Expand Up @@ -480,21 +480,20 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
rctx_files[rctx.attr.repositories_bzl_filename] = _generate_repositories(
rctx,
npm_imports,
pnpm_lock_label,
link_workspace,
)

for filename, contents in rctx_files.items():
rctx.file(filename, generated_by_prefix + "\n" + "\n".join(contents))

def _generate_repositories(rctx, npm_imports, pnpm_lock_label, link_workspace):
def _generate_repositories(rctx, npm_imports, link_workspace):
repositories_bzl = []

if len(npm_imports) > 0:
repositories_bzl.append("""load("@aspect_rules_js//npm:repositories.bzl", "npm_import")""")
repositories_bzl.append("")

repositories_bzl.append("# Generated npm_import repository rules corresponding to npm packages in {}".format(str(pnpm_lock_label)))
repositories_bzl.append("# Generated npm_import repository rules corresponding to npm packages in {}".format(str(rctx.attr.pnpm_lock)))
repositories_bzl.append("# buildifier: disable=function-docstring")
repositories_bzl.append("def npm_repositories():")
if len(npm_imports) == 0:
Expand Down
18 changes: 9 additions & 9 deletions npm/private/npm_translate_lock_state.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ WARNING: `update_pnpm_lock` attribute in `npm_translate_lock(name = "{rctx_name}
# labels only needed when updating the pnpm lock file
_init_update_labels(priv, rctx, attr, label_store)

_init_link_workspace(priv, rctx, attr, label_store)
_init_link_workspace(priv, attr)

# parse the pnpm lock file incase since we need the importers list for additional init
# TODO(windows): utils.exists is not yet support on Windows
Expand Down Expand Up @@ -134,7 +134,7 @@ def _init_pnpm_labels(priv, rctx, attr, label_store):

################################################################################
def _init_update_labels(priv, _, attr, label_store):
pnpm_lock_label = label_store.label("pnpm_lock")
pnpm_lock_label = attr.pnpm_lock
pnpm_lock_label_str = "//{}:{}".format(pnpm_lock_label.package, pnpm_lock_label.name)
action_cache_path = paths.join(
priv["external_repository_action_cache"],
Expand Down Expand Up @@ -170,7 +170,7 @@ def _init_patched_dependencies_labels(priv, _, attr, label_store):
# Read patches from pnpm-lock.yaml `patchedDependencies`
patches = []
for patch_info in priv["patched_dependencies"].values():
patches.append("//%s:%s" % (label_store.label("pnpm_lock").package, patch_info.get("path")))
patches.append("//%s:%s" % (attr.pnpm_lock.package, patch_info.get("path")))

# Convert patch label strings to labels
patches = [attr.pnpm_lock.relative(p) for p in patches]
Expand All @@ -186,9 +186,9 @@ def _init_importer_labels(priv, label_store):
label_store.add_sibling("lock", "package_json_{}".format(i), paths.join(p, PACKAGE_JSON_FILENAME))

################################################################################
def _init_link_workspace(priv, _, attr, label_store):
def _init_link_workspace(priv, attr):
# initialize link_workspace either from pnpm_lock label or from override
priv["link_workspace"] = attr.link_workspace if attr.link_workspace else label_store.label("pnpm_lock").repo_name
priv["link_workspace"] = attr.link_workspace if attr.link_workspace else attr.pnpm_lock.repo_name

################################################################################
def _init_external_repository_action_cache(priv, attr):
Expand All @@ -197,7 +197,7 @@ def _init_external_repository_action_cache(priv, attr):

################################################################################
def _init_root_package(priv, rctx, attr, label_store):
pnpm_lock_label = label_store.label("pnpm_lock")
pnpm_lock_label = attr.pnpm_lock

# use the directory of the pnpm_lock file as the root_package unless overridden by the root_package attribute
if attr.root_package == DEFAULT_ROOT_PACKAGE:
Expand Down Expand Up @@ -284,7 +284,7 @@ def _copy_update_input_files(priv, rctx, attr, label_store):
################################################################################
# we can derive input files that should be specified but are not and copy these over; we warn the user when we do this
def _copy_unspecified_input_files(priv, rctx, attr, label_store):
pnpm_lock_label = label_store.label("pnpm_lock")
pnpm_lock_label = attr.pnpm_lock

# pnpm-workspace.yaml
pnpm_workspace_key = "pnpm_workspace"
Expand Down Expand Up @@ -480,7 +480,7 @@ WARNING: Cannot determine home directory in order to load home `.npmrc` file in
_load_npmrc(priv, rctx, home_npmrc_path)

################################################################################
def _load_lockfile(priv, rctx, _, label_store):
def _load_lockfile(priv, rctx, attr, label_store):
importers = {}
packages = {}
patched_dependencies = {}
Expand All @@ -489,7 +489,7 @@ def _load_lockfile(priv, rctx, _, label_store):

yq_args = [
str(label_store.path("host_yq")),
str(label_store.path("pnpm_lock")),
attr.pnpm_lock,
"-o=json",
]
result = rctx.execute(yq_args)
Expand Down
Loading