Jason Morse
Jason Morse

Reputation: 6304

Preventing referenced assembly PDB and XML files copied to output

I have a Visual Studio 2008 C#/.NET 3.5 project with a post build task to ZIP the contents. However I'm finding that I'm also getting the referenced assemblies' .pdb (debug) and .xml (documentation) files in my output directory (and ZIP).

For example, if MyProject.csproj references YourAssembly.dll and there are YourAssembly.xml and YourAssembly.pdb files in the same directory as the DLL they will show up in my output directory (and ZIP).

I can exclude *.pdb when ZIP'ing but I cannot blanket exclude the *.xml files as I have deployment files with the same extension.

Is there a way to prevent the project from copying referenced assembly PDB and XML files?

Upvotes: 140

Views: 40151

Answers (9)

Using the AllowedReferenceRelatedFileExtensions property as others suggested above works well for me either in project files or via msbuild call as well.

There is one caveat though, which is that it works universally.

If we have a lot of projects that reference each other, this will also remove those pdbs that belong to the bottom projects referenced in top projects as well, not just external pdbs.

A possibility is to fallback to project-based setting and add it only to "bottom" projects, but that still makes it hard to get away with intermediate external references directly in top projects, so I am not sure how to control this properly.

In my opinion, it would be great to find a universal way of getting rid of all pdbs coming from external libraries but keep all pdbs coming from internal project references

One thing that seems to work for golden-means is using a combination as below:

/p:AllowedReferenceRelatedFileExtensions=.xml;.config /p:CopyLocalLockFileAssemblies=false 

the CopyLocalLockFileAssemblies property is set to false to prevent the copying of PDB files from NuGet packages, and the AllowedReferenceRelatedFileExtensions property is set to exclude ".pdb" files from external NuGet packages, but include all other related files (".xml" and ".config" files). Locally referenced projects that include PDB files will still have their PDB files copied to the output directory during build

Upvotes: 0

Vasanth8891
Vasanth8891

Reputation: 61

top 2 answers didn't work for me. I found a solution in this link which worked for me. http://kitsula.com/Article/How-to-exclude-xml-doc-files-from-msbuild.

Just in case, the above link is not working:

  1. Unload project in Solution Explorer

  2. Right click the project and click 'edit *.csproj'

  3. Add next lines in the PropertyGroup section of each environment.

  4. Reload and rebuild project.

     <AllowedReferenceRelatedFileExtensions>
              *.pdb;
              *.xml
     </AllowedReferenceRelatedFileExtensions>
    

Upvotes: 6

David Rogers
David Rogers

Reputation: 2643

I didn't have much luck with the other answers, I finally figured out how to do this in my implementation by using the built in "Delete" command, apparently there is a specific way you need to implement wildcards, it's bit nuanced, here's everything you need to be put into your "CSPROJ" (TargetDir is a built in variable, included automatically) under the "Project" tag:

<Target Name="RemoveFilesAfterBuild">   
    <ItemGroup>
        <XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
        <PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
    </ItemGroup>
    <Delete Files="@(XMLFilesToDelete)" />
    <Delete Files="@(PDBFilesToDelete)" />
</Target>

I've also had trouble with various language specific folders being generated, if you have that issue too, you can also remove unused language specific folders too. I've chosen to only trigger this under the build type "Release":

<ItemGroup>
    <FluentValidationExcludedCultures Include="be;cs;cs-CZ;da;de;es;fa;fi;fr;ja;it;ko;mk;nl;pl;pt;ru;sv;tr;uk;zh-CN;zh-CHS;zh-CHT">
        <InProject>false</InProject>
    </FluentValidationExcludedCultures> 
</ItemGroup>

<Target Name="RemoveTranslationsAfterBuild" AfterTargets="AfterBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <RemoveDir Directories="@(FluentValidationExcludedCultures->'$(OutputPath)%(Filename)')" />

    <ItemGroup>
        <XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
        <PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
    </ItemGroup>
    <Delete Files="@(XMLFilesToDelete)" />
    <Delete Files="@(PDBFilesToDelete)" />
</Target>

Upvotes: 3

Jeffrey Zhao
Jeffrey Zhao

Reputation: 5023

My answer might be trivial now but I'd like to share the BAT script I use to delete the xml files if there's a corresponding dll for it. It's useful if you just want to cleanup the output folder and has other xml files that don't want to remove.

SETLOCAL EnableDelayedExpansion

SET targetDir=%1

ECHO Deleting unnecessary XML files for dlls

FOR %%F IN (%targetDir%*.xml) DO (

  SET xmlPath=%%~fF
  SET dllPath=!xmlPath:.xml=.dll!

  IF EXIST "!dllPath!" (
    ECHO Deleting "!xmlPath!"
    DEL "!xmlPath!"
  )
)

Usage:

Cleanup.bat c:\my-output-folder\

It took me an hour to finish this simple work (thanks to the "delayed expansion" stuff) with all type of searching here and there. Hope it helps other BAT newbies like me.

Upvotes: 0

mwjackson
mwjackson

Reputation: 5451

You can also specify this via the command line:

MsBuild.exe build.file /p:AllowedReferenceRelatedFileExtensions=none

Upvotes: 75

Dingo
Dingo

Reputation: 341

This is a rather old question, but since there is no answer about how to turn off generating PDB and XML files via UI, i figured that it should be here for completeness.

In Visual Studio 2013: in project properties, under compile tab, uncheck "Generate XML documentation file", then click on "Advanced Compile Options" below that and change "Generate debug info" to "None", and that will do the trick.

Upvotes: 5

ProVega
ProVega

Reputation: 5914

If you only want to exclude the XML files (for say a debug release) you can do something like this:

<AllowedReferenceRelatedFileExtensions>
  <!-- Prevent default XML from debug release  -->
      *.xml
 </AllowedReferenceRelatedFileExtensions>

Basically, each extension (delimited by a semi-colon) listed will be excluded.

Upvotes: -4

Jason Morse
Jason Morse

Reputation: 6304

I wanted to be able to add and remove referenced assemblies in my primary application while avoiding the the need to maintain which files I needed to delete or exclude.

I dug through Microsoft.Common.targets looking for something that would work and found the AllowedReferenceRelatedFileExtensions property. It defaults to .pdb; .xml so I explicitly defined it in my project file. The catch is that you need something (whitespace is not sufficient) otherwise it will still use the default.

<Project ...>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <AllowedReferenceRelatedFileExtensions>
      <!-- Prevent default XML and PDB files copied to output in RELEASE. 
           Only *.allowedextension files will be included, which doesn't exist in my case.
       -->
      .allowedextension
    </AllowedReferenceRelatedFileExtensions> 
  </PropertyGroup>

Upvotes: 177

AndrewJacksonZA
AndrewJacksonZA

Reputation: 1273

You can add a Post Build event command similar to del "$(TargetDir)YourAssembly*.xml", "$(TargetDir)YourAssembly*.pdb"

Upvotes: 25

Related Questions