Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnforceCodeStyleInBuild does not cause the build to fail although errors are present #49439

Open
wertzui opened this issue Nov 17, 2020 · 13 comments

Comments

@wertzui
Copy link

wertzui commented Nov 17, 2020

Version Used:
Dotnet SDK 5.0.100
Visual Studio 2019 16.9.0 Preview 1

Steps to Reproduce:

  1. Add an .editorconfig file to your project.
  2. Add a violation of a rule which is set at the warning or error level. I added a Naming rule which enforces an underscore as a prefix for all fields and set it to be a warning.
  3. Setup you -csproj file like this
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>

  <!--some more properties-->

</Project>
  1. Write code which triggers the warning
  2. Ensure the error is present in Visual Studio (IDE1006 Naming rule violation: Missing prefix: '_')
  3. Build using Visual Studio
  4. Build using the command line dotnet build --configuration Release

Expected Behavior:
The build fails with the above error.

Actual Behavior:
The build succeeds.

@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 17, 2020
@jinujoseph jinujoseph added Bug Investigation Required and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 17, 2020
@jinujoseph jinujoseph added this to the 16.9 milestone Nov 17, 2020
@rosecodym
Copy link

I'm having this problem as well. The warning/error shows up in the Error List when it is set to Build + Intellisense but not when set to Build only, and it doesn't impede the build.

@rosecodym
Copy link

I just tried again using VS 2019 Preview 2 and the 5.0.200-preview.20601.7 SDK, and warnings are still not emitted at build time for broken .editorconfig rules.

@jasonmalinowski jasonmalinowski modified the milestones: 16.9, 16.10 Jan 15, 2021
@hakenr
Copy link

hakenr commented Mar 2, 2021

+the TreatWarningsAsErrors does not affect EnforceCodeStyleInBuild warnings, they are still emitted as warnings.

@irontoby
Copy link

irontoby commented Apr 16, 2021

I have noticed that these are no longer working for named rules such as:

csharp_style_inlined_variable_declaration = true:warning

but still work for rules using the analyzer ID such as:

dotnet_diagnostic.IDE0018.severity = warning

I've created a more specific bug #52682 for this issue, though perhaps it's the same issue (I didn't see this one at first).

@jasonmalinowski jasonmalinowski modified the milestones: 16.10, 16.11 May 10, 2021
@jinujoseph jinujoseph modified the milestones: 16.11, 17.0 Jul 16, 2021
@jasonmalinowski jasonmalinowski modified the milestones: 17.0, 17.1 Sep 13, 2021
@rosecodym
Copy link

Still happening on VS2022 17.0.4 and the 6.0.101 SDK. I'm also experiencing the regression described in #57812, so I don't even have IntelliSense support anymore. After reading https://developercommunity.visualstudio.com/t/vs2022-1700-rc2-ignores-shared-editorconfig/1561830, I tried project-specific .editorconfig files (instead of a solution-global one) but it didn't fix it.

@jasonmalinowski jasonmalinowski modified the milestones: 17.1, 17.2 Jan 5, 2022
@jasonmalinowski jasonmalinowski modified the milestones: 17.2, 17.3 Mar 22, 2022
@jasonmalinowski jasonmalinowski modified the milestones: 17.3, Backlog Jul 6, 2022
@rosecodym
Copy link

I think I've figured out the missing ingredient here: the documented prescription to use a different syntax when setting diagnostic severity. By configuring my rules like this:

csharp_prefer_simple_using_statement = true
dotnet_diagnostic.IDE0063.severity = warning

I'm seeing the results I expect. I don't know if I just didn't notice that documentation the last time I tried this or it didn't exist, but it seems to be working for me now.

@gcsizmadia
Copy link

For me this Stackoverflow answer was the solution I needed.

@jeremy-visionaid
Copy link

+the TreatWarningsAsErrors does not affect EnforceCodeStyleInBuild warnings, they are still emitted as warnings.

Just fixing this particular problem would go a long way to making the situation better

@jeremy-visionaid
Copy link

Now I understand the available options a bit better, I guess this does make sense, but isn't necessarily intuitive...
There's an additional property, CodeAnalysisTreatWarningsAsErrors that would default to TreatWarningsAsErrors. However, msbuild would evaluate it before TreatWarningsAsErrors is set in the project file. So, you either need to set both TreatWarningsAsErrors and CodeAnalysisTreatWarningsAsErrors in the project file, or set TreatWarningsAsErrors from the command line, e.g.
msbuild /p:TreatWarningsAsErrors=true

@as3249762
Copy link

I have a similar issue if anyone could help.

I've set EnforceCodeStyleInBuild to true.

<PropertyGroup>
	<OutputType>Exe</OutputType>
	<TargetFramework>net8.0</TargetFramework>
	<ImplicitUsings>enable</ImplicitUsings>
	<Nullable>enable</Nullable>
	<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

I've also set an error severity in the .editorconfig for files with non-filescoped namespaces csharp_style_namespace_declarations = file_scoped:error

While editing in visual studio I can now see an error in any files that use blockscoped namespaces, which is what I want, however when I go to build and run the project it runs fine, even though it should fail as there's an error.

@jeremy-visionaid
Copy link

@as3249762 Sounds like a separate issue, but maybe try IDE0160/IDE0161 instead/as well?

If I understand correctly, the problem here was that the op also needed to set CodeAnalysisTreatWarningsAsErrors as well as TreatWarningsAsErrors since they were trying to do it via the project config rather than providing the propery in the build invocation.

@as3249762
Copy link

@jeremy-visionaid Thanks for getting back to me :)

Adding IDE0160/IDE0161 kind of works.

If in visual studio I try to rebuild I get a failed build, so it looks like rebuild is behaving as I would expect.

If I subsequently try to build I also get a failed build. However it looks like build still isn't working as expected.

If I

  • Remove all errors in project
  • Build the project (which builds successfully)
  • Reintroduce errors in the project
  • Build again

The build will be successful, even though it should be failing.

Sorry if this should be put in a separate issue, this is my first time commenting on a github issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests