简体   繁体   中英

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM