paketo-buildpacks/dotnet-publish

Building apps with `--self-contained=true`

Closed this issue · 3 comments

fg-j commented

What happened?

  • What were you attempting to do?

I wanted to build a .NET app that optimizes the size of its assemblies.

  • What did you expect to happen?

I expected the buildpack to either a) automatically detect that the build should run with --self-contained=true or b) provide a config option that lets me specify that I want that flag set.

  • What was the actual behavior? Please provide log output, if possible.
git clone git@github.com:fg-j/Blogifier.git
git co -b add-buildpack-config
pack build blogifier-demo --buildpack gcr.io/paketo-buildpacks/dotnet-core:0.1.1
...
...
...
Paketo .NET Publish Buildpack 0.1.3
  Executing build process
    Running 'dotnet publish /workspace/src/Blogifier --configuration Release --runtime ubuntu.18.04-x64 --self-contained false --output /tmp/dotnet-publish-output892641884'
      Failed after 3m0.1765225s

        Welcome to .NET 5.0!
        ---------------------
        SDK Version: 5.0.102

        Telemetry
        ---------
        The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

        Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

        ----------------
        Installed an ASP.NET Core HTTPS development certificate.
        To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
        Learn about HTTPS: https://aka.ms/dotnet-https
        ----------------
        Write your first app: https://aka.ms/dotnet-hello-world
        Find out what's new: https://aka.ms/dotnet-whats-new
        Explore documentation: https://aka.ms/dotnet-docs
        Report issues and find source on GitHub: https://github.com/dotnet/core
        Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
        --------------------------------------------------------------------------------------
        Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
        Copyright (C) Microsoft Corporation. All rights reserved.

          Determining projects to restore...
          Restored /workspace/src/Blogifier.Core/Blogifier.Core.csproj (in 1.36 min).
          Restored /workspace/src/Blogifier.Admin/Blogifier.Admin.csproj (in 1.58 min).
          Restored /workspace/src/Blogifier.Shared/Blogifier.Shared.csproj (in 50 ms).
          Restored /workspace/src/Blogifier/Blogifier.csproj (in 14.02 sec).
          Blogifier.Shared -> /workspace/src/Blogifier.Shared/bin/Release/net5.0/Blogifier.Shared.dll

          Bundler: Begin processing bundleconfig.json
          Bundler: Done processing bundleconfig.json
          Blogifier.Admin -> /workspace/src/Blogifier.Admin/bin/Release/net5.0/Blogifier.Admin.dll
          Blogifier.Admin (Blazor output) -> /workspace/src/Blogifier.Admin/bin/Release/net5.0/wwwroot
          Blogifier.Core -> /workspace/src/Blogifier.Core/bin/Release/net5.0/Blogifier.Core.dll
          Blogifier -> /workspace/src/Blogifier/bin/Release/net5.0/ubuntu.18.04-x64/Blogifier.dll
          Blogifier -> /workspace/src/Blogifier/bin/Release/net5.0/ubuntu.18.04-x64/Blogifier.Views.dll
        /layers/paketo-buildpacks_dotnet-core-sdk/dotnet-core-sdk/sdk/5.0.102/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.ILLink.targets(143,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. [/workspace/src/Blogifier.Admin/Blogifier.Admin.csproj]
failed to execute 'dotnet publish': exit status 1
ERROR: failed to build: exit status 1
ERROR: failed to build: executing lifecycle: failed with status code: 145

Build Configuration

  • What platform (pack, kpack, tekton buildpacks plugin, etc.) are you
    using? Please include a version.
pack --version
0.17.0+git-d9cb4e7.build-2045
  • What buildpacks are you using? Please include versions.
paketo-buildpacks/dotnet-core:0.1.1
  • What builder are you using? If custom, can you provide the output from pack inspect-builder <builder>?

  • Can you provide a sample app or relevant configuration (buildpack.yml,
    nginx.conf, etc.)?

See the add-buildpack-config branch of my fork of the Blogifier repo.

Checklist

  • I have included log output.
  • The log output includes an error message.
  • I have included steps for reproduction.
fg-j commented

I tried building this repo on my local.

dotnet --version
5.0.103

First, used the dotnet publish command with the flags used by the buildpack. This resulted in the same failure:

error NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. [/workspace/src/Blogifier.Admin/Blogifier.Admin.csproj]

Then, removed the --self-contained=false flag to see what the behaviour is

dotnet publish src/Blogifier --configuration Release --runtime ubuntu.18.04-x64 --output /tmp/dotnet-publish-output771589643
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Shared/Blogifier.Shared.csproj (in 6.05 sec).
  Restored /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Core/Blogifier.Core.csproj (in 27.71 sec).
  Restored /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Admin/Blogifier.Admin.csproj (in 35.15 sec).
  Restored /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier/Blogifier.csproj (in 35.41 sec).
  Blogifier.Shared -> /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Shared/bin/Release/net5.0/Blogifier.Shared.dll

  Bundler: Begin processing bundleconfig.json
  Bundler: Done processing bundleconfig.json
  Blogifier.Core -> /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Core/bin/Release/net5.0/Blogifier.Core.dll
  Blogifier.Admin -> /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Admin/bin/Release/net5.0/Blogifier.Admin.dll
  Blogifier.Admin (Blazor output) -> /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier.Admin/bin/Release/net5.0/wwwroot
  Blogifier -> /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier/bin/Release/net5.0/ubuntu.18.04-x64/Blogifier.dll
  Blogifier -> /Users/pivotal/workspace/fg-j/Blogifier/src/Blogifier/bin/Release/net5.0/ubuntu.18.04-x64/Blogifier.Views.dll
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  Compressing Blazor WebAssembly publish artifacts. This may take a while...
  Blogifier -> /tmp/dotnet-publish-output771589643/

The log output includes Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink Compressing Blazor WebAssembly publish artifacts. This may take a while...

According to the Blazor Web Assemblies section of this blog post about .NET 5, it seems that Blazor apps attempt to optimize their size during build. This is what breaks when we specify --self-contained=false.

It looks like some of the project files in the project use the Project XML tag <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">, which tells .NET that it's a Blazor app (and that it should optimize the build artifacts).

The buildpack should probably look in project file(s) used to build the app and vary the flags passed to dotnet publish accordingly. Notably, the main project file at src/Blogifier/Blogifier.csproj does not have the BlazorWebAssembly tag, but the src/Blogifier.Admin/Blogifier.Admin.csproj file that is in the main projfile Include does have the BlazorWebAssembly tag.

An RFC is being written/considered to add support in this area.

fg-j commented

With #246 implemented, this sample app now builds!

pack build blogifier-demo --buildpack gcr.io/paketo-buildpacks/dotnet-core-runtime --buildpack gcr.io/paketo-buildpacks/dotnet-core-aspnet --buildpack gcr.io/paketo-buildpacks/dotnet-core-sdk --buildpack gcr.io/paketo-buildpacks/icu --buildpack ~/workspace/paketo-buildpacks/dotnet-publish/build/buildpack.tgz --buildpack gcr.io/paketo-buildpacks/dotnet-execute --env BP_DOTNET_PROJECT_PATH="./src/Blogifier" --env BP_DOTNET_PUBLISH_FLAGS="--self-contained=true"
[builder] Paketo .NET Publish Buildpack 9.9.9
[builder]   Executing build process
[builder]     Running 'dotnet publish /workspace/src/Blogifier --configuration Release --runtime ubuntu.18.04-x64 --output /tmp/dotnet-publish-output114730965 --self-contained=true'
[builder]       Completed in 3m43.429660682s
[builder]
[builder]   Removing source code
[builder]
[builder] Paketo .NET Execute Buildpack 0.5.1
[builder]   Assigning launch processes
[builder]     web: /workspace/Blogifier --urls http://0.0.0.0:${PORT:-8080}