Skip to content

Adds support for ng_module() to support compilations with warnings - #43582

Closed
dgp1130 wants to merge 2 commits into
angular:masterfrom
dgp1130:emit-mf
Closed

dgp1130 wants to merge 2 commits into
angular:masterfrom
dgp1130:emit-mf

Conversation

@dgp1130

@dgp1130 dgp1130 commented Sep 25, 2021 •

Copy link
Copy Markdown
Contributor

ngc_wrapped currently fails when given a compilation that emits only warnings. It doesn't generate the output .es5.MF file which Bazel interprets as a hard failure. This PR updates ngc_wrapped to emit the manifest as long as none of the diagnostics are errors. This allows it to support a compilation that emits some warnings but no errors and pass the build.

I added strict_templates to ng_module(), mostly in line with how we do this internally. I also added a new experimental_extended_template_diagnostics attribute which enables _extendedTemplateDiagnostics as well which gives a warning we can test with. For now, there is one target which enables this and contains an invalid banana in box, which is shown as a warning but most importantly does not fail the build. I wanted to add a second ng_module() target with expected_diagnostics to confirm that the banana in box warning is actually emitted, but it doesn't seem to have any effect and may not be supported. I left out that test for now.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix (ngc_wrapped no longer fails on compilations with warnings).
  • Feature (ng_module() now supports strict_templates)

What is the current behavior?

ngc_wrapped does not generate an .es5.MF file when the compilation returns warnings and no errors. This fails ng_module() because Bazel requires that all output files are generated.

Issue Number: b/199443160

What is the new behavior?

ngc_wrapped now generates the .es5.MF file as long as no errors are emitted. ng_module() now builds successfully when warnings are emitted.

Does this PR introduce a breaking change?

  • No

Other information

This PR is slightly awkward for versioning because it's really a bugfix for ngc_wrapped but also happens to implement a feature for ng_module() in order to test it. ng_module() is publicly exported, so strict_templates is usable. Extended template diagnostics have the experimental prefix to be more clear that this is intended as an internal option and not publicly supported yet.

/cc @atscott

@dgp1130 dgp1130 added the target: minor This PR is targeted for the next minor release label Sep 25, 2021
@dgp1130
dgp1130 requested a review from alxhub September 25, 2021 02:39
@google-cla google-cla Bot added the cla: yes label Sep 25, 2021
@pullapprove
pullapprove Bot requested a review from IgorMinar September 25, 2021 02:39
@dgp1130
dgp1130 force-pushed the emit-mf branch 2 times, most recently from 7068aab to 36acea3 Compare September 25, 2021 02:50
@dgp1130

dgp1130 commented Sep 28, 2021

Copy link
Copy Markdown
Contributor Author

Things are mostly looking good here, but it seems that this breaks the components repository.

