Skip to content

fix(core): auto-resolve duplicate host directive matches instead of throwing NG0309#67760

Closed
splincode wants to merge 1 commit intoangular:mainfrom
splincode:splincode/issues/57846
Closed

fix(core): auto-resolve duplicate host directive matches instead of throwing NG0309#67760
splincode wants to merge 1 commit intoangular:mainfrom
splincode:splincode/issues/57846

Conversation

@splincode
Copy link
Copy Markdown
Contributor

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.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When a directive appeared on an element more than once — either because it was both selector-matched and pulled in as a hostDirective of another matched directive, or because it was referenced multiple times across nested host directive chains — Angular threw a hard runtime error:
NG0309: Directive Foo matches multiple times on the same element. Directives can only match an element once.

Fixes #57846

What is the new behavior?

Angular silently deduplicates instead of throwing. The first occurrence wins and all subsequent occurrences of the same directive on the same element are ignored. The directive is instantiated exactly once, regardless of how many times it appears across selector matches and host directive chains.

This mirrors how providers behave when the same token appears in multiple providers arrays — no error, first registration is kept.

Execution order is preserved: since host directives are always inserted before their host in the directive list, a directive that enters via a hostDirective chain will still execute before the selector-matched directive that hosts it, even if it was also independently selector-matched.

Does this PR introduce a breaking change?

  • Yes
  • No

…hrowing NG0309

When a directive is both selector-matched on an element and referenced as a
hostDirective of another matched directive (or appears multiple times across
nested host directive chains), silently deduplicate it rather than throwing
NG0309. The first occurrence is kept; subsequent ones are ignored — mirroring
how providers handle duplicates.

Fixes angular#57846
@pullapprove pullapprove bot requested a review from thePunderWoman March 19, 2026 12:20
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Mar 19, 2026
@ngbot ngbot bot added this to the Backlog milestone Mar 19, 2026
@JeanMeche JeanMeche requested a review from crisbeto March 19, 2026 12:34
@JeanMeche
Copy link
Copy Markdown
Member

There is already #64132, no need to open duplicate PRs for something that needs some design decisons on our side.

@JeanMeche JeanMeche closed this Mar 19, 2026
@splincode splincode deleted the splincode/issues/57846 branch March 19, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hostDirectives should auto-resolve double matching

2 participants