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
Closed
Conversation
…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
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
importsarray, the compiler produces the generic error message: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(ordeferredImports), the compiler now produces a more helpful error message:The diagnostic also includes related information pointing to the class declaration with the suggestion:
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, modifyingmakeUnknownComponentImportDiagnosticandmakeUnknownComponentDeferredImportDiagnosticto check whether the referenced class declaration has theexportkeyword modifier. A helper functionisNonExportedClasswas extracted to avoid code duplication.A test case was added to
standalone_spec.tsthat usescompileNonExportedClasses: falseto simulate the language service behavior.