Skip to content

fix(ngcc): use annotateForClosureCompiler option#36652

Closed
DavidANeil wants to merge 1 commit into
angular:masterfrom
lucidsoftware:davidneil-ngcc-closure-compat
Closed

fix(ngcc): use annotateForClosureCompiler option#36652
DavidANeil wants to merge 1 commit into
angular:masterfrom
lucidsoftware:davidneil-ngcc-closure-compat

Conversation

@DavidANeil

Copy link
Copy Markdown

Adds @nocollapse to static properties added by ngcc iff annotateForClosureCompiler is true.

Resolves #36618.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #36618

What is the new behavior?

When ngcc is invoked with a tsconfig that contains the option "annotateForClosureCompiler": true then ngcc will output closure compatible javascript.

Does this PR introduce a breaking change?

  • No

@pullapprove pullapprove Bot requested a review from alxhub April 16, 2020 08:04
@JoostK JoostK requested review from JoostK and removed request for alxhub April 16, 2020 09:00
@JoostK JoostK added comp: ngcc freq1: low action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release risk: low type: bug/fix labels Apr 16, 2020
@ngbot ngbot Bot added this to the needsTriage milestone Apr 16, 2020

@JoostK JoostK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for looking into this and opening a PR to resolve the issue! 🎉

It looks to me like we don't have to make the changes to TraitCompiler, CompileResult, WritePropExpr and ExpressionTranslatorVisitor, instead keeping the necessary changes local to ngcc's renderer.

My idea is to only change Renderer.renderDefinitions/Renderer.renderStatements such that the @nocollapse comment is added there, if annotateForClosureCompiler is enabled. This is similar to ngtsc's approach of adding the comment in a TypeScript transformer, which would be the renderer in ngcc's case.

@DavidANeil

Copy link
Copy Markdown
Author

My idea is to only change Renderer.renderDefinitions/Renderer.renderStatements such that the @nocollapse comment is added there, if annotateForClosureCompiler is enabled. This is similar to ngtsc's approach of adding the comment in a TypeScript transformer, which would be the renderer in ngcc's case.

Thanks for the quick feedback @JoostK!
How are you imagining that transform to happen? The renderer just calls

this.srcFormatter.printStatement(stmt, sourceFile, imports);

Which goes all the way from Statement (which you are wanting to not change) to string. It could be possible to just prepend the @nocollapse to the string, but that seems hacky at best.
So should we just figure out which type of stmt need these added in the renderer?
To make it that Statement supports leading comments would still require these changes in ExpressionTranslatorVisitor, and I'm afraid that by the time we get to printing it is much more ambiguous which statements even need the @nocollapse annotation. At the very best we would have to do something like

if (this.annotateForClosureCompiler && stmt.expr instanceof WritePropExpr && someEsotericCheckThatThisIsAStaticPropertyAndNotAnyOtherKind(stmt)) {
    stmt.expr.leadingComment = '* @nocollapse ';
}
this.srcFormatter.printStatement(stmt, sourceFile, imports);

Which still requires the changes in WritePropExpr and visitWritePropExpr.


While writing the above I tried implementing it and was unsuccessful. I'm happy to be convinced that there is a better way, I'm just not sure I'm knowledgeable enough already to be able to implement that way.
I'm happy to collaborate on this PR with commits authored by others.

@JoostK

JoostK commented Apr 16, 2020

Copy link
Copy Markdown
Member

Thanks for the reply. I agree that tacking on the comment to a string would be a hack, but I see some other ways:

  1. Emit a o.CommentStmt in front of a definition statement in createAssignmentStatement (would need to start returning an array). This would require updating ExpressionTranslatorVisitor as support for that statement kind is not currently implemented.

  2. Introduce a new type in ngcc to represent a statement, which can have comments associated with it. RendererFormatter.printStatement can then take this new type and emit synthetic comments as desired. Something like this:

export type EmitStatement {
  stmt: o.Statement;
  comments: string[];
}

I'm not sold on the name but I can't think of something better

  1. Change RendererFormatter.printStatement such that it accepts the leadingComment you added to WritePropExpr. The implementations can then add the comment synthetically to the ts.Node before printing it as a string.
    This is still a bit of a hack.

My personal preference would be in the order as presented above. If you run into problems feel free to follow up here.

@DavidANeil

Copy link
Copy Markdown
Author

I should be able to get to this toward the end of next week.

@petebacondarwin

Copy link
Copy Markdown
Contributor

@DavidANeil - do you still have time to work on this?

