Skip to content

build: apply angular bazel ng_module fix to unblock framework #19276

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

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
57 changes: 57 additions & 0 deletions tools/postinstall/@angular_bazel_ivy_flat_module.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git node_modules/@angular/bazel/src/ng_module.bzl node_modules/@angular/bazel/src/ng_module.bzl
index 9480c4b679dc..cf6f8ebaf437 100644
--- node_modules/@angular/bazel/src/ng_module.bzl
+++ node_modules/@angular/bazel/src/ng_module.bzl
@@ -334,8 +334,11 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
"angularCompilerOptions": angular_compiler_options,
})

+def _has_target_angular_summaries(target):
+ return hasattr(target, "angular") and hasattr(target.angular, "summaries")
+
def _collect_summaries_aspect_impl(target, ctx):
- results = depset(target.angular.summaries if hasattr(target, "angular") else [])
+ results = depset(target.angular.summaries if _has_target_angular_summaries(target) else [])

# If we are visiting empty-srcs ts_library, this is a re-export
srcs = ctx.rule.attr.srcs if hasattr(ctx.rule.attr, "srcs") else []
@@ -343,7 +346,7 @@ def _collect_summaries_aspect_impl(target, ctx):
# "re-export" rules should expose all the files of their deps
if not srcs and hasattr(ctx.rule.attr, "deps"):
for dep in ctx.rule.attr.deps:
- if (hasattr(dep, "angular")):
+ if (_has_target_angular_summaries(dep)):
results = depset(dep.angular.summaries, transitive = [results])

return struct(collect_summaries_aspect_result = results)
@@ -588,20 +591,23 @@ def ng_module_impl(ctx, ts_compile_actions):

outs = _expected_outs(ctx)

+ providers["angular"] = {}
+
if is_legacy_ngc:
- providers["angular"] = {
- "summaries": outs.summaries,
- "metadata": outs.metadata,
- }
+ providers["angular"]["summaries"] = outs.summaries
+ providers["angular"]["metadata"] = outs.metadata
providers["ngc_messages"] = outs.i18n_messages

- if is_legacy_ngc and _should_produce_flat_module_outs(ctx):
- if len(outs.metadata) > 1:
+ if _should_produce_flat_module_outs(ctx):
+ # Sanity error if more than one metadata file has been created in the
+ # legacy ngc compiler while a flat module should be produced.
+ if is_legacy_ngc and len(outs.metadata) > 1:
fail("expecting exactly one metadata output for " + str(ctx.label))

providers["angular"]["flat_module_metadata"] = struct(
module_name = ctx.attr.module_name,
- metadata_file = outs.metadata[0],
+ # Metadata files are only generated in the legacy ngc compiler.
+ metadata_file = outs.metadata[0] if is_legacy_ngc else None,
typings_file = outs.bundle_index_typings,
flat_module_out_file = _flat_module_out_file(ctx),
)
23 changes: 16 additions & 7 deletions tools/postinstall/apply-patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fs = require('fs');
* Version of the post install patch. Needs to be incremented when
* existing patches or edits have been modified.
*/
const PATCH_VERSION = 5;
const PATCH_VERSION = 6;

/** Path to the project directory. */
const projectDir = path.join(__dirname, '../..');
Expand Down Expand Up @@ -72,13 +72,13 @@ searchAndReplace(
`$1#\n$2results = depset(dep.angular.metadata, transitive = [results])`,
'node_modules/@angular/bazel/src/ng_module.bzl');
searchAndReplace(
/^((\s*)results = depset\(target.angular.summaries if hasattr\(target, "angular"\) else \[]\))$/m,
`$1#\n$2results = depset(target.angular.metadata if hasattr(target, "angular") else [], transitive = [results])`,
/^((\s*)results = depset\(target.angular\.summaries if _has_target_angular_summaries\(target\) else \[]\))$/m,
`$1#\n$2results = depset(target.angular.metadata if _has_target_angular_summaries(target) else [], transitive = [results])`,
'node_modules/@angular/bazel/src/ng_module.bzl');
// Ensure that "metadata" of transitive dependencies can be collected.
searchAndReplace(
/("metadata": outs.metadata),/,
`$1 + [m for dep in ctx.attr.deps if hasattr(dep, "angular") for m in dep.angular.metadata],`,
/providers\["angular"]\["metadata"] = outs\.metadata/,
`$& + [m for dep in ctx.attr.deps if (hasattr(dep, "angular") and hasattr(dep.angular, "metadata")) for m in dep.angular.metadata]`,
'node_modules/@angular/bazel/src/ng_module.bzl');

// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1208.
Expand All @@ -89,7 +89,15 @@ try {
// Can be removed once @angular/bazel is updated here to include this patch.
// try/catch needed for this the material CI tests to work in angular/repo
applyPatch(path.join(__dirname, './@angular_bazel_ng_module.patch'));
} catch (_) {}
} catch {}

try {
// Temporary patch pre-req for https://github.com/angular/angular/pull/36971.
// Can be removed once @angular/bazel is updated here to include this patch.
// try/catch needed for this as the framework repo has this patch already applied,
// and re-applying again causes an error.
applyPatch(path.join(__dirname, './@angular_bazel_ivy_flat_module.patch'));
} catch {}

// Workaround for https://github.com/angular/angular/issues/33452:
searchAndReplace(/angular_compiler_options = {/, `$&
Expand Down Expand Up @@ -177,7 +185,8 @@ function searchAndReplace(search, replacement, relativeFilePath) {
fileEdits.push(originalContent => {
const newFileContent = originalContent.replace(search, replacement);
if (originalContent === newFileContent) {
throw Error(`Could not perform replacement in: ${filePath}.`);
throw Error(`Could not perform replacement in: ${filePath}.\n` +
`Searched for pattern: ${search}`);
}
return newFileContent;
});
Expand Down