Skip to content
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

Report warning when using the package without invoking AddServices #30

Closed
kzu opened this issue Nov 10, 2022 · 0 comments · Fixed by #31
Closed

Report warning when using the package without invoking AddServices #30

kzu opened this issue Nov 10, 2022 · 0 comments · Fixed by #31
Labels
enhancement New feature or request

Comments

@kzu
Copy link
Contributor

kzu commented Nov 10, 2022

This source generator is quite magical, but it still can't inject itself in the
service collection without users invoking the provided
ServiceCollection.AddServices() extension method. This has tripped a
a new user
in the past, which is not a great user experience.

We should make every attempt to detect this situation, so the idea is to:

  1. Detect if users have access to IServiceCollection in the current project: it may be that
    they just added the package to get the [Service] attribute for a library. Nothing to do
    in that case.
  2. Detect if users actually invoke IServiceCollection members in the current project, more
    or less flexibly (i.e. don't verify much where specifically, since the DI library can be used
    in multiple contexts). If they do, then it's an indication that there should be a call to the
    AddServices extension method too somewhere. As a hint, report the location on the
    first invocation to a service collection method (or extension method).
  3. Report as a warning, since we can determine this wrong, depending on whether users
    are initializing services some other way or indirectly. This allows them to ignore the warning
    using the regular configuration mechanisms for analyzers and diagnostics.
kzu added a commit that referenced this issue Nov 10, 2022
Conditions for reporting the warning are:

* Detect if users have access to IServiceCollection in the current project: it may be that
they just added the package to get the [Service] attribute for a library. Nothing to do
in that case.
* Detect if users actually invoke IServiceCollection members in the current project, more
or less flexibly (i.e. don't verify much where specifically, since the DI library can be used
in multiple contexts). If they do, then it's an indication that there should be a call to the
AddServices extension method too somewhere. As a hint, report the location on the
first invocation to a service collection method (or extension method).
* Report as a warning, since we can determine this wrong, depending on whether users
are initializing services some other way or indirectly. This allows them to ignore the warning
using the regular configuration mechanisms for analyzers and diagnostics.

Closes #30
kzu added a commit that referenced this issue Nov 10, 2022
Conditions for reporting the warning are:

* Detect if users have access to IServiceCollection in the current project: it may be that
they just added the package to get the [Service] attribute for a library. Nothing to do
in that case.
* Detect if users actually invoke IServiceCollection members in the current project, more
or less flexibly (i.e. don't verify much where specifically, since the DI library can be used
in multiple contexts). If they do, then it's an indication that there should be a call to the
AddServices extension method too somewhere. As a hint, report the location on the
first invocation to a service collection method (or extension method).
* Report as a warning, since we can determine this wrong, depending on whether users
are initializing services some other way or indirectly. This allows them to ignore the warning
using the regular configuration mechanisms for analyzers and diagnostics.

Closes #30
kzu added a commit that referenced this issue Nov 10, 2022
Conditions for reporting the warning are:

* Detect if users have access to IServiceCollection in the current project: it may be that
they just added the package to get the [Service] attribute for a library. Nothing to do
in that case.
* Detect if users actually invoke IServiceCollection members in the current project, more
or less flexibly (i.e. don't verify much where specifically, since the DI library can be used
in multiple contexts). If they do, then it's an indication that there should be a call to the
AddServices extension method too somewhere. As a hint, report the location on the
first invocation to a service collection method (or extension method).
* Report as a warning, since we can determine this wrong, depending on whether users
are initializing services some other way or indirectly. This allows them to ignore the warning
using the regular configuration mechanisms for analyzers and diagnostics.

Closes #30
kzu added a commit that referenced this issue Nov 10, 2022
Conditions for reporting the warning are:

* Detect if users have access to IServiceCollection in the current project: it may be that
they just added the package to get the [Service] attribute for a library. Nothing to do
in that case.
* Detect if users actually invoke IServiceCollection members in the current project, more
or less flexibly (i.e. don't verify much where specifically, since the DI library can be used
in multiple contexts). If they do, then it's an indication that there should be a call to the
AddServices extension method too somewhere. As a hint, report the location on the
first invocation to a service collection method (or extension method).
* Report as a warning, since we can determine this wrong, depending on whether users
are initializing services some other way or indirectly. This allows them to ignore the warning
using the regular configuration mechanisms for analyzers and diagnostics.

Closes #30
@kzu kzu closed this as completed in #31 Nov 10, 2022
kzu added a commit that referenced this issue Nov 10, 2022
Conditions for reporting the warning are:

* Detect if users have access to IServiceCollection in the current project: it may be that
they just added the package to get the [Service] attribute for a library. Nothing to do
in that case.
* Detect if users actually invoke IServiceCollection members in the current project, more
or less flexibly (i.e. don't verify much where specifically, since the DI library can be used
in multiple contexts). If they do, then it's an indication that there should be a call to the
AddServices extension method too somewhere. As a hint, report the location on the
first invocation to a service collection method (or extension method).
* Report as a warning, since we can determine this wrong, depending on whether users
are initializing services some other way or indirectly. This allows them to ignore the warning
using the regular configuration mechanisms for analyzers and diagnostics.

Closes #30
@kzu kzu added the enhancement New feature or request label Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant