PublishTrimmed should respect PublishSelfContained
See original GitHub issueDescription
When publishing a trimmed app, I am currently getting an error:
Microsoft.NET.ILLink.targets(196,5): error NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. [C:\DotNetTest\SelfContainedTest\SelfContainedTest.csproj]
Even though I’m setting PublishSelfContained=true
in my .csproj. I am also setting SelfContained=false
because I don’t want all of the runtime copied into my build output directory. I have many applications that I’m publishing (test apps in dotnet/aspnetcore) and each application is taking up ~100 MB in the bin
directory. This is unnecessary bloat.
Reproduction Steps
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishSelfContained>true</PublishSelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<!-- Set SelfContained=false to prevent the whole runtime from being copied into the build output directory -->
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
Running dotnet msbuild /t:Publish
on that project will fail. It will also fail just for:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishSelfContained>true</PublishSelfContained>
</PropertyGroup>
</Project>
Expected behavior
I expect both apps above to publish successfully.
Actual behavior
Both of them produce the above error.
Regression?
No response
Known Workarounds
Explicitly specify SelfContained=true
, but that causes bloat (will eventually be GBs) in my bin directory.
Configuration
No response
Other information
No response
Issue Analytics
- State:
- Created 5 months ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Trim self-contained applications - .NET
Learn how to trim self-contained apps to reduce their size. .NET Core bundles the runtime with an app that is published self-contained and ......
Read more >Untitled
PublishTrimmed should respect PublishSelfContained #32272 Web15 thg 3, 2023 · A target element can have both Inputs and Outputs attributes, indicating what ...
Read more >Should I use self-contained or framework-dependent ...
In this post I compare the impact of the framework-dependent vs self-contained mode on Docker image size, taking layer caching into account.
Read more >Azure DevOps: publish self-contained .net Core app with ...
I need to create a self-contained .net core (3.1 in my case) app and to pack & publish using chocolatey so it can...
Read more >NET 5.0 App Trimming and Potential for Future Progress
In this article we will: ... Publish self-contained HelloWorld weights 65MB! ... I hope – and bet – that app trimming will do...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
PublishSelfContained
does not work with MSBuild publish, you need to dodotnet publish
or forward the property_IsPublishing
to MSBuild publish.@eerhardt I hear you. We tried very hard to get it to work for MSBuild publish. It also doesn’t work inside of VS outside of the context of running a
dotnet publish
.There are several reasons we landed here. We could technically create some logic in MSBuild to make it work, but it wouldn’t work very well. Adding this to MSBuild would break several key philosophies of MSBuild. One of the problems is that people need to be able to write their own custom publish targets or call the Publish target in their build whenever. We don’t know at the very beginning of the build if someone is going to inject a publish target. But we need the
_IsPublishing
property to be defined by the beginning of the build/publish, as Configuration depends on it as well as properties likePublishSelfContained
andPublishRuntimeIdentifier
, and for some of these cases any time before the beginning of the build/publish is too late for it to work. @rainersigwald would be able to provide better context.One of the only ways (if the only) to get around that is to run the build/publish twice, so you can see in the 1st time if publish is ever invoked and commmunicate that to the 2nd run. But that’s pretty bad.
This is a good suggestion and we could do that. But it sounds like there are problems even if that’s used, which is unfortunate. I am trying to think if there is any other solution for this scenario.