Skip to content

Improve DX for identifying missing or unused module imports #41028

Closed as not planned

Description

@Airblader

🚀 feature request

Relevant Package

This feature request is for @angular/core or @angular/compiler (?)

Description

Angular will currently detect (and complain) if a directive with an input binding exists, but the directive is not known (due to its defining module not being imported):

<app-child [myAwesomeDirective]="42"></app-child>

However, this doesn't work for directives without an input binding, i.e.

<app-child myAwesomeDirective></app-child>

The reason for this is – I believe – that myAwesomeDirective is treated as an HTML attribute (which makes sense), and HTML attributes are, well, legal HTML. And custom attributes are specifically frequently used as content projection selectors.

However, this causes a couple of issues in the developer experience, specifically

  1. No error being shown if you forget to import the module.
  2. Making it unsafe to try and identify unused module imports through trial & error of removing them (and there being a lack of alternatives to safely do so).

Describe the solution you'd like

I don't have a specific solution in mind which I think can be considered good enough. Initially I thought about a new strictness setting which fails on unknown HTML attributes, but especially due to the content projection case this would be problematic and a setting I'd only activate for this specific purpose (which feels awkward); in terms of HTML, there's nothing "illegal" about unknown attributes.

I will point out, however, that IDEs such as IntelliJ can and do warn about unknown HTML attributes, even taking content projection into consideration for this. This is somewhat suitable for case (1), though only for code as you write it, not when e.g. moving a component to a different module, and it doesn't cover (2) which is actually my main concern as there's currently no good way of "housekeeping" module imports in a safe, reliable manner. To clarify, this isn't about tree-shaking, unused modules can of course be tree-shaken, but rather about general maintenance and maybe ultimately even allowing to uninstall unused dependencies.

I'm not sure what, if anything, can be reasonably done here, but would like to at least open this for visibility.


To give an example, UI libraries such as Angular Material or ng-zorro-antd require you to import components individually. So you end up importing your buttons, checkboxes, tabs and what have you. But the list only ever grows and can be tough to reduce it down to what is actually needed later on.

Activity

  1. Airblader commented on Feb 27, 2021

    @Airblader
    ContributorAuthor

    Side note, I am aware that removing a module import could also remove a service provided by it, breaking things, so it will probably never be "safe", unfortunately. However, in practice I didn't find that to be a common issue – unlike directives.

  2. added
    feature
    area: language-service
    on Mar 1, 2021
  3. added this to the Backlog milestone on Mar 1, 2021
  4. pauldraper commented on Apr 25, 2021

    @pauldraper
    Contributor

    Would it work to have brackets [myDirective] refer unambiguously to an Angular input/directive?

    The issue is just that [myDirective] currently requires a RHS expression.

  5. Airblader commented on Apr 25, 2021

    @Airblader
    ContributorAuthor

    Do you mean simply allowing [foo] to work without a RHS such that a compile error is raised if the directive is missing?

  6. pauldraper commented on Apr 26, 2021

    @pauldraper
    Contributor

    Yes.

  7. Airblader commented on Apr 26, 2021

    @Airblader
    ContributorAuthor

    That would probably be a relatively simple change to at least enable developers to adopt a style that allows catching this kind of thing. It'd still require doing things a certain way by convention, so it'd be helpful, but not really a solution, either.

  8. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  9. angular-robot commented on Jun 25, 2021

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  10. added
    feature: under consideration
    and removed on Sep 13, 2021
  11. JoostK commented on Sep 13, 2021

    @JoostK
    Member

    Labeling as "under consideration" as I'll be closing #43427 as a duplicate of this one.

  12. alfaproject commented on Jul 17, 2022

    @alfaproject
    Contributor

    I'm now migrating to standalone directives and actually Angular doesn't seem to complain about missing directives with input binding either anymore. I have to grep my code for the directives and then add the import manually to the new standalone ones ):

  13. added
    area: core
    on Jul 18, 2022
  14. robmv commented on May 21, 2024

    @robmv

    Maybe an alternative could be to be able to generate a report at build time of the used directives/components, so they can be analyzed or post processed to identify possible unused dependencies.

  15. s0h311 commented on May 29, 2024

    @s0h311

    @JoostK any news about this?

  16. eneajaho commented on Aug 30, 2024

    @eneajaho
    Contributor

    It's here for standalone components #57605 🎉

  17. JeanMeche commented on Jan 8, 2026

    @JeanMeche
    Member

    I think this has been mentionned in other issues. We don't plan to port #57605 for NgModules as it is way trickier to compute what modules are unused (as NgModule also declare providers).

    If you want that feature, migrate to standalone (there is a migration to assist you with that))

  18. angular-automatic-lock-bot commented on Feb 8, 2026

    @angular-automatic-lock-bot

    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.

  19. locked and limited conversation to collaborators on Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corearea: language-servicecore: directive matchingfeaturefeature: under consideration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions