Noggog
Noggog

Reputation: 61

Visual Studio nullability ref errors show as warnings if defined in Directory.Build.props

This might just be a temporary VS bug, but I wanted to see if anyone had any fixes or if there was perhaps a VS setting I was missing.

If I define nullability errors:

<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>

And I do so within a .csproj, it works out as one would expect: Nullability Errors Showing

However, if the properties are defined in a Directory.Build.props file, which applies to a solution en-masse, they do not show as errors anymore: No Errors Showing

It still blocks compilation, and you can still see the error in the raw text output.. but the red squiggle doesn't show (just green) and more importantly, the Error List pane is empty.

Does anyone know why this is, or how to fix it? I would like to have nullability errors defined en-masse via the Directory.Build.props, but it's unusable in its current state.

Upvotes: 1

Views: 885

Answers (1)

Mr Qian
Mr Qian

Reputation: 23715

Actually, automatic msbuild import function has two ways: Directory.Build.props and Directory.Build.targets.

In your side, you should use Directory.Build.targets rather than Directory.Build.props.

As the document said, Directory.Build.props is imported at the top of the csproj file. So it should be used to define global msbuild properties that used on the global environment. And the properties defined on it can be overwritten by the csproj file.

Your situation is that the WarningsAsErrors is overwritten by the system default value none from csproj file. Although the value is not shown on the csproj file, it actually shows as none by default. When you create a new project without any changes, it always shows as a warning.

Solution

Therefore, you should rename the file as Directory.Build.targets and it always is used to overwrite any operation properties like WarningsAsErrors.

Mostly like this similar issue.

Upvotes: 2

Related Questions