-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Annotate config.GetValue()
with [NotNullIfNotNull]
#101336
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/BindingHelperInfo.cs Lines 34 to 41 in cb978a5
But build is failing with:
|
@dahlbyk thanks for submitting the PR. I converted this to a draft PR as there is more need to be done before we can accept this PR. Adding the attribute to these APIs make sense, there is more need to be done here if we want to proceed with that.
We need to decide if we should go with the breaking change and file a breaking change doc for it. Also, we'll need to suppress the app compatibility failure:
CC @stephentoub @ericstj if they have any suggestion or recommendation about his change. |
Thanks for the initial feedback! I've conditionally included |
I took a stab at updating the source generator. Haven't figured out the CS0436 error mentioned above, but I did noticed it's specific to |
I don't think making a nullable API less nullable should be actually breaking. I think the issue flagged by APICompat is just that the reference assembly needs to be updated. Please make sure to update reference source in the ref folder. We also don't have strongly typed rules for nullability attributes - so those are just going to get flagged no matter what. If we agree to the change we can suppress the diagnostic. |
860871c
to
d44f69d
Compare
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == 'netstandard2.0'"> | ||
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Link="System.Private.CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs" /> | ||
</ItemGroup> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out this is redundant, as NullableAttributes.cs
is already included in most libraries automatically:
runtime/src/libraries/Directory.Build.targets
Lines 170 to 176 in 1c824d9
<!-- Adds Nullable annotation attributes to C# non .NETCoreApp builds. --> | |
<ItemGroup Condition="'$(Nullable)' != '' and | |
'$(Nullable)' != 'disable' and | |
'$(MSBuildProjectExtension)' == '.csproj' and | |
'$(TargetFrameworkIdentifier)' != '.NETCoreApp'"> | |
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Link="System\Diagnostics\CodeAnalysis\NullableAttributes.cs" /> | |
</ItemGroup> |
This explains #101336 (comment):
error CS0436: The type 'NotNullIfNotNullAttribute' in 'src\libraries\System.Private.CoreLib\src\System\Diagnostics\CodeAnalysis\NullableAttributes.cs' conflicts with the imported type 'NotNullIfNotNullAttribute' in 'Microsoft.Extensions.Configuration.Binder, ...'.
Microsoft.Extensions.Configuration.Binder.SourceGeneration.Tests.csproj
has its own NullableAttributes.cs
that it has ignored, but when actually used it conflicts with internal
one in Microsoft.Extensions.Configuration.Binder
:
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/Properties/InternalsVisibleTo.cs
Line 7 in 1c824d9
[assembly: InternalsVisibleTo("Microsoft.Extensions.Configuration.Binder.SourceGeneration.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] |
There may be a cleaner way, but I was able to work around this by allowing projects to opt out of the import.
src/libraries/Microsoft.Extensions.Configuration.Binder/src/CompatibilitySuppressions.xml
Outdated
Show resolved
Hide resolved
...s.Configuration.Binder/gen/Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj
Outdated
Show resolved
Hide resolved
d44f69d
to
001579c
Compare
@dahlbyk We shouldn't enforce the definition of the nullable attribute in the source generator as it could lead to complications and problems. The source generator only needs to verify if the attribute is defined during compilation and utilize it accordingly. If it's not defined, we should refrain from emitting any code that relies on this attribute. Users can choose to define the attribute in their code if they wish to support it, granting them flexibility in its usage. |
This seems reasonable but I'm new to SG so not sure what this looks like. Any examples/docs? |
You may look at the code runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Parser/KnownTypeSymbols.cs Line 78 in cdfc8f2
You search for |
001579c
to
4ffdaa3
Compare
Update: build is fine. I'm curious if the build is going to reproduce local failures that seem related to #95830:
It does, in fact, have an implementation: Line 226 in dad01da
I might just need to do a full rebuild? |
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Parser/KnownTypeSymbols.cs
Outdated
Show resolved
Hide resolved
...ourceGenerationTests/Microsoft.Extensions.Configuration.Binder.SourceGeneration.Tests.csproj
Outdated
Show resolved
Hide resolved
0aeb376
to
746a4ec
Compare
Restored |
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dahlbyk, I added a minor comments, otherwise LGTM .
...ries/Microsoft.Extensions.Configuration.Binder/Microsoft.Extensions.Configuration.Binder.sln
Outdated
Show resolved
Hide resolved
8b8b2be
to
92d4c26
Compare
Thanks @dahlbyk for your help getting this working and completing it. |
* Annotate GetValue() with [NotNullIfNotNull] * Avoid duplicate NullableAttributes.cs * Annotate generated GetValue() with [NotNullIfNotNull]
* Annotate GetValue() with [NotNullIfNotNull] * Avoid duplicate NullableAttributes.cs * Annotate generated GetValue() with [NotNullIfNotNull]
* Annotate GetValue() with [NotNullIfNotNull] * Avoid duplicate NullableAttributes.cs * Annotate generated GetValue() with [NotNullIfNotNull]
This is annoying:
Before the fix, the updated tests fail to compile:
The source generator has been updated as well.