Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce use of bazel_skylib paths.join #1812

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
17 changes: 8 additions & 9 deletions npm/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ load(
_git_init = "init",
_git_reset = "reset",
)
load("//npm/private:tar.bzl", "check_is_gnu_tar")
load(":starlark_codegen_utils.bzl", "starlark_codegen_utils")
load(":utils.bzl", "utils")
load("//npm/private:tar.bzl", "check_is_gnu_tar")

_LINK_JS_PACKAGE_LOADS_TMPL = """\
# buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -360,6 +360,8 @@ bzl_library(

_TARBALL_FILENAME = "package.tgz"
_EXTRACT_TO_DIRNAME = "package"
_EXTRACT_TO_PACKAGE_JSON = "{}/package.json".format(_EXTRACT_TO_DIRNAME)
_EXTRACT_TO_RULES_JS_METADATA = "{}/aspect_rules_js_metadata.json".format(_EXTRACT_TO_DIRNAME)
_DEFS_BZL_FILENAME = "defs.bzl"
_PACKAGE_JSON_BZL_FILENAME = "package_json.bzl"

Expand Down Expand Up @@ -500,9 +502,7 @@ def _npm_import_rule_impl(rctx):
# the patch targets the package.json itself
patch(rctx, patch_args = rctx.attr.patch_args, patch_directory = _EXTRACT_TO_DIRNAME)

pkg_json_path = paths.join(_EXTRACT_TO_DIRNAME, "package.json")

pkg_json = json.decode(rctx.read(pkg_json_path))
pkg_json = json.decode(rctx.read(_EXTRACT_TO_PACKAGE_JSON))

bins = _get_bin_entries(pkg_json, rctx.attr.package)

Expand Down Expand Up @@ -579,9 +579,9 @@ bin = bin_factory("node_modules")
bin_struct_fields = "\n".join(bin_struct_fields),
))

rctx_files[paths.join(link_package, _PACKAGE_JSON_BZL_FILENAME)] = bin_bzl
rctx_files["{}/{}".format(link_package, _PACKAGE_JSON_BZL_FILENAME) if link_package else _PACKAGE_JSON_BZL_FILENAME] = bin_bzl

build_file = paths.join(link_package, "BUILD.bazel")
build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel"
if build_file not in rctx_files:
rctx_files[build_file] = [generated_by_prefix]
if rctx.attr.generate_bzl_library_targets:
Expand All @@ -600,8 +600,7 @@ bin = bin_factory("node_modules")
rules_js_metadata["scripts"]["custom_postinstall"] = rctx.attr.custom_postinstall

if rules_js_metadata:
rules_js_json_path = paths.join(_EXTRACT_TO_DIRNAME, "aspect_rules_js_metadata.json")
rctx.file(rules_js_json_path, json.encode_indent(rules_js_metadata, indent = " "))
rctx.file(_EXTRACT_TO_RULES_JS_METADATA, json.encode_indent(rules_js_metadata, indent = " "))

for filename, contents in rctx_files.items():
rctx.file(filename, "\n".join(contents))
Expand Down Expand Up @@ -687,7 +686,7 @@ def _npm_import_links_rule_impl(rctx):
package_store_name = utils.package_store_name(rctx.attr.package, rctx.attr.version)

# "node_modules/{package_store_root}/{package_store_name}/node_modules/{package}"
lifecycle_output_dir = paths.join("node_modules", utils.package_store_root, package_store_name, "node_modules", rctx.attr.package)
lifecycle_output_dir = "node_modules/{}/{}/node_modules/{}".format(utils.package_store_root, package_store_name, rctx.attr.package)

# strip _links post-fix to get the repository name of the npm sources
npm_import_sources_repo_name = rctx.name[:-len(utils.links_repo_suffix)]
Expand Down
7 changes: 3 additions & 4 deletions npm/private/npm_link_package_store.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"npm_link_package_store rule"

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//js:providers.bzl", "JsInfo", "js_info")
load(":npm_package_store_info.bzl", "NpmPackageStoreInfo")
load(":utils.bzl", "utils")
Expand Down Expand Up @@ -78,13 +77,13 @@ def _npm_link_package_store_impl(ctx):

# symlink the package's path in the package store to the root of the node_modules
# "node_modules/{package}" so it is available as a direct dependency
root_symlink_path = paths.join("node_modules", package)
root_symlink_path = "node_modules/{}".format(package)

files = [utils.make_symlink(ctx, root_symlink_path, package_store_directory.path)]

for bin_name, bin_path in ctx.attr.bins.items():
bin_file = ctx.actions.declare_file(paths.join("node_modules", ".bin", bin_name))
bin_path = paths.normalize(paths.join("..", package, bin_path))
bin_file = ctx.actions.declare_file("node_modules/.bin/{}".format(bin_name))
bin_path = "../{}/{}".format(package, bin_path)
ctx.actions.write(
bin_file,
_BIN_TMPL.format(bin_path = bin_path),
Expand Down
13 changes: 7 additions & 6 deletions npm/private/npm_translate_lock_generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ sh_binary(
fail(msg)
fp_links[dep_key]["link_packages"][link_package] = []
elif dep_version.startswith("link:"):
dep_importer = paths.normalize(paths.join(import_path, dep_version[len("link:"):]))
dep_path = helpers.link_package(root_package, import_path, dep_version[len("link:"):])
dep_version = dep_version[len("link:"):]
dep_importer = paths.normalize("{}/{}".format(import_path, dep_version) if import_path else dep_version)
dep_path = helpers.link_package(root_package, import_path, dep_version)
dep_key = "{}+{}".format(dep_package, dep_path)
if dep_key in fp_links.keys():
fp_links[dep_key]["link_packages"][link_package] = []
Expand Down Expand Up @@ -282,10 +283,10 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
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 = paths.normalize(paths.join(link_package, "BUILD.bazel"))
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_file_path = paths.normalize(paths.join(link_package, _import.package, _RESOLVED_JSON_FILENAME))
resolved_json_file_path = "{}/{}/{}".format(link_package, _import.package, _RESOLVED_JSON_FILENAME).lstrip("/")
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",
Expand All @@ -298,14 +299,14 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
rctx_files[build_file].append("""load("@bazel_skylib//:bzl_library.bzl", "bzl_library")""")
rctx_files[build_file].append(_BZL_LIBRARY_TMPL.format(
name = _import.package,
src = ":" + paths.join(_import.package, _PACKAGE_JSON_BZL_FILENAME),
src = ":{}/{}".format(_import.package, _PACKAGE_JSON_BZL_FILENAME),
dep = "@{repo_name}//{link_package}:{package_name}_bzl_library".format(
repo_name = helpers.to_apparent_repo_name(_import.name),
link_package = link_package,
package_name = link_package.split("/")[-1] or _import.package.split("/")[-1],
),
))
package_json_bzl_file_path = paths.normalize(paths.join(link_package, _import.package, _PACKAGE_JSON_BZL_FILENAME))
package_json_bzl_file_path = "{}/{}/{}".format(link_package, _import.package, _PACKAGE_JSON_BZL_FILENAME) if link_package else "{}/{}".format(_import.package, _PACKAGE_JSON_BZL_FILENAME)
repo_package_json_bzl = "@@{repo_name}//{link_package}:{package_json_bzl}".format(
repo_name = _import.name,
link_package = link_package,
Expand Down
Loading