Skip to content

fix(core/schematics): skip migration for non-type-safe tokens in inject-migration and log warnings - #61999

Closed
aliabodaraa wants to merge 1 commit into
angular:mainfrom
aliabodaraa:fix/inject-migration-string-token
Closed

aliabodaraa wants to merge 1 commit into
angular:mainfrom
aliabodaraa:fix/inject-migration-string-token

Conversation

@aliabodaraa

@aliabodaraa aliabodaraa commented Jun 11, 2025 •

Copy link
Copy Markdown

Summary

This PR updates the inject migration logic to skip non-type-safe tokens (like string literals and variable/import references) and instead insert inline warning comments to guide developers to replace them with InjectionToken instances. This avoids invalid automatic transformations that could lead to compile-time errors.

Issue

Fixes #61982

Details

  • When the migration encounters a string literal token (e.g., 'myToken'), it now adds a comment like:
    // WARNING: String token 'myToken' found. Skipping migration. Please replace with InjectionToken.
  • When the token is an identifier (e.g., a variable or imported symbol), it adds:
    // WARNING: Variable token 'myToken' found. Skipping migration. Please replace with InjectionToken.

Tests

No new tests were added, but manual testing confirms the comment is correctly inserted in migrated code.


Please review and let me know if any changes are needed.

@google-cla

google-cla Bot commented Jun 11, 2025

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@pullapprove
pullapprove Bot requested a review from atscott June 11, 2025 09:15
@MeAkib

MeAkib commented Jun 11, 2025 •

Copy link
Copy Markdown
Contributor

@aliabodaraa You need to follow the contribution guidelines. specially Contributor License Agreement (CLA)

@aliabodaraa
aliabodaraa force-pushed the fix/inject-migration-string-token branch from 92516ab to 90c0d8d Compare June 11, 2025 09:56
@JoostK

JoostK commented Jun 11, 2025

Copy link
Copy Markdown
Member

This won't fix the reported issue, as it didn't use a string literal but rather a reference to a variable/import declaration.

@aliabodaraa aliabodaraa changed the title fix(core/schematics): add TODO comment for string tokens in inject-migration fix(core/schematics): skip migration for non-type-safe tokens in inject-migration and log warnings Jun 11, 2025
@aliabodaraa

aliabodaraa commented Jun 11, 2025 •

Copy link
Copy Markdown
Author

This won't fix the reported issue, as it didn't use a string literal but rather a reference to a variable/import declaration.

@JoostK
Thanks for the clarification! I've updated the migration to now handle both StringLiteral tokens and Identifier tokens (e.g. variable or import references). In both cases, it skips the migration and adds a warning comment in the code. Please let me know if there's any further case you'd like me to cover.

@MeAkib

MeAkib commented Jun 11, 2025

Copy link
Copy Markdown
Contributor

@aliabodaraa As I know, you should be do it under a single commit. You can achieve this by rebase, squash, etc.

@aliabodaraa
aliabodaraa force-pushed the fix/inject-migration-string-token branch from d04d709 to 686da94 Compare June 11, 2025 15:55
@aliabodaraa
aliabodaraa force-pushed the fix/inject-migration-string-token branch from 686da94 to a3e743f Compare June 11, 2025 16:10
@JoostK

JoostK commented Jun 11, 2025

Copy link
Copy Markdown
Member

I don't think this is a desirable approach either, as identifiers are commonly used to refer to InjectionTokens, not string. There is a localTypeChecker available to inspect types, but the migration operates with individual checkers per file to migrate, meaning that any references into external files won't resolve.

Practically speaking this means that the reported issue cannot be detected using the current migration setup, and I think that is fine; using strings for tokens is recommended against and mostly still exists because of AngularJS interop, the migration breaking this shouldn't be too common and any resulting compilation error will indicate that manual work is necessary; that seems fine to me.

@JeanMeche

Copy link
Copy Markdown
Member

Per Joost, I've also closed #61982

@JeanMeche JeanMeche closed this Jun 11, 2025
@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Jul 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

migration @angular/core:inject create uncompiled code

4 participants