1

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.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
Noggog
  • 61
  • 5
  • 2
    Did you restart the VS after adding a `Directory.Build.props`? Full rebuild/clean of solution might help, as well as deleting a `.vs` folder – Pavel Anikhouski Feb 07 '21 at 20:57
  • @jwdonahue do you have some sort of reference for that statement? They don't "implement support" for props/targets files on a per-property basis. Everything just "trickles down". And you can definitely set both properties OP is after in the props file... Its exactly what I do – pinkfloydx33 Feb 07 '21 at 21:23
  • Silly question, but the props file isn't in the same folder as the project is it (as opposed to at least the solution level/directory above project)? I've seen unexpected behavior if you try that – pinkfloydx33 Feb 07 '21 at 21:26
  • @pinkfloydx33, it's just been my experience that not everything works from the directory.build.props level (I really wish they did). I recall reading some documentation on how some tags just aren't meaningful at different scopes, but I don't have a reference on hand. – jwdonahue Feb 07 '21 at 21:44
  • @pinkfloydx33, I retract the statement. The fact that I've found some things just don't work, doesn't mean they shouldn't have. – jwdonahue Feb 07 '21 at 21:48

1 Answers1

2

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.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Nullable=enable can be set in a props file and works just fine. On the OP I overlooked WarningsAsErrors and misread it as TreatWarningsAsErrors, the latter of which can also be set in a props file just fine – pinkfloydx33 Feb 08 '21 at 14:01