Skip to content

Unused imports diagnostics for compound arrays without NgModules #59754

Open

Description

@dmitry-stepanenko

Command

build

Description

Recently the new flag "unusedStandaloneImports" was added that detects unused standalone imports in the given standalone components.

It works just fine, however there're quite a lot of use cases where modules are applicable even along with standalone components, e.g. complex UI packages like @angular/material/tabs, thus having the diagnostics to check whether such modules are used is also highly valuable.

I understand that resolving all dependencies of a module is much more computationally heavy operation in comparison with standalone components, but I can still see it being useful as an optional flag that can be enabled only on CI or to cleanup such imports manually from time to time.

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Activity

  1. transferred this issue fromangular/angular-clion Jan 28, 2025
  2. added this to the needsTriage milestone on Jan 28, 2025
  3. crisbeto commented on Jan 28, 2025

    @crisbeto
    Member

    The problem with modules is that they can bring in providers, in addition to declarations.

  4. JoostK commented on Jan 29, 2025

    @JoostK
    Member

    As @crisbeto mentions, NgModules play a role in DI more so than standalone components. We could look for standalone components that are present in @NgModule.imports to determine if they are used, but this would have to consider any of the declarations of the NgModule (making it a bit more complicated) and we're generally steering in the direction of standalone components, so there's no plans at this time to introduce additional checks for NgModule setups.

  5. dmitry-stepanenko commented on Jan 30, 2025

    @dmitry-stepanenko
    Author

    @JoostK

    and we're generally steering in the direction of standalone components

    Understandable, given that are there any recommendations from the team how do you see imports of the large features without modules (e.g. the @angular/material/tabs that I've mentioned earlier) ? Just a destructed array of components won't really work because some of them may not be used in a certain scenario and importing every bit manually is sometimes too verbose

  6. JoostK commented on Jan 30, 2025

    @JoostK
    Member

    Thinking about this more, I realize there's quite a few variations surrounding this topic.

    1. Standalone components with an NgModule in its imports array.
    2. Standalone components with a reference to an array of components in its imports array.
    3. NgModule components with NgModule.imports that isn't used.

    My initial interpretation of this request was concerning 3), but I see now it's more about 1). In that case it's indeed DI that gets in the way, as the compiler doesn't know whether an NgModule or its transitive imports registers any providers.

    For 2) it is already possible to declare an array of component references and refer to that in Component.imports. However, this situation is currently exempt from the detection as it is assumed that the array exists for sharing purposes (iff the array is exported). This assumption is fair, but it misses the detection of a reference to such array where none of its contents are being used. I think that is something that could be achieved, with the constraint that the array could still not contain NgModules (because of DI).

    So I do think there is a possibility to extend the unused directive checks, but interestingly that would still exclude NgModules.

  7. dmitry-stepanenko commented on Jan 30, 2025

    @dmitry-stepanenko
    Author

    Yes @JoostK that's exactly what I meant, probably my original description wasn't fully clear. If there would be a possibility to import an array of standalone components as a const and verify if at least one of them is used within a given standalone component, that would really help much.

    Currently we're using a lot modules with { imports: [...components], exports: [...components] } in the metadata, so I guess those can easily be changed to the direct import of components constant.

  8. dmitry-stepanenko commented on Jan 31, 2025

    @dmitry-stepanenko
    Author

    @JoostK taking into account our discussion above, would you like to reopen this ticket?

  9. reopened this on Jan 31, 2025
  10. changed the title [-]Unused imports diagnostics for modules[/-] [+]Unused imports diagnostics for compound arrays without NgModules[/+] on Jan 31, 2025
  11. JoostK commented on Jan 31, 2025

    @JoostK
    Member

    I have, but with a different scope. Only scenario 2 (from my earlier comment) can be detected, the scenario this was originally requested to be covered cannot (at least not fully safely)

  12. added a commit that references this issue on Jan 17, 2026
    38eb438
  13. JeanMeche commented on Jan 18, 2026

    @JeanMeche
    Member

    I'm wondering if such a diagnostic really makes sense.
    The common use case I see is a library exposing the array in addition to each individiual directive/component.

    Users who reach out to this array probably don't care about adding each individual directive but this will still be flagged by the diagnostic.

  14. JoostK commented on Jan 19, 2026

    @JoostK
    Member

    Users who reach out to this array probably don't care about adding each individual directive but this will still be flagged by the diagnostic.

    This isn't the intent behind this request, at least not what I described as 2) in #59754 (comment). The rule ought to flag references to arrays where none of the array's elements are used. Note that #66620 doesn't actually implement this rule, it is different in semantics and I haven't determined yet if its suggestion could be considered meaninful as well.

  15. JeanMeche commented on Jan 19, 2026

    @JeanMeche
    Member

    Oh okay, I was indeed mislead by #66620. In that case, yes what's we're discussing here makes sense.

  16. added a commit that references this issue on Jan 19, 2026
    74ca851
  17. added a commit that references this issue on Oct 8, 2026
    775b6c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      Sponsor
      SponsoredKunjungi sekarang
      Promo