CS8019 Error on Assemblyinfo on temp file MSBuild

2019-06-16 12:39发布

I am getting a code analysis error on my build server the error is

...NETFramework,Version=v4.6.AssemblyAttributes.cs(3,1): error CS8019:Unnecessary using directive.

This is in a Temp file which Visual Studio creates.

In my project I have "Suppress results from generated code (managed only)" ticked. I would have thought that would be enough.

But I still get the error on the server and locally i get none.

Any ideas?

3条回答
Juvenile、少年°
2楼-- · 2019-06-16 13:11

Michal's answer only partially helps here. Yes, you can redirect where that file is written but it will still violate the CS8019 rule.

You have two options:

  1. Also set the <TargetFrameworkMonikerAssemblyAttributeText> property to something that doesn't violate the rule. For instance:

    // &lt;autogenerated /&gt;
    [assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(&quot;$(TargetFrameworkMoniker)&quot;, FrameworkDisplayName = &quot;$(TargetFrameworkMonikerDisplayName)&quot;)]
    
  2. Or, redirect the file to someplace not temporary. In my case I chose to write it to the solution root so all projects would share the file. I then manually edited the file to remove the violations and committed the file along with the rest of my code. The file doesn't get overwritten if it already exists so this will generally be safe.

查看更多
beautiful°
3楼-- · 2019-06-16 13:19
  1. Only setting the TargetFrameworkMonikerAssemblyAttributesPath property does not remove the warning. It relocates the file that generates the warning, which will prove useful.
  2. Setting the TargetFrameworkMonikerAssemblyAttributeText property does not work. It appears that this property is overwritten by the target that generates this file. (In MSBuild 14.0, the property is overwritten by the target _SetTargetFrameworkMonikerAttribute in the file Microsoft.CSharp.CurrentVersion.targets and is later being referred to in the target GenerateTargetFrameworkMonikerAttribute in file Microsoft.Common.CurrentVersion.targets.)
  3. (Working solution) Setting the TargetFrameworkMonikerAssemblyAttributesFileClean to false will prevent the file from being overwritten if it already exists. You can thus let the build script generate it, fix the using ...; lines manually, save it and see that it is not regenerated when rebuilding. At this point, placing the file in a non-temporary path makes sense.

    Adding the following to a SharedBuildScript.msbuild.xml file and referring to that within individual project files ensures that they all refer to the same single file:

    <PropertyGroup>
        <TargetFrameworkMonikerAssemblyAttributesFileClean>False</TargetFrameworkMonikerAssemblyAttributesFileClean>
        <TargetFrameworkMonikerAssemblyAttributesPath>$(MSBuildThisFileDirectory)SharedAssemblyAttributes.cs</TargetFrameworkMonikerAssemblyAttributesPath>
    </PropertyGroup>
    
查看更多
小情绪 Triste *
4楼-- · 2019-06-16 13:21

Googling for CS8019 AssemblyAttributes yielded many interesting articles, such as this blog post. Quoting:

Fortunately for us, MSBuild is flexible enough so we can work around it. The good design is to generate this file into the Intermediate directory (usually called obj), because this is where all transient and temporary files should go during a build process. We can either set this property in our project file:

<PropertyGroup>
    <TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
</PropertyGroup>

Or if your build uses a common .props file, set this property there. This will ensure that your build doesn’t depend on the TEMP directory and is more isolated, repeatable and incremental.

查看更多
登录 后发表回答