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

refactor: tweak ':' separator locations in package_json.bzl load paths #241

Merged
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
2 changes: 1 addition & 1 deletion docs/migrate.md

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

2 changes: 1 addition & 1 deletion e2e/npm_link_package/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
load("@rules_foo_npm//foo/@aspect-test/a:package_json.bzl", aspect_test_a_bin = "bin")
load("@rules_foo_npm//foo:@aspect-test/a/package_json.bzl", aspect_test_a_bin = "bin")

npm_link_all_packages(name = "node_modules")

Expand Down
2 changes: 1 addition & 1 deletion e2e/pnpm_workspace/app/a/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_test")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//app/a/@aspect-test/a:package_json.bzl", aspect_test_a_bin = "bin")
load("@npm//app/a:@aspect-test/a/package_json.bzl", aspect_test_a_bin = "bin")

npm_link_all_packages(name = "node_modules")

Expand Down
2 changes: 1 addition & 1 deletion e2e/pnpm_workspace_dot_dot/app/a/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_test")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//app/a/@aspect-test/a:package_json.bzl", aspect_test_a_bin = "bin")
load("@npm//app/a:@aspect-test/a/package_json.bzl", aspect_test_a_bin = "bin")

npm_link_all_packages(name = "node_modules")

Expand Down
2 changes: 1 addition & 1 deletion examples/js_binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("@aspect_bazel_lib//lib:directory_path.bzl", "directory_path")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary", "js_test")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@npm//examples/npm_deps/@aspect-test/a:package_json.bzl", aspect_test_a_bin = "bin")
load("@npm//examples/npm_deps:@aspect-test/a/package_json.bzl", aspect_test_a_bin = "bin")
load("@npm//:defs.bzl", "npm_link_all_packages")

# Link all direct dependencies in /examples/npm_deps/package.json to
Expand Down
2 changes: 1 addition & 1 deletion examples/js_library/two/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@npm//typescript:package_json.bzl", typescript_bin = "bin")
load("@npm//:typescript/package_json.bzl", typescript_bin = "bin")
load("@aspect_rules_js//js:defs.bzl", "js_test")

typescript_bin.tsc(
Expand Down
2 changes: 1 addition & 1 deletion examples/macro/mocha.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Example macro wrapping the mocha CLI"

load("@npm//examples/macro/mocha:package_json.bzl", "bin")
load("@npm//examples/macro:mocha/package_json.bzl", "bin")

def mocha_test(name, srcs, args = [], data = [], env = {}, **kwargs):
bin.mocha_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/npm_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm__rollup__2.70.2//examples/npm_deps:package_json.bzl", "rollup", "rollup_binary", "rollup_test")

# Load from the "bin" property from the package.json of the uvu package
load("@npm//examples/npm_deps/uvu:package_json.bzl", uvu_bin = "bin")
load("@npm//examples/npm_deps:uvu/package_json.bzl", uvu_bin = "bin")

###########################

Expand Down
2 changes: 1 addition & 1 deletion js/private/node-patches/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@npm//typescript:package_json.bzl", typescript_bin = "bin")
load("@npm//:typescript/package_json.bzl", typescript_bin = "bin")

typescript_bin.tsc(
name = "compile",
Expand Down
41 changes: 19 additions & 22 deletions npm/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,17 @@ def _impl(rctx):

bazel_name = utils.bazel_name(rctx.attr.package, rctx.attr.version)

root_package_json_bzl = False
rctx_files = {
"BUILD.bazel": generated_by_lines + [
"""load("@aspect_rules_js//npm/private:npm_package_internal.bzl", _npm_package = "npm_package_internal")""",
],
}

rctx_files["BUILD.bazel"].append(_JS_PACKAGE_TMPL.format(
extract_to_dirname = _EXTRACT_TO_DIRNAME,
package = rctx.attr.package,
version = rctx.attr.version,
))

if bins:
for link_package in rctx.attr.link_packages.keys():
Expand Down Expand Up @@ -406,34 +416,21 @@ bin = bin_factory("node_modules")
bin_struct_fields = "\n".join(bin_struct_fields),
))

