When opening older existing projects, my copy of Visual Studio 2022 insists that <Nullable>enable</Nullable>
is now the default behavior, causing those older projects to fail to compile:
3>------ Rebuild All started: Project: Foo, Configuration: Debug Any CPU ------
3>CSC : error CS8630: Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version '8.0' or greater.
Of course, I can add <Nullable>disable</Nullable>
to the project file, and it compiles. But we have a heterogeneous environment, with many different versions of Visual Studio and many older versions of .NET in active use, with some code still needing to be built under things like .NET Framework 4.x and .NET Compact 3.x. Because of this, "change this project file," and especially "change all project files in this solution," are not viable answers for us: For older code, the projects must remain unchanged so they can build on older versions of Visual Studio as well.
So, simply, is there a workaround to make newer versions of Visual Studio not default to <Nullable>enable</Nullable>
and <LangVersion>7.3</LangVersion>
, which cause failures when loading older projects? Or do I have no choice but to either (A) load those in an older version of Visual Studio or (B) edit the project files to get it to build, but then manually remove the <LangVersion>
and <Nullable>
directives before checking anything in?
(I am on VS2022 64-bit, 17.8.2, the latest available version as of this writing.)
After some searching, I found this Directory.Build.props
file in the root of all my source code directories, above where any projects were being checked out. Notably, I did not create it, but the culprit seems to be named within it:
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<AvaloniaVersion>11.0.2</AvaloniaVersion>
</PropertyGroup>
</Project>
Presumably when I was testing Avalonia some months ago, one of the Avalonia extensions added it on my behalf. It obviously is overriding the <Nullable>
setting.
So if you run into this issue, as @RandRandom suggested, check for a Directory.Build.props
file in any directory above those you're working in.