Treating warnings as errors in Visual Studio 2019 for build but NOT intellisense

僤鯓⒐⒋嵵緔 提交于 2020-07-23 06:14:11

问题


I've just started using <TreatWarningsAsErrors>true</TreatWarningsAsErrors> in my csproj files, and when messing up a bit of code, I've found myself taking quite a bit of extra time hovering over all the different red lines to find the one that's actually an error, as I want to prioritse fixing the errors over the warnings. It would be great if the intellisense warnings could still show as green lines in the text editor, but also cause the build to fail.

I think that I could make the test/staging/production builds fail by passing /warnaserror to MSBuild, but I do want the development build in VS to fail as well if there are any warnings.

Does anyone know if this is possible? I'm using .NET Framework 4.8 (but would also be interested to know if it's possible in .NET Core) in Visual Studio 2019.

Thanks very much!


回答1:


I've found myself taking quite a bit of extra time hovering over all the different red lines to find the one that's actually an error, as I want to prioritse fixing the errors over the warnings. It would be great if the intellisense warnings could still show as green lines in the text editor, but also cause the build to fail.

As far as l know, Intellisense is very sensitive to catching errors, and once the built warnings are turned into errors, intellisense will catch them as errors.

Does anyone know if this is possible? I'm using .NET Framework 4.8 (but would also be interested to know if it's possible in .NET Core) in Visual Studio 2019.

Edit 1

So it is impossible to distinguish between warnings and real errors in this state. And in this state, Intellisense doesn't discriminate between them.

As an alternative, you can try my suggestion:

Suggestion

You can create a new configuration called Production which differ from Debug or Release. In this solution, the first step is to find the real error, and the second step is to turn the warning into an error so that you can optimize the code.

First, you can use Debug or Release to build your project in this mode, it can catch the real build error so that you can fix these errors preferentially.

Second, change Configuration to Production. This phase is often referred to as the final production release phase, and the build process will turn warnings into errors so you can spot bad code and optimize it.

The real question is how to create the Production Configuration:

Step

1) click menu Build-->Configuration Manager-->click new in Active solution configuration-->create a new configuration called Production.

2) unload the project and add <TreatWarningsAsErrors>true</TreatWarningsAsErrors> under Production like this:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Production|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Production\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <LangVersion>7.3</LangVersion>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>

In a word, first, change Configuration to Debug or Release, then build the project to find the real errors to fix them. Second, change change Configuration to Production, build the project to find the warning errors to fix them.

Hope it could help you.



来源:https://stackoverflow.com/questions/60285079/treating-warnings-as-errors-in-visual-studio-2019-for-build-but-not-intellisense

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!