dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.21k stars 1.35k forks source link

MSB4011 is not suppressable by MSBuildWarningsAsMessages #4038

Open rainersigwald opened 5 years ago

rainersigwald commented 5 years ago

Originally reported at https://developercommunity.visualstudio.com/content/problem/400893/using-msbuildwarningsasmessages-in-directorybuildp.html.

I have added Directory.Build.props to the same folder as my solution with content:

<Project>
<PropertyGroup> 
<MSBuildWarningsAsMessages>MSB4011</MSBuildWarningsAsMessages>
</PropertyGroup>
</Project>

The problem is that I still get the MSB4011 warnings when I compile.

I know that file is found because if I change the <PropertyGroup> tag to <PropertyGroup1>, I get an error when compiling.

Why isn't it working?

I am using visual studio professional 2017, 15.9.3. The projects are C++.

rainersigwald commented 5 years ago

I think this is happening for reasons related to #3295: the warning path for evaluation-related warnings is distinct from normal execution-time warnings.

In this case, the final value of MSBuildWarningsAsMessages can't be known mid-evaluation, so I'm not sure what could be done to fix this. We could maybe inspect the current value just before logging a warning, but that seems heavy-handed.

rainersigwald commented 5 years ago

Smallest repro I could think of: repro.zip

jeffkl commented 5 years ago

Some warnings are logged by the same code that parse the codes to suppress in your Directory.Build.props. To suppress this warning (not recommended if you can just fix the double import) you can use the /nowarn command-line argument for builds run via msbuild.exe. But there is no way to suppress it in Visual Studio.

botrif commented 5 years ago

It's impossible to avoid this warning when using new SDK-style projects if T4 templates are to be transformed during an msbuild build because the template targets must be included after the CSharp targets, and with SDK-style projects the CSharp targets get automatically included at the end of the csproj. I'd love to know of any workaround for this double-include, or at least how to get rid of this warning in the IDE

rainersigwald commented 5 years ago

@botrif I'm not sure I understand the whole situation, but can you expand the Sdk imports into their explicit form, like:

<Project>
    <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

    <PropertyGroup>
        <TargetFramework>net46</TargetFramework>
    </PropertyGroup>

    <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

    <Import Project="T4 stuff" />
</Project>

?

alexdrl commented 5 years ago

We are facing the same problem, but we are trying to make this warnings as error to stop this from happening earlier.

bhupeshpant19jan commented 4 years ago

We are also hitting the same issue. This is really discouraging.

lapinott commented 2 years ago

Same issue here.

Any chance suppressing this warning (or any MSB... for that matter) will be featured?

Full issue here (self-explanatory) : https://docs.microsoft.com/en-us/answers/questions/823721/visual-studio-34msb401134-build-warning-i-want-to.html

Would something similar to #pragma once reasonable for property sheets?

michael-hawker commented 2 years ago

It seems odd that this is special cased to not be suppressible? We're also using our props for dependency management across projects, those projects reference each other, so I don't think we can work around having them included multiple times.

So these warnings are just noise masking more important warnings in VS.

ackh commented 2 years ago

There is a fundamental problem I see here. If one wants to override a default target of the standard build process it is necessary to explicitly add <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" /> and after that define what is to be overridden as outlined here. However, this line results in a MSB4011 warning. If you're working in an environment where warnings are automatically turned into errors by the build system you cannot override default MSBuild targets.

rainersigwald commented 2 years ago

If one wants to override a default target of the standard build process it is necessary to explicitly add <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" /> and after that define what is to be overridden as outlined here. However, this line results in a MSB4011 warning.

That should not be true--@ackh did you add that explicit import without removing the Sdk="Microsoft.NET.Sdk from the top-level <Project> element?

The docs on this weren't super clear so I submitted MicrosoftDocs/visualstudio-docs#8444.

ackh commented 2 years ago

@rainersigwald You are right, I simply overlooked that I need to remove Sdk="Microsoft.NET.Sdk" from the Project node. Doing so prevents my problem entirely. Thanks for your quick reaction to my comment!