question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PublishTrimmed should respect PublishSelfContained

See original GitHub issue

Description

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:open
  • Created 5 months ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
nagilsoncommented, May 4, 2023

PublishSelfContained does not work with MSBuild publish, you need to do dotnet publish or forward the property _IsPublishing to MSBuild publish.

0reactions
nagilsoncommented, May 5, 2023

That seems like a mistake.

@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 like PublishSelfContained and PublishRuntimeIdentifier, 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.

If we want users to forward/check/set that property, we should give it an official name without an _ at the front

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found