Uh oh!
There was an error while loading. Please reload this page.
Annotate config.GetValue() with [NotNullIfNotNull] - #101336
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
But build is failing with:
|
tarekgh
commented
Apr 21, 2024
@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. |
dahlbyk
commented
Apr 22, 2024
Thanks for the initial feedback! I've conditionally included |
dahlbyk
commented
Apr 22, 2024
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 |
ericstj
commented
Apr 22, 2024
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
d44f69dCompare| <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.
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
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:
There may be a cleaner way, but I was able to work around this by allowing projects to opt out of the import.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
d44f69d to
001579cComparetarekgh
commented
Apr 26, 2024
@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. |
dahlbyk
commented
Apr 26, 2024
This seems reasonable but I'm new to SG so not sure what this looks like. Any examples/docs? |
tarekgh
commented
Apr 27, 2024
You may look at the code
as example to see how you can search for a type in the compilation. You may look at the docs Compilation.GetTypesByMetadataName and Compilation.GetTypeByMetadataName too for more info. You search for |
001579c to
4ffdaa3Compare
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: I might just need to do a full rebuild? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
0aeb376 to
746a4ecComparedahlbyk
commented
Apr 28, 2024
Restored |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
8b8b2be to
92d4c26Comparetarekgh
commented
Apr 29, 2024
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.