ERROR: /tmp/angular-components-repo/src/cdk/bidi/BUILD.bazel:11:10: in ng_module rule //src/cdk/bidi:bidi: 
Traceback (most recent call last):
	File "/home/circleci/.cache/bazel/_bazel_circleci/a762c3cf80bc91e4f82b925d9aef0a0c/external/npm/@angular/bazel/src/ng_module.bzl", line 683, column 34, in _ng_module_impl
		ts_providers = ng_module_impl(ctx, compile_ts)
	File "/home/circleci/.cache/bazel/_bazel_circleci/a762c3cf80bc91e4f82b925d9aef0a0c/external/npm/@angular/bazel/src/ng_module.bzl", line 645, column 35, in ng_module_impl
		providers = ts_compile_actions(
	File "/home/circleci/.cache/bazel/_bazel_circleci/a762c3cf80bc91e4f82b925d9aef0a0c/external/npm/@bazel/typescript/internal/common/compilation.bzl", line 311, column 40, in compile_ts
		tsconfig_es6 = tsc_wrapped_tsconfig(
	File "/home/circleci/.cache/bazel/_bazel_circleci/a762c3cf80bc91e4f82b925d9aef0a0c/external/npm/@angular/bazel/src/ng_module.bzl", line 357, column 26, in _ngc_tsconfig
		"strictTemplates": ctx.attr.strict_templates,
Error: dictionary expression has duplicate key: "strictTemplates"

Apparently it already has a hack to enable strictTemplates and adding the feature to ng_module() breaks it due to duplicated strictTemplates keys in the tsconfig.json.

The fix is pretty straightforward, just delete the components patch and update //tools:defaults.bzl's ng_module() wrapper to always set strict_templates = True. Unfortunately I'm not sure how to safely land that change without an intermediate breakage or regression.

My only other thought is to update ng_module() to add the key if strict_templates = True, that way it would be absent if strict_templates = False, which would enable the patch to continue working. It's a hacky workaround, but avoids a breakage in components. I'll need to follow up after this PR lands to:

  • Remove the strict templates patch in components and switch to the new strict_templates attribute on ng_module().
  • Refactor FW to always add strictTemplates: true when strict_templates == True, which is the more intuitive way of writing this.

…te_diagnostics` to `ng_module()` rule

Refs angular#42966.
Fixes angular#33452.

This allows `ng_module()` targets to be built with strict templates enabled, it mostly works the way we already do this internally. Also adds extended template diagnostics behind an experimental option so it can be used internally and for tests.

`strict_templates` can only be used if `type_check` is also enabled and `experimental_extended_template_diagnostics` can only be used if `strict_templates` is enabled.
Refs angular#42966.

Previously if _any_ diagnostics were emitted, regardless of their category, the manifest would not be generated. This means that if a target emits only warnings and no errors, it would still fail to build because it does not generate all the required output files (specifically the `.es5.MF` file). Now the manifest file is generated as long as there are no error diagnostics in the result. This makes `ng_module()` support compiler warnings as a user would expect.

Added a test which uses extended template diagnostics to trigger the invalid banana in box diagnostic. This generates a warning and uses Skylib's `build_test()` to verify that it builds successfully. Unfortunately there is no easy way to verify that the warning diagnostic is emitted at all. `expected_diagnostics` should be able to do that, but it doesn't seem to have any effect on `ng_module()` and may not be integrated. Instead, testing that a target with warnings builds correctly is the best we can easily do here without a deeper investigation.
@dgp1130

dgp1130 commented Sep 28, 2021

Copy link
Copy Markdown
Contributor Author

This PR should also close #33452 since it adds strict_templates to ng_module().

@IgorMinar IgorMinar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!

Reviewed-for: bazel, dev-infra

@dgp1130 dgp1130 added the action: merge The PR is ready for merge by the caretaker label Sep 29, 2021
@alxhub alxhub closed this in 62d7005 Sep 29, 2021
alxhub pushed a commit that referenced this pull request Sep 29, 2021
…43582)

Refs #42966.

Previously if _any_ diagnostics were emitted, regardless of their category, the manifest would not be generated. This means that if a target emits only warnings and no errors, it would still fail to build because it does not generate all the required output files (specifically the `.es5.MF` file). Now the manifest file is generated as long as there are no error diagnostics in the result. This makes `ng_module()` support compiler warnings as a user would expect.

Added a test which uses extended template diagnostics to trigger the invalid banana in box diagnostic. This generates a warning and uses Skylib's `build_test()` to verify that it builds successfully. Unfortunately there is no easy way to verify that the warning diagnostic is emitted at all. `expected_diagnostics` should be able to do that, but it doesn't seem to have any effect on `ng_module()` and may not be integrated. Instead, testing that a target with warnings builds correctly is the best we can easily do here without a deeper investigation.

PR Close #43582
@dgp1130
dgp1130 deleted the emit-mf branch September 29, 2021 17:05
dgp1130 added a commit to dgp1130/components that referenced this pull request Oct 1, 2021
…g_module()` and remove local workaround

Refs: angular/angular#33452.

This workaround is no longer necessary since `ng_module()` actually supports strict templates now (see angular/angular#43582).
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants