From 04e3db7b61502a0b4c9daf0006413a282c6a6076 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 11:48:08 +0000 Subject: [PATCH 1/7] build: remove usages of `rules_nodejs` `copy_to_bin` Replaces usages of `rules_nodejs` `copy_to_bin` with the `rules_js`/Aspect bazel lib equivalent. --- BUILD.bazel | 16 +--------------- tests/legacy-cli/e2e/assets/BUILD.bazel | 2 +- tests/legacy-cli/e2e/ng-snapshot/BUILD.bazel | 2 +- tools/defaults2.bzl | 8 ++++++++ 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index f54a531b46b0..e428d013ef93 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,13 +1,7 @@ load("@aspect_rules_ts//ts:defs.bzl", rules_js_tsconfig = "ts_config") - -# Copyright Google Inc. All Rights Reserved. -# -# Use of this source code is governed by an MIT-style license that can be -# found in the LICENSE file at https://angular.dev/license load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") -load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin") -load("@npm//@bazel/concatjs:index.bzl", "ts_config") load("@npm2//:defs.bzl", "npm_link_all_packages") +load("//tools:defaults2.bzl", "copy_to_bin") package(default_visibility = ["//visibility:public"]) @@ -100,11 +94,3 @@ config_setting( ":enable_snapshot_repo_deps": "true", }, ) - -ts_config( - name = "tsconfig-build-esm", - src = "tsconfig-build-esm.json", - deps = [ - ":tsconfig.json", - ], -) diff --git a/tests/legacy-cli/e2e/assets/BUILD.bazel b/tests/legacy-cli/e2e/assets/BUILD.bazel index a2889b56559f..a3e838b59a7b 100644 --- a/tests/legacy-cli/e2e/assets/BUILD.bazel +++ b/tests/legacy-cli/e2e/assets/BUILD.bazel @@ -1,4 +1,4 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin") +load("//tools:defaults2.bzl", "copy_to_bin") copy_to_bin( name = "assets", diff --git a/tests/legacy-cli/e2e/ng-snapshot/BUILD.bazel b/tests/legacy-cli/e2e/ng-snapshot/BUILD.bazel index 079ffeb1accc..a6196b576d38 100644 --- a/tests/legacy-cli/e2e/ng-snapshot/BUILD.bazel +++ b/tests/legacy-cli/e2e/ng-snapshot/BUILD.bazel @@ -1,4 +1,4 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin") +load("//tools:defaults2.bzl", "copy_to_bin") copy_to_bin( name = "ng-snapshot", diff --git a/tools/defaults2.bzl b/tools/defaults2.bzl index 6e00e853c79e..c3f76846e847 100644 --- a/tools/defaults2.bzl +++ b/tools/defaults2.bzl @@ -1,4 +1,6 @@ +load("@aspect_bazel_lib//lib:copy_to_bin.bzl", _copy_to_bin = "copy_to_bin") load("@aspect_rules_jasmine//jasmine:defs.bzl", _jasmine_test = "jasmine_test") +load("@aspect_rules_js//js:defs.bzl", _js_binary = "js_binary") load("//tools:interop.bzl", _ts_project = "ts_project") load("//tools/bazel:npm_package.bzl", _npm_package = "npm_package") @@ -8,6 +10,12 @@ def ts_project(**kwargs): def npm_package(**kwargs): _npm_package(**kwargs) +def copy_to_bin(**kwargs): + _copy_to_bin(**kwargs) + +def js_binary(**kwargs): + _js_binary(**kwargs) + def jasmine_test(data = [], args = [], **kwargs): # Create relative path to root, from current package dir. Necessary as # we change the `chdir` below to the package directory. From 2817b59469534265b52c470a64ad1930ba2f25ef Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 11:49:02 +0000 Subject: [PATCH 2/7] build: migrate `ts_json_schema` rule to `rules_js` Migrates to `rules_js` and simplifies! the `ts_json_schema` rule --- tools/BUILD.bazel | 17 +++---------- tools/ts_json_schema.bzl | 55 ++++++---------------------------------- 2 files changed, 12 insertions(+), 60 deletions(-) diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel index a2525c94cce7..88cc63790940 100644 --- a/tools/BUILD.bazel +++ b/tools/BUILD.bazel @@ -1,13 +1,6 @@ load("@aspect_bazel_lib//lib/private:tar_toolchain.bzl", "tar_toolchain") -load("@aspect_rules_js//js:defs.bzl", "js_binary") load("@bazel_skylib//rules:copy_file.bzl", "copy_file") - -# Copyright Google Inc. All Rights Reserved. -# -# Use of this source code is governed by an MIT-style license that can be -# found in the LICENSE file at https://angular.dev/license -# @external_begin -load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") +load("//tools:defaults2.bzl", "js_binary") package(default_visibility = ["//visibility:public"]) @@ -15,7 +8,7 @@ exports_files([ "package_json_release_filter.jq", ]) -nodejs_binary( +js_binary( name = "ng_cli_schema", data = [ "ng_cli_schema_generator.js", @@ -23,14 +16,13 @@ nodejs_binary( entry_point = "ng_cli_schema_generator.js", ) -nodejs_binary( +js_binary( name = "quicktype_runner", data = [ "quicktype_runner.js", - "@npm//quicktype-core", + "//:node_modules/quicktype-core", ], entry_point = "quicktype_runner.js", - templated_args = ["--bazel_patch_module_resolver"], ) tar_toolchain( @@ -62,4 +54,3 @@ js_binary( entry_point = ":copy_worker_js", fixed_args = ["--vanilla-ts"], ) -# @external_end diff --git a/tools/ts_json_schema.bzl b/tools/ts_json_schema.bzl index 336d5811fe04..deb34c0af597 100644 --- a/tools/ts_json_schema.bzl +++ b/tools/ts_json_schema.bzl @@ -1,46 +1,4 @@ -# Copyright Google Inc. All Rights Reserved. -# -# Use of this source code is governed by an MIT-style license that can be -# found in the LICENSE file at https://angular.dev/license - -def _ts_json_schema_interface_impl(ctx): - args = [ - ctx.files.src[0].path, - ctx.outputs.ts.path, - ] - - ctx.actions.run( - inputs = ctx.files.src + ctx.files.data, - executable = ctx.executable._binary, - outputs = [ctx.outputs.ts], - arguments = args, - ) - - return [DefaultInfo()] - -_ts_json_schema_interface = rule( - _ts_json_schema_interface_impl, - attrs = { - "src": attr.label( - allow_files = [".json"], - mandatory = True, - ), - "out": attr.string( - mandatory = True, - ), - "data": attr.label_list( - allow_files = [".json"], - ), - "_binary": attr.label( - default = Label("//tools:quicktype_runner"), - executable = True, - cfg = "exec", - ), - }, - outputs = { - "ts": "%{out}", - }, -) +load("@aspect_rules_js//js:defs.bzl", "js_run_binary") # Generates a TS file that contains the interface for a JSON Schema file. Takes a single `src` # argument as input, an optional data field for reference files, and produces a @@ -50,9 +8,12 @@ _ts_json_schema_interface = rule( def ts_json_schema(name, src, data = []): out = src.replace(".json", ".ts") - _ts_json_schema_interface( + js_run_binary( name = name + ".interface", - src = src, - out = out, - data = data, + outs = [out], + srcs = [src] + data, + tool = "//tools:quicktype_runner", + progress_message = "Generating TS interface for %s" % src, + mnemonic = "TsJsonSchema", + args = ["$(rootpath %s)" % src, "$(rootpath %s)" % out], ) From f16c43cac60682628a9e5f8ed0628e2c959586b7 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 12:57:26 +0000 Subject: [PATCH 3/7] build: migrate CLI schema generation to `rules_js` Migrates the CLI schema generation to `rules_js`, also significantly simplifying the rule boilerplate. --- packages/angular/build/BUILD.bazel | 7 ++- packages/angular/cli/BUILD.bazel | 37 ++----------- .../angular_devkit/build_angular/BUILD.bazel | 7 ++- packages/schematics/angular/BUILD.bazel | 7 ++- tools/ng_cli_schema_generator.bzl | 54 ++++--------------- 5 files changed, 32 insertions(+), 80 deletions(-) diff --git a/packages/angular/build/BUILD.bazel b/packages/angular/build/BUILD.bazel index 9843d3a86502..b9c75e97af08 100644 --- a/packages/angular/build/BUILD.bazel +++ b/packages/angular/build/BUILD.bazel @@ -1,6 +1,6 @@ load("@npm//@angular/build-tooling/bazel/api-golden:index.bzl", "api_golden_test_npm_package") load("@npm2//:defs.bzl", "npm_link_all_packages") -load("//tools:defaults2.bzl", "jasmine_test", "npm_package", "ts_project") +load("//tools:defaults2.bzl", "copy_to_bin", "jasmine_test", "npm_package", "ts_project") load("//tools:ts_json_schema.bzl", "ts_json_schema") licenses(["notice"]) @@ -29,6 +29,11 @@ ts_json_schema( src = "src/builders/ng-packagr/schema.json", ) +copy_to_bin( + name = "schemas", + srcs = glob(["**/schema.json"]), +) + RUNTIME_ASSETS = glob( include = [ "src/**/schema.json", diff --git a/packages/angular/cli/BUILD.bazel b/packages/angular/cli/BUILD.bazel index dc53492b28b5..a150ed215460 100644 --- a/packages/angular/cli/BUILD.bazel +++ b/packages/angular/cli/BUILD.bazel @@ -74,40 +74,9 @@ ts_project( ) CLI_SCHEMA_DATA = [ - "//packages/angular/build:src/builders/application/schema.json", - "//packages/angular/build:src/builders/dev-server/schema.json", - "//packages/angular/build:src/builders/extract-i18n/schema.json", - "//packages/angular/build:src/builders/ng-packagr/schema.json", - "//packages/angular_devkit/build_angular:src/builders/app-shell/schema.json", - "//packages/angular_devkit/build_angular:src/builders/browser/schema.json", - "//packages/angular_devkit/build_angular:src/builders/browser-esbuild/schema.json", - "//packages/angular_devkit/build_angular:src/builders/dev-server/schema.json", - "//packages/angular_devkit/build_angular:src/builders/extract-i18n/schema.json", - "//packages/angular_devkit/build_angular:src/builders/jest/schema.json", - "//packages/angular_devkit/build_angular:src/builders/web-test-runner/schema.json", - "//packages/angular_devkit/build_angular:src/builders/karma/schema.json", - "//packages/angular_devkit/build_angular:src/builders/ng-packagr/schema.json", - "//packages/angular_devkit/build_angular:src/builders/prerender/schema.json", - "//packages/angular_devkit/build_angular:src/builders/ssr-dev-server/schema.json", - "//packages/angular_devkit/build_angular:src/builders/protractor/schema.json", - "//packages/angular_devkit/build_angular:src/builders/server/schema.json", - "//packages/schematics/angular:app-shell/schema.json", - "//packages/schematics/angular:application/schema.json", - "//packages/schematics/angular:class/schema.json", - "//packages/schematics/angular:component/schema.json", - "//packages/schematics/angular:directive/schema.json", - "//packages/schematics/angular:enum/schema.json", - "//packages/schematics/angular:guard/schema.json", - "//packages/schematics/angular:interceptor/schema.json", - "//packages/schematics/angular:interface/schema.json", - "//packages/schematics/angular:library/schema.json", - "//packages/schematics/angular:module/schema.json", - "//packages/schematics/angular:ng-new/schema.json", - "//packages/schematics/angular:pipe/schema.json", - "//packages/schematics/angular:resolver/schema.json", - "//packages/schematics/angular:service/schema.json", - "//packages/schematics/angular:service-worker/schema.json", - "//packages/schematics/angular:web-worker/schema.json", + "//packages/angular/build:schemas", + "//packages/angular_devkit/build_angular:schemas", + "//packages/schematics/angular:schemas", ] cli_json_schema( diff --git a/packages/angular_devkit/build_angular/BUILD.bazel b/packages/angular_devkit/build_angular/BUILD.bazel index 689e044d1500..0c3e6d2dffb6 100644 --- a/packages/angular_devkit/build_angular/BUILD.bazel +++ b/packages/angular_devkit/build_angular/BUILD.bazel @@ -5,7 +5,7 @@ load("@npm//@angular/build-tooling/bazel/api-golden:index.bzl", "api_golden_test_npm_package") load("@npm2//:defs.bzl", "npm_link_all_packages") -load("//tools:defaults2.bzl", "jasmine_test", "npm_package", "ts_project") +load("//tools:defaults2.bzl", "copy_to_bin", "jasmine_test", "npm_package", "ts_project") load("//tools:ts_json_schema.bzl", "ts_json_schema") licenses(["notice"]) @@ -79,6 +79,11 @@ ts_json_schema( src = "src/builders/web-test-runner/schema.json", ) +copy_to_bin( + name = "schemas", + srcs = glob(["**/schema.json"]), +) + RUNTIME_ASSETS = glob( include = [ "src/**/schema.json", diff --git a/packages/schematics/angular/BUILD.bazel b/packages/schematics/angular/BUILD.bazel index 8b3f6bf0b728..b9054ad1c05e 100644 --- a/packages/schematics/angular/BUILD.bazel +++ b/packages/schematics/angular/BUILD.bazel @@ -4,7 +4,7 @@ # found in the LICENSE file at https://angular.dev/license load("@npm2//:defs.bzl", "npm_link_all_packages") -load("//tools:defaults2.bzl", "jasmine_test", "npm_package", "ts_project") +load("//tools:defaults2.bzl", "copy_to_bin", "jasmine_test", "npm_package", "ts_project") load("//tools:ts_json_schema.bzl", "ts_json_schema") licenses(["notice"]) @@ -37,6 +37,11 @@ ALL_SCHEMA_TARGETS = [ for (src, name) in ALL_SCHEMA_TARGETS ] +copy_to_bin( + name = "schemas", + srcs = glob(["**/schema.json"]), +) + RUNTIME_ASSETS = [ "collection.json", "migrations/migration-collection.json", diff --git a/tools/ng_cli_schema_generator.bzl b/tools/ng_cli_schema_generator.bzl index 1d2092e4a7dc..9bcc4d287a6a 100644 --- a/tools/ng_cli_schema_generator.bzl +++ b/tools/ng_cli_schema_generator.bzl @@ -1,44 +1,12 @@ -# Copyright Google Inc. All Rights Reserved. -# -# Use of this source code is governed by an MIT-style license that can be -# found in the LICENSE file at https://angular.dev/license - -def _cli_json_schema_interface_impl(ctx): - args = [ - ctx.files.src[0].path, - ctx.outputs.json.path, - ] - - ctx.actions.run( - inputs = ctx.files.src + ctx.files.data, - executable = ctx.executable._binary, - outputs = [ctx.outputs.json], - arguments = args, +load("@aspect_rules_js//js:defs.bzl", "js_run_binary") + +def cli_json_schema(name, src, out, data = []): + js_run_binary( + name = name, + outs = [out], + srcs = [src] + data, + tool = "//tools:ng_cli_schema", + progress_message = "Generating CLI interface from %s" % src, + mnemonic = "NgCliJsonSchema", + args = ["$(rootpath %s)" % src, "$(rootpath %s)" % out], ) - - return [DefaultInfo()] - -cli_json_schema = rule( - _cli_json_schema_interface_impl, - attrs = { - "src": attr.label( - allow_files = [".json"], - mandatory = True, - ), - "out": attr.string( - mandatory = True, - ), - "data": attr.label_list( - allow_files = [".json"], - mandatory = True, - ), - "_binary": attr.label( - default = Label("//tools:ng_cli_schema"), - executable = True, - cfg = "exec", - ), - }, - outputs = { - "json": "%{out}", - }, -) From ce8ebe926da90df0612875c7c5db7d42bc220f45 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 13:17:23 +0000 Subject: [PATCH 4/7] build: migrate remaining usage of `pkg_npm` to `rules_js` This is necessary so that we can delete the `pkg_npm` macro and fully leverage the `rules_js` variant. --- packages/angular/ssr/BUILD.bazel | 2 +- packages/angular/ssr/schematics/BUILD.bazel | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/angular/ssr/BUILD.bazel b/packages/angular/ssr/BUILD.bazel index 8da17a526f08..c71e81e58c93 100644 --- a/packages/angular/ssr/BUILD.bazel +++ b/packages/angular/ssr/BUILD.bazel @@ -53,7 +53,7 @@ ng_package( "../../third_party/beasties", ], nested_packages = [ - "//packages/angular/ssr/schematics:npm_package", + "//packages/angular/ssr/schematics:pkg", ], deps = [ ":ssr", diff --git a/packages/angular/ssr/schematics/BUILD.bazel b/packages/angular/ssr/schematics/BUILD.bazel index 6f16fa4f933b..d8b5c05dfb4c 100644 --- a/packages/angular/ssr/schematics/BUILD.bazel +++ b/packages/angular/ssr/schematics/BUILD.bazel @@ -3,7 +3,7 @@ # Use of this source code is governed by an MIT-style license that can be # found in the LICENSE file at https://angular.dev/license -load("//tools:defaults.bzl", "pkg_npm") +load("@aspect_rules_js//npm:defs.bzl", "npm_package") load("//tools:defaults2.bzl", "jasmine_test", "ts_project") load("//tools:ts_json_schema.bzl", "ts_json_schema") @@ -57,7 +57,6 @@ ts_project( "//packages/angular/ssr/schematics:" + src.replace(".json", ".ts") for (src, _) in ALL_SCHEMA_TARGETS ], - data = [":schematics_assets"], module_name = "@angular/ssr/schematics", deps = [ "//packages/angular/ssr:node_modules/@angular-devkit/schematics", @@ -86,16 +85,18 @@ ts_project( jasmine_test( name = "ssr_schematics_test", data = [ + ":schematics_assets", ":ssr_schematics_test_lib_rjs", ], ) # This package is intended to be combined into the main @angular/ssr package as a dep. -pkg_npm( - name = "npm_package", - pkg_json = None, - visibility = ["//packages/angular/ssr:__pkg__"], - deps = [ - ":schematics", +npm_package( + name = "pkg", + srcs = [ + "package.json", + ":schematics_assets", + ":schematics_rjs", ], + visibility = ["//packages/angular/ssr:__pkg__"], ) From 04d386651f91bcb79ac5f9bae179ed53ad3e213f Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 13:19:18 +0000 Subject: [PATCH 5/7] build: remove legacy `defaults.bzl` macro file This file is currently no longer necessary after migrating all consumers to their `rules_js` variants, so we can delete the file. In follow-ups we will consider renaming `defaults2.bzl` back to this file, or have a better name altogether. --- packages/angular/ssr/BUILD.bazel | 3 +- tools/defaults.bzl | 228 ------------------------------- tools/defaults2.bzl | 13 ++ 3 files changed, 14 insertions(+), 230 deletions(-) delete mode 100644 tools/defaults.bzl diff --git a/packages/angular/ssr/BUILD.bazel b/packages/angular/ssr/BUILD.bazel index c71e81e58c93..f16b21afea70 100644 --- a/packages/angular/ssr/BUILD.bazel +++ b/packages/angular/ssr/BUILD.bazel @@ -2,8 +2,7 @@ load("@aspect_rules_js//npm:defs.bzl", "npm_package") load("@npm//@angular/build-tooling/bazel/api-golden:index.bzl", "api_golden_test_npm_package") load("@npm2//:defs.bzl", "npm_link_all_packages") load("@rules_pkg//:pkg.bzl", "pkg_tar") -load("//tools:defaults.bzl", "ng_package") -load("//tools:defaults2.bzl", "ts_project") +load("//tools:defaults2.bzl", "ng_package", "ts_project") package(default_visibility = ["//visibility:public"]) diff --git a/tools/defaults.bzl b/tools/defaults.bzl deleted file mode 100644 index a642782bd39a..000000000000 --- a/tools/defaults.bzl +++ /dev/null @@ -1,228 +0,0 @@ -"""Re-export of some bazel rules with repository-wide defaults.""" - -load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin") -load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory") -load("@aspect_bazel_lib//lib:jq.bzl", "jq") -load("@aspect_bazel_lib//lib:utils.bzl", "to_label") -load("@build_bazel_rules_nodejs//:index.bzl", _js_library = "js_library", _pkg_npm = "pkg_npm") -load("@npm//@angular/bazel:index.bzl", _ng_module = "ng_module", _ng_package = "ng_package") -load("@npm//@angular/build-tooling/bazel:extract_js_module_output.bzl", "extract_js_module_output") -load("@rules_pkg//:pkg.bzl", "pkg_tar") -load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs") -load("//tools:snapshot_repo_filter.bzl", "SNAPSHOT_REPO_JQ_FILTER") -load("//tools:substitutions.bzl", "substitutions") - -_DEFAULT_TSCONFIG_NG = "//:tsconfig-build-ng" -_DEFAULT_TSCONFIG_TEST = "//:tsconfig-test.json" - -def _default_module_name(testonly): - """ Provide better defaults for package names. - - e.g. rather than angular/packages/core/testing we want @angular/core/testing - - TODO(alexeagle): we ought to supply a default module name for every library in the repo. - But we short-circuit below in cases that are currently not working. - """ - pkg = native.package_name() - - if testonly: - # Some tests currently rely on the long-form package names - return None - - if pkg.startswith("packages/"): - return "@angular/" + pkg[len("packages/angular/"):] - - return None - -js_library = _js_library - -def pkg_npm(name, pkg_deps = [], use_prodmode_output = False, **kwargs): - """Override of pkg_npm to produce package outputs and version substitutions conventional to the angular-cli project. - - Produces a package and a tar of that package. Expects a package.json file - in the same folder to exist. - - Args: - name: Name of the pkg_npm rule. '_archive.tgz' is appended to create the tarball. - pkg_deps: package.json files of dependent packages. These are used for local path substitutions when --config=local is set. - use_prodmode_output: False to ship ES5 devmode output, True to ship ESM output. Defaults to False. - **kwargs: Additional arguments passed to the real pkg_npm. - """ - pkg_json = kwargs.pop("pkg_json", ":package.json") - visibility = kwargs.pop("visibility", None) - - if pkg_json: - deps = kwargs.pop("deps", []) - - # The `pkg_npm` rule brings in devmode (`JSModuleInfo`) and prodmode (`JSEcmaScriptModuleInfo`) - # output into the NPM package We do not intend to ship the prodmode ECMAScript `.mjs` - # files, but the `JSModuleInfo` outputs (which correspond to devmode output). Depending on - # the `use_prodmode_output` macro attribute, we either ship the ESM output of dependencies, - # or continue shipping the devmode ES5 output. - # TODO: Clean this up in the future if we have combined devmode and prodmode output. - # https://github.com/bazelbuild/rules_nodejs/commit/911529fd364eb3ee1b8ecdc568a9fcf38a8b55ca. - # https://github.com/bazelbuild/rules_nodejs/blob/stable/packages/typescript/internal/build_defs.bzl#L334-L337. - extract_js_module_output( - name = "%s_js_module_output" % name, - provider = "JSEcmaScriptModuleInfo" if use_prodmode_output else "JSModuleInfo", - include_declarations = True, - include_default_files = True, - forward_linker_mappings = False, - include_external_npm_packages = False, - deps = deps, - ) - - # Merge package.json with root package.json and perform various substitutions to - # prepare it for release. For jq docs, see https://stedolan.github.io/jq/manual/. - jq( - name = "basic_substitutions", - # Note: this jq filter relies on the order of the inputs - # buildifier: do not sort - srcs = ["//:package.json", pkg_json], - filter_file = "//tools:package_json_release_filter.jq", - args = ["--slurp"], - out = "substituted/package.json", - ) - - # Copy package.json files to bazel-out so we can use their bazel-out paths to determine - # the corresponding package npm package tgz path for substitutions. - copy_to_bin( - name = "package_json_copy", - srcs = [pkg_json], - ) - pkg_deps_copies = [] - for pkg_dep in pkg_deps: - pkg_label = to_label(pkg_dep) - if pkg_label.name != "package.json": - fail("ERROR: only package.json files allowed in pkg_deps of pkg_npm macro") - pkg_deps_copies.append("@%s//%s:package_json_copy" % (pkg_label.workspace_name, pkg_label.package)) - - # Substitute dependencies on other packages in this repo with tarballs. - link_package_json_to_tarballs( - name = "tar_substitutions", - src = "substituted/package.json", - pkg_deps = [":package_json_copy"] + pkg_deps_copies, - out = "substituted_with_tars/package.json", - ) - - # Substitute dependencies on other packages in this repo with snapshot repos. - jq( - name = "snapshot_repo_substitutions", - srcs = ["substituted/package.json"], - filter = SNAPSHOT_REPO_JQ_FILTER, - out = "substituted_with_snapshot_repos/package.json", - ) - - # Move the generated package.json along with other deps into a directory for pkg_npm - # to package up because pkg_npm requires that all inputs be in the same directory. - copy_to_directory( - name = "package", - srcs = select({ - # Do tar substitution if config_setting 'package_json_use_tar_deps' is true (local builds) - "//:package_json_use_tar_deps": [":%s_js_module_output" % name, "substituted_with_tars/package.json"], - "//:package_json_use_snapshot_repo_deps": [":%s_js_module_output" % name, "substituted_with_snapshot_repos/package.json"], - "//conditions:default": [":%s_js_module_output" % name, "substituted/package.json"], - }), - replace_prefixes = { - "substituted_with_tars/": "", - "substituted_with_snapshot_repos/": "", - "substituted/": "", - }, - exclude_srcs_patterns = [ - # Exclude compiled outputs of dependent packages - "packages/**/*", - # Exclude `node_modules` which may be pulled by the `js_module_output` runfiles. - "node_modules/**/*", - ], - allow_overwrites = True, - ) - - nested_packages = ["package"] - else: - nested_packages = None - - _pkg_npm( - name = name, - # We never set a `package_name` for NPM packages, neither do we enable validation. - # This is necessary because the source targets of the NPM packages all have - # package names set and setting a similar `package_name` on the NPM package would - # result in duplicate linker mappings that will conflict. e.g. consider the following - # scenario: We have a `ts_library` for `@angular/core`. We will configure a package - # name for the target so that it can be resolved in NodeJS executions from `node_modules`. - # If we'd also set a `package_name` for the associated `pkg_npm` target, there would be - # two mappings for `@angular/core` and the linker will complain. For a better development - # experience, we want the mapping to resolve to the direct outputs of the `ts_library` - # instead of requiring tests and other targets to assemble the NPM package first. - # TODO(devversion): consider removing this if `rules_nodejs` allows for duplicate - # linker mappings where transitive-determined mappings are skipped on conflicts. - # https://github.com/bazelbuild/rules_nodejs/issues/2810. - package_name = None, - validate = False, - substitutions = select({ - "//:stamp": substitutions["legacy"]["stamp"], - "//conditions:default": substitutions["legacy"]["nostamp"], - }), - visibility = visibility, - nested_packages = nested_packages, - tgz = None, - **kwargs - ) - - if pkg_json: - pkg_tar( - name = name + "_archive", - srcs = [":%s" % name], - extension = "tgz", - strip_prefix = "./%s" % name, - visibility = visibility, - ) - -def ng_module(name, tsconfig = None, entry_point = None, testonly = False, deps = [], module_name = None, package_name = None, **kwargs): - """Default values for ng_module""" - deps = deps + ["@npm//tslib"] - if testonly: - # Match the types[] in //packages:tsconfig-test.json - deps.append("@npm//@types/jasmine") - deps.append("@npm//@types/node") - if not tsconfig: - if testonly: - tsconfig = _DEFAULT_TSCONFIG_TEST - else: - tsconfig = _DEFAULT_TSCONFIG_NG - - if not module_name: - module_name = _default_module_name(testonly) - - # If no `package_name` is explicitly set, we use the default module name as package - # name, so that the target can be resolved within NodeJS executions, by activating - # the Bazel NodeJS linker. See: https://github.com/bazelbuild/rules_nodejs/pull/2799. - if not package_name: - package_name = _default_module_name(testonly) - - if not entry_point: - entry_point = "public_api.ts" - _ng_module( - name = name, - flat_module_out_file = name, - tsconfig = tsconfig, - entry_point = entry_point, - testonly = testonly, - deps = deps, - # `module_name` is used for AMD module names within emitted JavaScript files. - module_name = module_name, - # `package_name` can be set to allow for the Bazel NodeJS linker to run. This - # allows for resolution of the given target within the `node_modules/`. - package_name = package_name, - **kwargs - ) - -def ng_package(deps = [], **kwargs): - _ng_package( - deps = deps, - license = "//:LICENSE", - substitutions = select({ - "//:stamp": substitutions["legacy"]["stamp"], - "//conditions:default": substitutions["legacy"]["nostamp"], - }), - **kwargs - ) diff --git a/tools/defaults2.bzl b/tools/defaults2.bzl index c3f76846e847..31af54a7644a 100644 --- a/tools/defaults2.bzl +++ b/tools/defaults2.bzl @@ -1,7 +1,9 @@ load("@aspect_bazel_lib//lib:copy_to_bin.bzl", _copy_to_bin = "copy_to_bin") load("@aspect_rules_jasmine//jasmine:defs.bzl", _jasmine_test = "jasmine_test") load("@aspect_rules_js//js:defs.bzl", _js_binary = "js_binary") +load("@npm//@angular/bazel:index.bzl", _ng_package = "ng_package") load("//tools:interop.bzl", _ts_project = "ts_project") +load("//tools:substitutions.bzl", "substitutions") load("//tools/bazel:npm_package.bzl", _npm_package = "npm_package") def ts_project(**kwargs): @@ -16,6 +18,17 @@ def copy_to_bin(**kwargs): def js_binary(**kwargs): _js_binary(**kwargs) +def ng_package(deps = [], **kwargs): + _ng_package( + deps = deps, + license = "//:LICENSE", + substitutions = select({ + "//:stamp": substitutions["legacy"]["stamp"], + "//conditions:default": substitutions["legacy"]["nostamp"], + }), + **kwargs + ) + def jasmine_test(data = [], args = [], **kwargs): # Create relative path to root, from current package dir. Necessary as # we change the `chdir` below to the package directory. From 9fa77d09b197e0a04d7ef2bb53f3736e59753ab1 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 13:22:20 +0000 Subject: [PATCH 6/7] build: remove leftover unused `rules_nodejs` npm dependency This dependency does not provide any value for the `ts_project` compilation, nor does it seem to be necessary; so we are cleaning up this dependency. --- packages/angular_devkit/build_angular/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/angular_devkit/build_angular/BUILD.bazel b/packages/angular_devkit/build_angular/BUILD.bazel index 0c3e6d2dffb6..869fc061e0c8 100644 --- a/packages/angular_devkit/build_angular/BUILD.bazel +++ b/packages/angular_devkit/build_angular/BUILD.bazel @@ -305,7 +305,6 @@ ts_project( "//packages/angular_devkit/architect/testing:testing_rjs", "//packages/angular_devkit/core:core_rjs", "//packages/angular_devkit/core/node:node_rjs", - "@npm//rxjs", ], ) From 42856017f762bdb72f9aea3a48d8ba75f6c78928 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Jan 2025 15:43:17 +0000 Subject: [PATCH 7/7] build: switch beasties bundling to `rules_js` Switches the beasties bundling to `rules_js`, using rollup directly from the node modules installation. Notably we are facing a small issue that doesn't cause any issues right now, because rollup tries to dereference symlinks by default given a bug: https://github.com/aspect-build/rules_js/issues/1827. This means we can't rely on the jailed resolution, but in practice it shouldn't cause an issue at this point. --- .../ssr/third_party/beasties/BUILD.bazel | 59 +++++++------------ .../third_party/beasties/rollup.config.mjs | 1 - 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/packages/angular/ssr/third_party/beasties/BUILD.bazel b/packages/angular/ssr/third_party/beasties/BUILD.bazel index b9623ec316ac..e54d156866e9 100644 --- a/packages/angular/ssr/third_party/beasties/BUILD.bazel +++ b/packages/angular/ssr/third_party/beasties/BUILD.bazel @@ -1,5 +1,5 @@ load("@aspect_rules_js//js:defs.bzl", "js_library") -load("@npm//@bazel/rollup:index.bzl", "rollup_bundle") +load("@npm2//:rollup/package_json.bzl", rollup = "bin") package(default_visibility = ["//visibility:public"]) @@ -16,52 +16,33 @@ js_library( js_library( name = "beasties_bundled", srcs = [ - ":bundled_beasties_files", - ], - deps = [ - "//:node_modules/beasties", + ":bundled_beasties", ], ) -# Filter out esbuild metadata files and only copy the necessary files -genrule( - name = "bundled_beasties_files", +rollup.rollup( + name = "bundled_beasties", srcs = [ - ":bundled_beasties", + "rollup.config.mjs", + "//:node_modules/@rollup/plugin-alias", + "//:node_modules/@rollup/plugin-commonjs", + "//:node_modules/@rollup/plugin-node-resolve", + "//:node_modules/beasties", + "//:node_modules/rollup-license-plugin", + "//:node_modules/unenv", ], outs = [ + "THIRD_PARTY_LICENSES.txt", "index.js", "index.js.map", - "THIRD_PARTY_LICENSES.txt", ], - cmd = """ - for f in $(locations :bundled_beasties); do - # Only process files inside the bundled_beasties directory - if [[ "$${f}" == *bundled_beasties ]]; then - cp "$${f}/index.js" $(location :index.js) - cp "$${f}/index.js.map" $(location :index.js.map) - cp "$${f}/THIRD_PARTY_LICENSES.txt" $(location :THIRD_PARTY_LICENSES.txt) - fi - done - """, -) - -rollup_bundle( - name = "bundled_beasties", - config_file = ":rollup.config.mjs", - entry_points = { - "@npm//:node_modules/beasties/dist/index.mjs": "index", - }, - format = "esm", - link_workspace_root = True, - output_dir = True, - sourcemap = "true", - deps = [ - "@npm//@rollup/plugin-alias", - "@npm//@rollup/plugin-commonjs", - "@npm//@rollup/plugin-node-resolve", - "@npm//beasties", - "@npm//rollup-license-plugin", - "@npm//unenv", + args = [ + "--format=esm", + "--config=$(rootpath rollup.config.mjs)", + "--input=node_modules/beasties/dist/index.mjs", + "--sourcemap=true", + "--dir=packages/angular/ssr/third_party/beasties", ], + progress_message = "Bundling beasties", + silent_on_success = False, ) diff --git a/packages/angular/ssr/third_party/beasties/rollup.config.mjs b/packages/angular/ssr/third_party/beasties/rollup.config.mjs index e2283403bc1c..c872fd914aba 100644 --- a/packages/angular/ssr/third_party/beasties/rollup.config.mjs +++ b/packages/angular/ssr/third_party/beasties/rollup.config.mjs @@ -28,7 +28,6 @@ export default { nodeResolve({ preferBuiltins: false, browser: true, - jail: process.cwd(), }), commonjs(), alias({