if link_package == "":
root_package_json_bzl = True
else:
rctx.file(paths.normalize(paths.join(link_package, "BUILD.bazel")), "\n".join(generated_by_lines + [
"exports_files({})".format(starlark_codegen_utils.to_list_attr([_PACKAGE_JSON_BZL_FILENAME])),
]))
rctx.file(paths.normalize(paths.join(link_package, _PACKAGE_JSON_BZL_FILENAME)), "\n".join(bin_bzl))
rctx_files[paths.join(link_package, _PACKAGE_JSON_BZL_FILENAME)] = bin_bzl

build_file = paths.join(link_package, "BUILD.bazel")
if build_file not in rctx_files:
rctx_files[build_file] = generated_by_lines[:]
rctx_files[build_file].append("""exports_files(["{}"])""".format(_PACKAGE_JSON_BZL_FILENAME))

if rctx.attr.run_lifecycle_hooks:
_inject_run_lifecycle_hooks(rctx, pkg_json_path)

if rctx.attr.custom_postinstall:
_inject_custom_postinstall(rctx, pkg_json_path, rctx.attr.custom_postinstall)

build_file = generated_by_lines + [
"""load("@aspect_rules_js//npm/private:npm_package_internal.bzl", _npm_package = "npm_package_internal")""",
]

build_file.append(_JS_PACKAGE_TMPL.format(
extract_to_dirname = _EXTRACT_TO_DIRNAME,
package = rctx.attr.package,
version = rctx.attr.version,
))

if root_package_json_bzl:
build_file.append("exports_files(%s)" % starlark_codegen_utils.to_list_attr([_PACKAGE_JSON_BZL_FILENAME]))

rctx.file("BUILD.bazel", "\n".join(build_file))
for filename, contents in rctx_files.items():
rctx.file(filename, "\n".join(contents))

def _sanitize_bin_name(name):
""" Sanitize a package name so we can use it in starlark function names """
Expand Down
14 changes: 7 additions & 7 deletions npm/private/npm_translate_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ _NPM_IMPORT_TMPL = \
"""

_BIN_TMPL = \
"""load("@{repo_name}//{repo_package_json_bzl}", _bin = "bin", _bin_factory = "bin_factory")
"""load("{repo_package_json_bzl}", _bin = "bin", _bin_factory = "bin_factory")
bin = _bin
bin_factory = _bin_factory
"""
Expand Down Expand Up @@ -733,17 +733,17 @@ load("@aspect_rules_js//npm/private:npm_linked_packages.bzl", "npm_linked_packag
# If this is a problem, we could lookup into the packages again like
# if lockfile.get("packages").values()[i].get("hasBin"):
if True:
build_file_path = paths.normalize(paths.join(link_package, _import.package, "BUILD.bazel"))
build_file_path = paths.normalize(paths.join(link_package, "BUILD.bazel"))
rctx.file(build_file_path, "\n".join(generated_by_lines))
package_json_bzl_file_path = paths.normalize(paths.join(link_package, _import.package, _PACKAGE_JSON_BZL_FILENAME))
repo_package_json_bzl = paths.normalize(paths.join(link_package, _PACKAGE_JSON_BZL_FILENAME)).rsplit("/", 1)
if len(repo_package_json_bzl) == 1:
repo_package_json_bzl = [""] + repo_package_json_bzl
repo_package_json_bzl = ":".join(repo_package_json_bzl)
repo_package_json_bzl = "@{repo_name}//{link_package}:{package_json_bzl}".format(
repo_name = _import.name,
link_package = link_package,
package_json_bzl = _PACKAGE_JSON_BZL_FILENAME,
)
rctx.file(package_json_bzl_file_path, "\n".join([
_BIN_TMPL.format(
repo_package_json_bzl = repo_package_json_bzl,
repo_name = _import.name,
),
]))

Expand Down