Skip to content

Comments

fix(compiler-cli): improve error message when non-exported class is used in component imports#67223

Closed
yogeshwaran-c wants to merge 1 commit intoangular:mainfrom
yogeshwaran-c:fix/improve-non-exported-import-error
Closed

fix(compiler-cli): improve error message when non-exported class is used in component imports#67223
yogeshwaran-c wants to merge 1 commit intoangular:mainfrom
yogeshwaran-c:fix/improve-non-exported-import-error

Conversation

@yogeshwaran-c
Copy link

What kind of change does this PR introduce?

Bug fix (improved error message / developer experience)

What is the current behavior?

When a non-exported class is used in a standalone component's imports array, the compiler produces the generic error message:

"Component imports must be standalone components, directives, pipes, or must be NgModules."

This is particularly confusing because the class may have all the correct Angular decorators (e.g., @Directive, @Component, @Pipe) -- the real issue is simply that the class is not exported.

This is especially impactful in the Angular Language Service, which always sets compileNonExportedClasses: false, causing non-exported classes to not be analyzed and leading to misleading diagnostics.

Closes #49874

What is the new behavior?

When a non-exported class is used in imports (or deferredImports), the compiler now produces a more helpful error message:

"The class 'Dir' is used in 'imports' but is not exported from its declaring file. Angular requires imported classes to be exported so that they can be resolved during compilation."

The diagnostic also includes related information pointing to the class declaration with the suggestion:

"Add the 'export' keyword to the class declaration."

If the class IS exported but still unrecognized, the original generic error message is preserved.

Additional context

The fix is in packages/compiler-cli/src/ngtsc/scope/src/util.ts, modifying makeUnknownComponentImportDiagnostic and makeUnknownComponentDeferredImportDiagnostic to check whether the referenced class declaration has the export keyword modifier. A helper function isNonExportedClass was extracted to avoid code duplication.

A test case was added to standalone_spec.ts that uses compileNonExportedClasses: false to simulate the language service behavior.

…sed in component imports

When a class that is not exported is used in a standalone component's
`imports` array, the compiler now produces a more helpful error message
explaining that the class needs to be exported, instead of the generic
"Component imports must be standalone components, directives, pipes, or
must be NgModules" message.

This is especially important for the Angular Language Service which sets
`compileNonExportedClasses: false`, causing non-exported classes to not
be analyzed and resulting in confusing diagnostics.

The new error message includes related information pointing to the class
declaration and suggesting to add the `export` keyword.

Fixes angular#49874
@pullapprove pullapprove bot requested a review from devversion February 23, 2026 19:09
@angular-robot angular-robot bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Feb 23, 2026
@ngbot ngbot bot added this to the Backlog milestone Feb 23, 2026
@devversion devversion requested review from alxhub and removed request for devversion February 23, 2026 19:16
@yogeshwaran-c
Copy link
Author

Closing this PR. Apologies for the low-quality submissions — the changes were not properly tested in the actual application before submitting. I will ensure thorough manual testing before any future contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: compiler Issues related to `ngc`, Angular's template compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Langage service reports an error when Importing a non-exported class in a standalone component.

1 participant