Roslyn: Auto-generated Program.cs file not annotated as such

Created on 31 Jan 2017  路  9Comments  路  Source: dotnet/roslyn

Originally logged raised as DotNetAnalyzers/StyleCopAnalyzers#2282.

Version Used:

1.0.0-rc3-004530

Steps to Reproduce:

  1. Clone https://github.com/martincostello/website.
  2. Checkout the DotNet-Core-With-MSBuild branch.
  3. Run Build.ps1 -RestorePackages $true -RunTests $false

Expected Behavior:

Build completes with no analysis warnings.

Actual Behavior:

Analysis warnings are generated by the compiler-generated Program.cs file:

obj\Release\netcoreapp1.1\Website.Tests.Program.cs(2,1): warning SA1516: Elements must be separated by blank line [C:\projects\website\tests\Website.Tests\Website.Tests.csproj]
obj\Release\netcoreapp1.1\Website.Tests.Program.cs(2,15): warning SA1502: Element must not be on a single line [C:\projects\website\tests\Website.Tests\Website.Tests.csproj]
obj\Release\netcoreapp1.1\Website.Tests.Program.cs(2,7): warning SA1400: Element 'Program' must declare an access modifier [C:\projects\website\tests\Website.Tests\Website.Tests.csproj]
obj\Release\netcoreapp1.1\Website.Tests.Program.cs(2,47): warning SA1502: Element must not be on a single line [C:\projects\website\tests\Website.Tests\Website.Tests.csproj]
obj\Release\netcoreapp1.1\Website.Tests.Program.cs(2,28): warning SA1400: Element 'Main' must declare an access modifier [C:\projects\website\tests\Website.Tests\Website.Tests.csproj]

The generated code is:

using System;
class Program {static void Main(string[] args){}}

Looking at other auto-generated files, this should have one of either of the following headers in the file so that it is ignored by analyzers:

// <auto-generated> This file has been auto generated. </auto-generated>

or:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


// Generated by the MSBuild WriteCodeFragment class.
Area-Analyzers

Most helpful comment

There is one more minor issue here: this picks up with code coverage as an untested block. As it's an auto-generated class, you can't add an attribute to ignore code coverage. Not sure the correct way to add this as an issue. Is someone able to assist! I'd love to be more involved in this community. Nevermind, found an open issue in #813 in the VSTest Repo: https://github.com/Microsoft/vstest/issues/813

All 9 comments

Apologies if this is an MSBuild issue rather than a compiler issue.

@codito I believe this file is generated in test SDK. I'm wondering why do we need to generate this file and what happens if the project already contains class Program.

To answer my own question, this is what happens:

1>obj\Debug\netcoreapp1.0\XUnitTestProject1.Program.cs(2,7,2,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'Program'

This feels like a hack. I think we should remove this generated file.

@tmat auto generation of program entrypoint is intentional (see https://github.com/dotnet/sdk/issues/201). We should add an Auto generated header.

It still feels like a hack. Perhaps good for now, but I think we should do this better in next release. Apart from the issue with projects that already define Program class, the current approach requires the SDK to have special code for each language. Right now I don't see F#. Do we not want to use this for F#?
@srivatsn

Resolved by Microsoft/vstest#424.

There is one more minor issue here: this picks up with code coverage as an untested block. As it's an auto-generated class, you can't add an attribute to ignore code coverage. Not sure the correct way to add this as an issue. Is someone able to assist! I'd love to be more involved in this community. Nevermind, found an open issue in #813 in the VSTest Repo: https://github.com/Microsoft/vstest/issues/813

I'll just reiterate my point: this is a hack and should be removed.
@livarcocc

Was this page helpful?
0 / 5 - 0 ratings