sdk icon indicating copy to clipboard operation
sdk copied to clipboard

dotnet publish - way to remove pdb and other files

Open saxenark opened this issue 6 years ago • 41 comments

Hi,

I am using dotnet publish to publish my dotnet core 2.2 web application. I notice that the published folder has several unrequired files, specifically .pdbs, .appsettings.Development, .runtimeconfig.dev etc.

Qn: Is there a way dotnet publish command can be made to exclude these files or would I have to delete these manually?

Thanks

saxenark avatar Oct 24 '19 12:10 saxenark

@livarcocc @jeffschwMSFT

scalablecory avatar Oct 28 '19 18:10 scalablecory

Hi @scalablecory @livarcocc @jeffschwMSFT,

please let me know if there is a way to remove these unnecessary files during publish.

saxenark avatar Oct 30 '19 09:10 saxenark

Same question with dotnet core 3.1 web application. Any way to remove those unrequired files using command line parameters or .csproj PropertyGroup properties? DebugType=None and DebugSymbols=false ~~seem to be ignored.~~

ducalai avatar Mar 11 '20 10:03 ducalai

Using Property group in .csproj with <DebugType>None</DebugType> and <DebugSymbols>false</DebugSymbols> must be done on each referenced .csproj to apply on each referenced assemblies.

Using dotnet publish parameters /p:DebugType=None /p:DebugSymbols=false apply on each referenced assemblies.

ducalai avatar Mar 11 '20 12:03 ducalai

@saxenark did the answer given by @ducalai solve your problem?

carlossanlop avatar Sep 18 '20 21:09 carlossanlop

I want to point at the inconsistency of how the parameters applied: I tried to specify

<DebugType>None</DebugType>
<DebugSymbols>False</DebugSymbols>

In my \PublishProfiles\FolderProfile.pubxml, it works only for the project you publish, not for references. I tried to do as @ducalai suggested - added publish parameters /p:DebugType=None /p:DebugSymbols=false and it worked for references too. BTW the same thing with -p:Configuration=Release

LevYas avatar Apr 14 '21 13:04 LevYas

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.

ghost avatar Apr 16 '21 18:04 ghost

Issue still here.

  • I want .pdb in release folder but not in publish.
  • I don't want use DebugType for each project, this is redundancy, it should worked when use on main project only.
  • I don't like use command line, I use VS publish button.
  • How about .xml doc file?

ahdung avatar Apr 19 '21 00:04 ahdung

I have the same issue as @ahdung. I want to exclude some massive XML docs from integrations with other services and only keep the API docs for Swashbuckle.

I believe the real issue is that <Content Update="XXXX" CopyToPublishDirectory="Never" /> doesn't work for files generated by the buid process.

See this:

    <ItemGroup>
        <Content Update="appsettings*.json" CopyToPublishDirectory="Never" />
        <Content Update="*.xml" CopyToPublishDirectory="Never" />
    </ItemGroup>

If you put that in your *.pubxml file, only the appsettings*.json files wil be excluded. As far as I'm concerned that is a bug because at the time of publishing there should be no difference in how you treat files in the build output folder.

AGlezB avatar Aug 27 '21 16:08 AGlezB

The problem with the solution above is it's always excluded. But I need it excluded only for release builds. This works:

<PropertyGroup Condition="'$(Configuration)'=='Release'">
  <DebugSymbols>False</DebugSymbols>
  <DebugType>None</DebugType>
</PropertyGroup>

lonix1 avatar May 31 '22 13:05 lonix1

@jeffschwMSFT any progress on this? As of net7.0 we still cannot exclude certain files.

AGlezB avatar Nov 24 '22 12:11 AGlezB

@agocke should this issue be in SDK or runtime?

jeffschwMSFT avatar Nov 25 '22 16:11 jeffschwMSFT

SDK. The proposal as stands is some sort of property to affect all publish scenarios, which is core SDK logic.

agocke avatar Nov 25 '22 22:11 agocke

Also interested in this feature for NativeAOT scenarios. Right now with a minimal api app the app is 25MB and the symbols 80MB (using StripSymbols on Linux). It's not obvious that you can get rid of the .dbg file if you care about the size (for most users I'd say). I would expect that these arguments would work on dotnet publish at least, and globally. Right now the .dbg file is still generated with these two arguments.

sebastienros avatar Dec 09 '22 23:12 sebastienros

@sebastienros I think there might be two separate scenarios here, actually.

  1. Produce no symbols at all, anywhere.
  2. Don't produce symbols during publish, but do produce them for build.

I think (1) can be a Native AOT specific issue that we should respect <DebugSymbols>false</> and <DebugType>none</>. (2) is the new scenario as it would only affect publish scenarios.

Broadly though, I don't see how this would help the goal of informing users that they don't need to carry around the .dbg file. This seems like basically the same UX as .pdb files and I think the current expectation is that users know that they don't need to copy the PDB file around.

agocke avatar Dec 09 '22 23:12 agocke

Agreed. I want 2) here.

sebastienros avatar Dec 10 '22 00:12 sebastienros

If you are experiencing all kinds of junk files in your publish output directory, it may be because you have set <PublishDir> and <PublishUrl> to the same value in your .pubxml profile. This apparent bug has been reported to Microsoft here.

HughWarrington avatar Jan 16 '23 12:01 HughWarrington

adding the following PropertyGroup to the .pubxml seems to have solved the issue for me:

  <PropertyGroup>
    <DebugType>none</DebugType>
    <GenerateDocumentationFile>false</GenerateDocumentationFile>
    <AllowedReferenceRelatedFileExtensions>none</AllowedReferenceRelatedFileExtensions>
    <NoWarn>$(NoWarn);SA0001</NoWarn>
  </PropertyGroup>

Update [2023-08-21]: Setting these properties within .pubxml only works when performing publish from within Visual Studio. For command line dotnet publish, DebugType needs to be specified as command line parameter to take effect:

dotnet publish -p DebugType=none

6bee avatar Mar 31 '23 21:03 6bee

Since this was filed, the publish is down to a minimum set (exe, dll, runtimeconfig, deps, and pdb). pdb can be disabled with DebugType. Marking fixed. PublishDocumentationFile can also be used to disable publish for that particular file. https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#publishdocumentationfile

marcpopMSFT avatar Apr 19 '23 20:04 marcpopMSFT

Vì điều này đã được gửi, xuất bản được giảm xuống mức tối thiểu (exe, dll, runtimeconfig, deps và pdb). pdb có thể bị tắt bằng DebugType. Đánh dấu cố định. PublishDocumentationFile cũng có thể được sử dụng để tắt xuất bản cho tệp cụ thể đó. https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#publishdocumentationfile

if i use p:DebugType=None it will cannot build native aot

NCLnclNCL avatar May 22 '23 03:05 NCLnclNCL

In my team we do store PDB files to symbol server. So I want to generate PDBs. At the same time we want to create Docker container with the application files, and this container must not contain PDB's (for security and size concerns). Nobody here demonstrated how that can be done AFAU.

dobsa avatar Aug 21 '23 11:08 dobsa

@dobsa You want to create a .snupkg using dotnet pack --include-symbols

lonix1 avatar Aug 21 '23 11:08 lonix1

In my team we do store PDB files to symbol server. So I want to generate PDBs. At the same time we want to create Docker container with the application files, and this container must not contain PDB's (for security and size concerns). Nobody here demonstrated how that can be done AFAU.

You may also specify the following properties, for PDB files to be included in extra symbols package (.snupkg):

<PropertyGroup>
  <IncludeSymbols>true</IncludeSymbols>
  <SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

See https://learn.microsoft.com/en-us/nuget/create-packages/symbol-packages-snupkg for more info.

6bee avatar Aug 21 '23 12:08 6bee

@dobsa You want to create a .snupkg using dotnet pack --include-symbols

I don't really mind how and where the symbols end up, as long as they are not present in the publish directory.

dobsa avatar Aug 21 '23 16:08 dobsa

In my team we do store PDB files to symbol server. So I want to generate PDBs. At the same time we want to create Docker container with the application files, and this container must not contain PDB's (for security and size concerns). Nobody here demonstrated how that can be done AFAU.

You may also specify the following properties, for PDB files to be included in extra symbols package (.snupkg):

<PropertyGroup>
  <IncludeSymbols>true</IncludeSymbols>
  <SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

See https://learn.microsoft.com/en-us/nuget/create-packages/symbol-packages-snupkg for more info.

May be we do something wrong, but we build a Linux executable and put the content of publish folder into Docker container. This publish folder seems to always contain the PDBs if we generated them. Sure I can delete *.pdb afterwards when building the conainer. But more reasonable option would be to have an option to skip copying them.

dobsa avatar Aug 21 '23 16:08 dobsa

May be we do something wrong, but we build a Linux executable and put the content of publish folder into Docker container. This publish folder seems to always contain the PDBs if we generated them. Sure I can delete *.pdb afterwards when building the conainer. But more reasonable option would be to have an option to skip copying them.

Thanks for clarifying. In this case you might want to specify DebugType=none as a command line argument when performing dotnet publish as a separate build step:

dotnet publish -p DebugType=none

6bee avatar Aug 21 '23 17:08 6bee

Was there a problem with the ExcludeFoldersFromDeployment and ExcludeFilesFromDeployment in .pubxml files? I think this issue began because those are not supported by dotnet publish. Maybe bringing those back would make this issue go away.

AGlezB avatar Aug 21 '23 18:08 AGlezB

May be we do something wrong, but we build a Linux executable and put the content of publish folder into Docker container. This publish folder seems to always contain the PDBs if we generated them. Sure I can delete *.pdb afterwards when building the conainer. But more reasonable option would be to have an option to skip copying them.

Thanks for clarifying. In this case you might want to specify DebugType=none as a command line argument when performing dotnet publish as a separate build step:

dotnet publish -p DebugType=none

Thanks for the tip. With this option, some PDBs are gone. But some are still left. Interesting.

Actually it seems that only the PDBs of the project I am publishing are skipped. But this project has many project dependencies. And pdbs from those other projects are just copied over for whatever mysterious reason.

dobsa avatar Aug 22 '23 17:08 dobsa

Was there a problem with the ExcludeFoldersFromDeployment and ExcludeFilesFromDeployment in .pubxml files? I think this issue began because those are not supported by dotnet publish. Maybe bringing those back would make this issue go away.

It seems similar as above. Only PDBs from the project being published are skipped. PDBs from dependent projects are copied over.

dobsa avatar Aug 22 '23 18:08 dobsa

IMO this issue should be re-opened. There is no way to skip copying PDB files into publish folder once they were generated. The only thing that worked for me was to delete them once they were copied, using .pubxml file:

  <Target Name="DeleteFiles" AfterTargets="Publish">
    <ItemGroup>
        <FilesToDelete Include="$(PublishDir)*.pdb"/>
    </ItemGroup>
    <Delete Files="@(FilesToDelete)">
    <Output
        TaskParameter="DeletedFiles"
        ItemName="FilesDeleted"/>
    </Delete>
    <Message Text="Deleted PDB files: @(FilesDeleted)" Importance="high" />
</Target>

dobsa avatar Aug 23 '23 05:08 dobsa