Skip to content
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

Forbid orphan components #52061

Closed
wants to merge 6 commits into from
Closed

Conversation

pmvald
Copy link
Member

@pmvald pmvald commented Oct 5, 2023

This PR adds an option to Angular compiler options which allow to produce runtime error if orphan components found. Good for identifying such cases.

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: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Oct 5, 2023
@pmvald pmvald changed the title Forbid orphan comp Forbid orphan components Oct 6, 2023
@pmvald pmvald added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime area: compiler Issues related to `ngc`, Angular's template compiler core: error handling target: minor This PR is targeted for the next minor release PullApprove: disable labels Oct 6, 2023
@ngbot ngbot bot modified the milestone: Backlog Oct 6, 2023
@pmvald pmvald marked this pull request as ready for review October 6, 2023 04:08
Copy link
Member

@devversion devversion left a comment

Choose a reason for hiding this comment

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

Some initial comments.

packages/router/src/state_manager.ts Show resolved Hide resolved
@@ -163,6 +164,17 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
injector: Injector, projectableNodes?: any[][]|undefined, rootSelectorOrNode?: any,
environmentInjector?: NgModuleRef<any>|EnvironmentInjector|
undefined): AbstractComponentRef<T> {
// Check if the component is orphan
if (ngDevMode && this.componentDef.debugInfo?.forbidOrphanRendering) {
if (depsTracker.isOrphanComponent(this.componentType)) {
Copy link
Member

Choose a reason for hiding this comment

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

There is an interesting scenario we should chat about (related to our ngDevMode/jitMode conversations). If ngJitMode is false but ngDevMode is true, this will currently always yield an error IIRC because setNgModuleScope would never be invoked?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yeah, correct. For now I added ngJitMode to this if statement. Also in the very first commit I added a check to ensure that the option forbidOrphanComponents is not set without jit mode (i.e., option supportJitMode). But these are just temporary solution, and it seems we need a better flag system. I'll write design doc soon on this. My quick thought is we may need a ngDebugMode flag as well which causes more verbose and runtime checks to pop in (unless we want to redefine ngDevMode to do this, but seems like this flag is a bit limited IIRC)

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. My first thought is that ngDebugMode should not exist and ngDevMode should be sufficient. We are not adding any super verbose logging/debugging as far as I know

packages/core/src/render3/component_ref.ts Outdated Show resolved Hide resolved
@pmvald pmvald force-pushed the forbid-orphan-comp branch 2 times, most recently from 9114f48 to a539f4a Compare October 8, 2023 21:15
@pmvald pmvald requested a review from devversion October 8, 2023 22:06
…orphan component

Orphan component is an anti-pattern in Angular where a component is rendered while the NgModule declaring it is not installed. It is not easy to capture this scenario, specially in compile time. But it is possible to capture a special case in runtime where the component is being rendered without its NgModule even loaded into the browser. This change adds a flag in cli compiler option to enable such checking, and throwing a runtime exception if it happens. Note that such check is only done in dev mode.

Currently the check requires some generated code that is behind ngJitMode flag (i.e., call to ɵɵsetNgModuleScope), and the new flag can be set only if JIT mode is enabled (i.e., supportJitMode=true) otherwise an error will be thrown.

The orphan component is a main blocker for rolling out local compilation in g3. This option is needed for identifying and isolating such cases.
…mponent's debug info

A new flag added to the component's debug info to determine whether to throw runtime error (in dev mode) if component is being rendered without its NgModule. This flag is only set for non-standalone components.
The flag `forbidOrphanRendering` is only set for non-standalone components, and indicates that the dev mode runtime should through error if the component is rendered without its ngModule loaded in the browser. This runtime error can help with further debugging.
… orphan

A new method `isOrphanComponent` is added to the deps tracker API to check if the NgModule declaring this component, if exists, is loaded into the browser.
A runtime error will be thrown if a non-standalone component is being rendered without its NgModule loaded in the browser. This error is thrown only in dev mode and only if the Angular option `forbidOrphanComponents` is set to true. The error contains useful info to find the orphan component in the source code.
Now users can configure the option `forbidOrphanComponents` in the tsconfig's angularCompilerOptions part.
@pmvald pmvald added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 10, 2023
@pmvald
Copy link
Member Author

pmvald commented Oct 10, 2023

Caretaker note: the ci/test failed due to //packages/core/test/bundling/defer:symbol_test which has been flaky these days. But it is fixed this morning and once rebased, this PR should pass that as well.

@pmvald pmvald added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 10, 2023
@atscott
Copy link
Contributor

atscott commented Oct 10, 2023

This PR was merged into the repository by commit 59ba2a6.

@atscott atscott closed this in e8201a5 Oct 10, 2023
atscott pushed a commit that referenced this pull request Oct 10, 2023
…mponent's debug info (#52061)

A new flag added to the component's debug info to determine whether to throw runtime error (in dev mode) if component is being rendered without its NgModule. This flag is only set for non-standalone components.

PR Close #52061
atscott pushed a commit that referenced this pull request Oct 10, 2023
#52061)

The flag `forbidOrphanRendering` is only set for non-standalone components, and indicates that the dev mode runtime should through error if the component is rendered without its ngModule loaded in the browser. This runtime error can help with further debugging.

PR Close #52061
atscott pushed a commit that referenced this pull request Oct 10, 2023
… orphan (#52061)

A new method `isOrphanComponent` is added to the deps tracker API to check if the NgModule declaring this component, if exists, is loaded into the browser.

PR Close #52061
atscott pushed a commit that referenced this pull request Oct 10, 2023
A runtime error will be thrown if a non-standalone component is being rendered without its NgModule loaded in the browser. This error is thrown only in dev mode and only if the Angular option `forbidOrphanComponents` is set to true. The error contains useful info to find the orphan component in the source code.

PR Close #52061
atscott pushed a commit that referenced this pull request Oct 10, 2023
Now users can configure the option `forbidOrphanComponents` in the tsconfig's angularCompilerOptions part.

PR Close #52061
@angular-automatic-lock-bot
Copy link

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 Nov 11, 2023
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…orphan component (angular#52061)

Orphan component is an anti-pattern in Angular where a component is rendered while the NgModule declaring it is not installed. It is not easy to capture this scenario, specially in compile time. But it is possible to capture a special case in runtime where the component is being rendered without its NgModule even loaded into the browser. This change adds a flag in cli compiler option to enable such checking, and throwing a runtime exception if it happens. Note that such check is only done in dev mode.

Currently the check requires some generated code that is behind ngJitMode flag (i.e., call to ɵɵsetNgModuleScope), and the new flag can be set only if JIT mode is enabled (i.e., supportJitMode=true) otherwise an error will be thrown.

The orphan component is a main blocker for rolling out local compilation in g3. This option is needed for identifying and isolating such cases.

PR Close angular#52061
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…mponent's debug info (angular#52061)

A new flag added to the component's debug info to determine whether to throw runtime error (in dev mode) if component is being rendered without its NgModule. This flag is only set for non-standalone components.

PR Close angular#52061
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
angular#52061)

The flag `forbidOrphanRendering` is only set for non-standalone components, and indicates that the dev mode runtime should through error if the component is rendered without its ngModule loaded in the browser. This runtime error can help with further debugging.

PR Close angular#52061
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
… orphan (angular#52061)

A new method `isOrphanComponent` is added to the deps tracker API to check if the NgModule declaring this component, if exists, is loaded into the browser.

PR Close angular#52061
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…r#52061)

A runtime error will be thrown if a non-standalone component is being rendered without its NgModule loaded in the browser. This error is thrown only in dev mode and only if the Angular option `forbidOrphanComponents` is set to true. The error contains useful info to find the orphan component in the source code.

PR Close angular#52061
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…ar#52061)

Now users can configure the option `forbidOrphanComponents` in the tsconfig's angularCompilerOptions part.

PR Close angular#52061
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 area: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime core: error handling detected: feature PR contains a feature commit merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note PullApprove: disable target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants