65

I have 3 temporary files being created in obj/debug:

E.g.

  • TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
  • TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
  • TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

(The guids don't seem to change even after a solution clean)

My build is failing because:

SA1633: The file has no header, the header Xml is invalid, or the header is not located at the top of the file.

I don't want to turn the StyleCop rule off. How do I find out what is creating these temporary files?

The site is an asp.net MVC 4 site, with 5 models, 4 controllers, 2 classes, 2 aspx web pages and 1 service reference, which numerically don't seem to tally with the 3 files.

Any pointers?

Edit: If I change framework from 4.5 back to 4 these files go away and the build is successful.

My version of StyleCop is 4.4, I'm more than open to finding a way to get it to ignore obj/debug

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
  • Have you manually deleted these files (sometimes clean doesn't knock out things in the obj folder) and do they come back? – Tommy Sep 13 '12 at 12:39
  • @Tommy yup, I've manually nuked the obj/debug folder and they come back with the same guids. – NikolaiDante Sep 13 '12 at 12:41
  • If they are temporary files that are not part of the solution/deployment and you have no control over how MSBuild is creating these files, would it be prudent to just have StyleCop ignore the obj folder? I would think these may be tied to MVC views/web page portions of your project, especially if you have MVCBuildViews turned on or just part of the overall project dll build process (am not a c-sharp compiler expert) – Tommy Sep 13 '12 at 12:45
  • @Tommy I couldn't see how to add a folder level ignore with StyleCop 4.4 The settings.StyleCop file doesn't seem to support folder level exclusions. – NikolaiDante Sep 13 '12 at 12:49
  • Well there goes that idea, I could not seem to find anything as well. All solutions point to using metadata attributes. – Tommy Sep 13 '12 at 12:53
  • @Tommy I found a way to make StyleCop treat these as generated files, see answer below. – NikolaiDante Sep 14 '12 at 09:42

7 Answers7

95

I solved this issue by going to the project solution (whose build) was giving this error.

  1. right click on the project and unload the project.
  2. Then right click on the project and edit the .csproj file.
  3. Look for these temp (problematic) generated files. (see example code)
  4. remove this file references from the .csproj file.
  5. Right click on project and load back the project.
  6. Rebuild the solution.
  7. Its good to go now...

they look like this in the csproj file:

<Compile Include="src\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs" />
<Compile Include="src\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs" />
<Compile Include="src\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs" />
BozoJoe
  • 6,117
  • 4
  • 44
  • 66
Rajesh
  • 961
  • 1
  • 5
  • 2
  • 2
    I did not need to edit the .csproj file, but did need to unload, remove all content from /bin, /obj, all the .sln files, and all the .suo and .csproj.usr files. Cleaned and built, reloaded the project and was g2g. Thanks! – Rachael Jan 05 '15 at 21:46
  • Hi Rajesh. Thank you so much. – VivekDev Mar 24 '16 at 11:02
  • 1
    Does anyone can explain the cause of the problem? Why does this happen to so many people? – ˈvɔlə Nov 25 '19 at 11:34
39

In the parsers block of a StyleCop.Settings file add an entry for these files: The value is a regex, so a tighter one for matching a guid could be used, but these meets my need for now.

  <Parsers>
    <Parser ParserId="Microsoft.StyleCop.CSharp.CsParser">
      <ParserSettings>
        <BooleanProperty Name="AnalyzeDesignerFiles">False</BooleanProperty>
        <CollectionProperty Name="GeneratedFileFilters">
          <Value>\.g\.cs$</Value>
          <Value>\.generated\.cs$</Value>
          <Value>\.g\.i\.cs$</Value>
          <Value>TemporaryGeneratedFile_.*\.cs$</Value>
        </CollectionProperty>
      </ParserSettings>
    </Parser>
  </Parsers>
NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
16

The 3 files with "TemporaryGeneratedFile_" prefix are auto-generated by the Microsoft.WorkflowBuildExtensions.targets file most likely imported via the chain:

  • *.csproj -->
  • Microsoft.CSharp.targets -->
  • Microsoft.Common.targets -->
  • Microsoft.WorkflowBuildExtensions.targets

They are generated under the intermediate output path pointed by the $(IntermediateOutputPath) MSBuild property, usually something like obj\debug. One way to deal with the StyleCop warnings/errors about these auto-generated files is to tell StyleCop to skip any *.cs files under the $(IntermediateOutputPath). For example, include the following item in your project:

<ItemGroup>
    <ExcludeFromStyleCop Include="$(IntermediateOutputPath)\**\*.cs" />
</ItemGroup>

ExcludeFromStyleCop is an item name recognized by the StyleCop.targets file to exclude files from analysis during a build (at least for StyleCop 4.7). The double star ** is MSBuild syntax for searching recursively under a folder.

The new item might show up in the Solution Explorer in Visual Studio. If that is undesirable it can be hidden by using the 'Visible' item metadata:

<ItemGroup>
    <ExcludeFromStyleCop Include="$(IntermediateOutputPath)\**\*.cs" >
        <Visible>False</Visible>
    </ExcludeFromStyleCop>
</ItemGroup>

Similar approach can be used to exclude other files if necessary. I hope that helps.

JustinStolle
  • 4,182
  • 3
  • 37
  • 48
Vlado
  • 161
  • 1
  • 2
  • Aside from the closing ItemGroup having the slash on the wrong side, this solution worked perfectly for me! – Dracorat Apr 17 '14 at 19:19
2

I recently came across this after making a couple of code changes in an old WebForms project. I took the following steps:

  1. Close the project
  2. Open the .csproj file in a text editor
  3. Searched for "TemporaryGeneratedFile_" and found the 3 files causing trouble
  4. Deleted these three includes:
<Compile Include="obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs"/>
<Compile Include="obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs"/>
<Compile Include="obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs"/>
  1. Save and reopen
  2. Rebuilt and no further issue with it
CJM
  • 71
  • 1
  • 3
0

I was helping a colleague who had added a new project to our main VS solution and who got these same 3 errors. I went through the suggested steps above with him but did not have any success in fixing it. After this, I discovered he had missed one of the steps we perform when adding a new project within our solution that uses Code Analysis and StyleCop. He had forgot to add the Settings.StyleCop file to his project :)

John
  • 1
0

I have recently ran into this same problem out of nowhere.

For me, I was able to overcome this by opening the .csproj files for each project, then remove the following line:

<Import Project="$(SolutionDir)\CodeAnalize\Microsoft.StyleCop.targets" />

After re-opening the solution, it was able to build everything without an error.

erupnu
  • 1
  • 1
0

I faced a similar problem and solved it as follow:

Deleted debug in bin folder in addition to debug in obj folder rebuild and it worked