Skip to content

Commit 1550663

Browse files
devversionatscott
authored andcommitted
fix(bazel): ng_module rule does not expose flat module information in Ivy (angular#36971)
The `ng_module` rule supports the generation of flat module bundles. In View Engine, information about this flat module bundle is exposed as a Bazel provider. This is helpful as other rules like `ng_package` could rely on this information to determine entry-points for the APF. With Ivy this currently does not work because the flat module information is not exposed in the provider. The reason for this is unclear. We should also provide this information in Ivy so that rules like `ng_package` can also determine the correct entry-points when a package is built specifically with `--config=ivy`. PR Close angular#36971
1 parent c40cbec commit 1550663

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

packages/bazel/src/ng_module.bzl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,19 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
354354
"angularCompilerOptions": angular_compiler_options,
355355
})
356356

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

360363
# If we are visiting empty-srcs ts_library, this is a re-export
361364
srcs = ctx.rule.attr.srcs if hasattr(ctx.rule.attr, "srcs") else []
362365

363366
# "re-export" rules should expose all the files of their deps
364367
if not srcs and hasattr(ctx.rule.attr, "deps"):
365368
for dep in ctx.rule.attr.deps:
366-
if (hasattr(dep, "angular")):
369+
if (_has_target_angular_summaries(dep)):
367370
results = depset(dep.angular.summaries, transitive = [results])
368371

369372
return struct(collect_summaries_aspect_result = results)
@@ -608,20 +611,23 @@ def ng_module_impl(ctx, ts_compile_actions):
608611

609612
outs = _expected_outs(ctx)
610613

614+
providers["angular"] = {}
615+
611616
if is_legacy_ngc:
612-
providers["angular"] = {
613-
"summaries": outs.summaries,
614-
"metadata": outs.metadata,
615-
}
617+
providers["angular"]["summaries"] = outs.summaries
618+
providers["angular"]["metadata"] = outs.metadata
616619
providers["ngc_messages"] = outs.i18n_messages
617620

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

622627
providers["angular"]["flat_module_metadata"] = struct(
623628
module_name = ctx.attr.module_name,
624-
metadata_file = outs.metadata[0],
629+
# Metadata files are only generated in the legacy ngc compiler.
630+
metadata_file = outs.metadata[0] if is_legacy_ngc else None,
625631
typings_file = outs.bundle_index_typings,
626632
flat_module_out_file = _flat_module_out_file(ctx),
627633
)

packages/bazel/src/ng_package/packager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ function main(args: string[]): number {
180180

181181
moduleFiles['esm2015_index'] = path.join(binDir, 'esm2015', relative);
182182

183-
// Metadata file is optional as entry-points can be also built
184-
// with the "ts_library" rule.
183+
// Metadata file is optional as entry-points can be built with the `ts_library`
184+
// rule or `ng_module` rule in Ivy mode.
185185
const metadataFile = moduleFiles['metadata'];
186186
if (!metadataFile) {
187187
return;

packages/bazel/test/ng_package/BUILD.bazel

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ jasmine_node_test(
2525
"@npm//@types/shelljs",
2626
"@npm//shelljs",
2727
],
28-
# The "ng_package" rule currently does not properly handle flat module
29-
# bundles which the common package tests rely on. We temporarily disable
30-
# this test until we fix `ng_package` for Ivy. Tracked with: FW-2144.
31-
tags = ["fixme-ivy-aot"],
3228
)
3329

3430
ts_library(
@@ -49,10 +45,6 @@ jasmine_node_test(
4945
"//packages/common:npm_package",
5046
"@npm//shelljs",
5147
],
52-
# The "ng_package" rule currently does not properly handle flat module
53-
# bundles which the common package tests rely on. We temporarily disable
54-
# this test until we fix `ng_package` for Ivy. Tracked with: FW-2144.
55-
tags = ["fixme-ivy-aot"],
5648
)
5749

5850
ts_library(

packages/compiler-cli/integrationtest/bazel/ng_module/extract_flat_module_index.bzl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
def _extract_flat_module_index(ctx):
99
files = []
1010
for dep in ctx.attr.deps:
11-
if hasattr(dep, "angular"):
12-
metadata = dep.angular.flat_module_metadata
13-
files.extend([metadata.metadata_file, metadata.typings_file])
11+
if hasattr(dep, "angular") and hasattr(dep.angular, "flat_module_metadata"):
12+
flat_module = dep.angular.flat_module_metadata
13+
files.append(flat_module.typings_file)
14+
15+
# The flat module metadata file could be `None` for targets
16+
# built with Ivy. No metadata files are generated in ngtsc.
17+
if flat_module.metadata_file != None:
18+
files.append(flat_module.metadata_file)
1419
return [DefaultInfo(files = depset(files))]
1520

1621
extract_flat_module_index = rule(

0 commit comments

Comments
 (0)