-
Notifications
You must be signed in to change notification settings - Fork 6k
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
The annotation for nullable reference types should only be used in code within a '#nullable' context. #9517
Comments
@arivoir Adding the
|
@BillWagner Yes, I have the LangVersion tag correctly. Actually I could migrate the project correctly and fix all the bugs that appeared because of the change to c#8. Now I'm seeing the warnings are comming from the Intellisense, because when I change the "Build+IntelliSense" combo box of the "Error List" to just "Build" they go away. Neither Analyze nor Clean nor Rebuild nor deleting bin and obj folders make any difference. I'm using the following project to test Open.Flickr Please let me know if you need more info. |
@arivoir It sounds like a bug with the "Error List" being out of date and not refreshing properly. I assume that closing and re-opening VS solves the problem. Could you file an issue in https://github.com/dotnet/roslyn if you are able to reproduce the issue? Thanks |
@jcouv I tried restarting Visual Studio and even the PC, but the problem is still there. Once you open the project it takes some seconds, apparently running the Intelli-sense, and the "Error List" is re-populated with these warnings. |
@arivoir One more question: if you build the project ( |
@jcouv No, they do not appear in the "Output" pane. Only in "Error List" |
@arivoir I talked to a coworker and I think we've figured this out. I suspect you have a legacy-style project file. So what happens is that Build does the right thing, but when the IDE tries to approximate the build process (Intellisense) it did not properly account for the This will be fixed by the time C# 8.0 ships, and it is tracked by dotnet/project-system#4058 In the meantime, you can use the "Build" filter (instead of "Build+Intellisense"), or if the project is small you could add |
@jcouv I'm afraid it is not the case. It's actually using the new style Open.Flickr.csproj You can get the project and try in your environment. Please let me know whether you're having the same issue as me. |
@arivoir This is mysterious. I cloned your project and opened it with a dev16 preview, but couldn't repro.
|
I originally found this problem in preview1, and then upgraded to preview1.1 where the problem continued. Please let me know if there is any log or something I can make to help. |
I tried this on a preview 1 ( I would be tempted trying to "repair" your preview 1 (or 1.1) installation. Tagging @heejaechang in case he has some ideas. To summarize, the issue is that the "Error List" with "Build+IntelliSense" filter shows some nullable warnings that appear nowhere else (neither in "Output" pane, nor in "Build" filter). |
Opened a roslyn issue: dotnet/roslyn#31765 Feel free to close this issue (there is no problem with docs). |
Closing this based on above discussion and issue. |
@xuhongxu96 In preview2, the name of the build property changed. |
I'm reopening this. I'll update the docs in the next two weeks to reflect the change in the build property. |
Didn't get it clear enough. |
@isxaker works fine for me with .NET Framework. Maybe it's fixed now? |
can you post here your .csproj file please |
Okay, I've tested it and it doesn't seem with to work with .NET Framework old csproj format unless you add
but after converting to new csproj format (https://github.com/hvanbakel/CsprojToVs2017) it works with .NET Framework:
Also, if you're using ReSharper or Rider you have to install EAP. additional notes:
@jcouv described that earlier in this discussion |
This issue seem to be back with with the latests Visual Studio 2019 Preview (16.2.0 Preview 1.0). After upgrading, I get multiple "CS8632 C# The annotation for nullable reference types should only be used in code within a context." when I open my solution https://github.com/egil/bootstrap-dotnet. Adding One of the projects in the solution that fails to compile with that warning is tests/Egil.RazorComponents.Bootstrap.Tests that has this .csproj: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CodeAnalysisRuleSet>Egil.RazorComponents.Bootstrap.Tests.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CodeAnalysisRuleSet>Egil.RazorComponents.Bootstrap.Tests.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Egil.RazorComponents.Bootstrap\Egil.RazorComponents.Bootstrap.csproj" />
</ItemGroup>
</Project> I have tried reinstalling |
The option has been renamed to |
Great. That is a good simplification. I must have missed that in the release notes. Thanks for the hint. |
Just to clarify, SDK 2.2.204, 2.2.300 and 3.0.100-preview5-011568 still appear to use The same with Visual Studio 2019 16.1 ( 2019 16.2 Preview does have have it though ( |
I just enabled nullable like this
and my project is failing to compile 'Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version 'preview' or greater'. I'm using .NET Core 3 preview 6 and Visual Studio 16.2 Preview 2 |
I'm getting this issue. Here's my project settings: Adding LangVersion doesn't have any impact. This appears to align with updating to VS2019 16.4, however I cannot say that for certain. Nullable reference types were being used prior to the update without this message being decorated everywhere. |
I was facing this kind of issue at my nullable reference type property declarations, so I kind of accidentally found the solution for this warning by setting So in the end my code ended up looking like this #nullable enable
public Type? TypeProp { get; set; }
#nullable disable So my guess is that depending on the type of situation you are facing, you should use one of the values mentioned in @jcouv 's answer, then disabling the setting by ending the block with Also, none of that |
Except that's not a solution to the issue, since setting For some reason, in my situation the evaluation is ignoring the existence of the nullable setting in the project file. |
My latest solution was to implement it as suggested by other answers and it worked just fine. NET Core versions older than 3.0 may still face the issue probably because their framework versions don't use C# 8.0 yet, which is used by default by VS 2019 as its IntelliSense compiler, as far as I know. Those cases will require either different fixes, maybe tinkering with the IDE settings, or for them to be worked on exclusively via VS 2017 instead. Based on Microsoft's own documentation, I reached the following solution: <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>annotations</Nullable>
</PropertyGroup>
</Project> That enables the nullable context, eliminating the CS8632 warning message - disregarding the need for a Also, you won't see it happen until you restart VS completely, since IntelliSense uses the last loaded setting for real time compilation. |
All of my project settings are correct as it pertains to what behavior I am expecting to see in the project regarding any nullable reference type warnings. However, after further investigation on another developer's development environment, it was determined that the latest version of ReSharper is the cause of the issues I'm seeing. So I will redirect my issue reporting to their issue tracker. Thank you. |
Thanks @Tiramonium It worked for me. |
I have the following in my.csproj file. When running in vscode on my local machine, it doesn't throw any warnings/errors. But when I build it in the Azure pipelines task, I get a bunch of these CS8632 warnings.. Any idea what might be happening?
|
@ata18 Just to confirm, when you're doing a local build (say from command-line, not VSCode), are you getting the expected nullability warnings? There's also a small possibility that the specific compiler versions used in pipeline task and locally differ. The nullability feature has had revisions. You could troubleshoot this by confirming the compiler versions (add |
@ata18 first of all, if you noticed, this warning got introduced in C# 8.0 and I'm assuming it carried onto 9.0 as well, which is used by NET 5.0. If you had read this thread you would have noticed that Another thing I noticed in your settings that might be causing unexpected results is that you set it to treat warnings as errors with |
@jcouv Thanks for your response, I will check the compiler versions. @Tiramonium You are correct. I know that true enables the check. And I have fixed all the errors related to
What is wrong here? |
@ata18 I just noticed a very basic error in your code. String is a C# reference type which inherently implements its nullable version, same with generic reference types like |
@Tiramonium I don't think that is true with c# 8+. |
Enabling |
After converting my project to c#8 and fixing all the problems. Every warning was removed, and suddently I got 97 Warnings of these
Warning CS8632 The annotation for nullable reference types should only be used in code within a '#nullable' context.
Do I need to add the #nullabel context if I used <NullableReferenceTypes>true</NullableReferenceTypes> in the proj file?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: