8000 fix(compiler-cli): report more accurate diagnostic for invalid import by crisbeto · Pull Request #60455 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

fix(compiler-cli): report more accurate diagnostic for invalid import #60455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

crisbeto
Copy link
Member

Currently when an incorrect value is in the imports array, we highlight the entire array which can be very noisy for large arrays. This comes up semi-regularly (at least for me) when an import is missing.

These changes add some logic that reports a more accurate diagnostic location for the most common case where the imports array is static. Non-static arrays will fall back to the current behavior.

Example of what I'm referring to:
image

@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Mar 19, 2025
@crisbeto crisbeto requested a review from JoostK March 19, 2025 10:53
@angular-robot angular-robot bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Mar 19, 2025
@ngbot ngbot bot added this to the Backlog milestone Mar 19, 2025
@crisbeto crisbeto requested a review from devversion March 19, 2025 14:08
@@ -132,7 +138,21 @@ export function validateAndFlattenComponentImports(
),
);
} else {
diagnostics.push(createValueHasWrongTypeError(expr, imports, errorMessage).toDiagnostic());
const isStaticArray =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional nit: I wonder if it makes sense to just limit to isArrayExpression(expr) && expr.elements.every(x => ts.isIdentifier(x)) && !imports.some(Array.isArray). I don't know for what else this logic might be true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to account for this case which I think is allowed:

const IMPORTS = [...];

imports: [Foo, IMPORTS];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see. Aren't you excluding that cass below with the some isArray check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I thought your snippet excluded the isArray check but it's actually the length check. I put it in there as a quicker way to exit in case there's a spread.

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 19, 2025
@ngbot
Copy link
ngbot bot commented Mar 19, 2025

I see that you just added the action: merge label, but the following checks are still failing:
    failure status "google-internal-tests" is failing
    pending status "mergeability" is pending
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken main, please try rebasing to main and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

Currently when an incorrect value is in the `imports` array, we highlight the entire array which can be very noisy for large arrays. This comes up semi-regularly (at least for me) when an import is missing.

These changes add some logic that reports a more accurate diagnostic location for the most common case where the `imports` array is static. Non-static arrays will fall back to the current behavior.
@crisbeto crisbeto force-pushed the better-imports-diag branch from eb279d3 to ad0b8ab Compare March 19, 2025 15:34
let diagnosticValue: ResolvedValue;

if (ref instanceof DynamicValue) {
diagnosticNode = ref.node;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without getOriginNodeForDiagnostics I think the following may perhaps become unclear:

const IMPORTS = [HelloDir, InvalidReference];

@Component({ imports: IMPORTS })
export class MyCmp {}

Assuming InvalidReference is a DynamicValue (e.g. because an import is missing/invalid), the diagnostic would be reported within the IMPORTS initializer, separate from the component declaration. This becomes more unclear when IMPORTS is declared in a different source file.

I suspect that using getOriginNodeForDiagnostics would mostly behave the same as the explicit array literal check covers (making it redundant), except for cases where the imports array literal contains a spread element (so possibly still meaningful).

@pkozlowski-opensource
Copy link
Member

This PR was merged into the repository by commit 29eded6.

The changes were merged into the following branches: main, 19.2.x

pkozlowski-opensource pushed a commit that referenced this pull request Mar 19, 2025
…#60455)

Currently when an incorrect value is in the `imports` array, we highlight the entire array which can be very noisy for large arrays. This comes up semi-regularly (at least for me) when an import is missing.

These changes add some logic that reports a more accurate diagnostic location for the most common case where the `imports` array is static. Non-static arrays will fall back to the current behavior.

PR Close #60455
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0