@petebacondarwin petebacondarwin added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 19, 2020
@DavidANeil

Copy link
Copy Markdown
Author

Yup sorry for the radio silence, my plan was to come back to it as soon as I enabled Ivy for our code base, and I thought that "the end of next week" would be a good estimate for when that would be done, but it turns out "the end of next month" was probably a more accurate estimate. I think I am about 3/4 done with enabling Ivy, so I should be circling back to this in a week or so?

Sorry I can't get to it sooner.

@petebacondarwin

Copy link
Copy Markdown
Contributor

No problem. Good luck with your ivy-ifying of your code.

@IgorMinar

Copy link
Copy Markdown
Contributor

Let's discuss the overall "Angular + Closure Compiler outside of google3" story before we start adding more flags and closure support into ngc and especially ngcc.

I have a meeting with @DavidANeil scheduled for next week, so I'll bring up this PR there and will post an update on this PR afterwards.

@DavidANeil

Copy link
Copy Markdown
Author

Sounds good, that's what I was thinking too.

I just wanted to get this PR into a better state before I entirely forgot my mental model of the ngcc renderer.

@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.

After discussing the build setup with @DavidANeil I think that this is a reasonable change to make.

@DavidANeil can you please update this PR with:

  • updat the commit message to explain "The Why" - the motivation behind this change
  • rebase the PR (it should make the saucelabs flake go away)

LGTM otherwise. Thanks!

The rationale behind this decision is concistency. ngc already supports the flag, and ngcc exists only to convert code from VE to Ivy, so there is no good reason for ngcc not to support this flag - it doesn't increase our support burden or feature set. In fact one could argue that this change fixes a regression caused by the Ivy and ngcc rollout in v9.

@IgorMinar IgorMinar requested a review from JoostK June 9, 2020 18:04

@JoostK JoostK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I had completely missed the refactored commit, thanks for the cleanup!

Also, the linter is complaining about the commit message being too short (needs to be 100 chars at least). Could you also change Resolves #36618 into Closes #36618, that's the term we typically use.

One small nit about an import, otherwise LGTM!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you move this import to @angular/compiler above?

Adds @nocollapse to static properties added by ngcc
iff annotateForClosureCompiler is true.

The Closure Compiler will collapse static properties
into the global namespace.  Adding this annotation keeps
the properties attached to their respective object, which
allows them to be referenced via a class's constructor.
The annotation is already added by ngtsc and ngc under the
same option, this commit extends the functionality to ngcc.

Closes angular#36618.
@DavidANeil DavidANeil force-pushed the davidneil-ngcc-closure-compat branch from cab05e3 to fe84683 Compare June 10, 2020 03:43
@JoostK JoostK added action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jun 10, 2020
@AndrewKushnir

Copy link
Copy Markdown
Contributor

Presubmit

mhevery pushed a commit that referenced this pull request Jun 11, 2020
Adds @nocollapse to static properties added by ngcc
iff annotateForClosureCompiler is true.

The Closure Compiler will collapse static properties
into the global namespace.  Adding this annotation keeps
the properties attached to their respective object, which
allows them to be referenced via a class's constructor.
The annotation is already added by ngtsc and ngc under the
same option, this commit extends the functionality to ngcc.

Closes #36618.

PR Close #36652
@mhevery mhevery closed this in 8c682c5 Jun 11, 2020
ngwattcos pushed a commit to ngwattcos/angular that referenced this pull request Jun 25, 2020
Adds @nocollapse to static properties added by ngcc
iff annotateForClosureCompiler is true.

The Closure Compiler will collapse static properties
into the global namespace.  Adding this annotation keeps
the properties attached to their respective object, which
allows them to be referenced via a class's constructor.
The annotation is already added by ngtsc and ngc under the
same option, this commit extends the functionality to ngcc.

Closes angular#36618.

PR Close angular#36652
@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 Jul 12, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
Adds @nocollapse to static properties added by ngcc
iff annotateForClosureCompiler is true.

The Closure Compiler will collapse static properties
into the global namespace.  Adding this annotation keeps
the properties attached to their respective object, which
allows them to be referenced via a class's constructor.
The annotation is already added by ngtsc and ngc under the
same option, this commit extends the functionality to ngcc.

Closes angular#36618.

PR Close angular#36652
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 action: presubmit The PR is in need of a google3 presubmit cla: yes freq1: low risk: low target: patch This PR is targeted for the next patch release type: bug/fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

platformBrowser().bootstrapModule does not work under Closure Compiler ADVANCED_OPTIMIZATIONS

6 participants