rules_dotnet icon indicating copy to clipboard operation
rules_dotnet copied to clipboard

Download correct app-host for the target RID in order to support cross-compilation

Open njlr opened this issue 2 years ago • 2 comments

Full repro here: https://github.com/njlr/rules-dotnet-publish-docker

load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_dotnet//dotnet:defs.bzl", "fsharp_binary", "publish_binary")

fsharp_binary(
  name = "app",
  srcs = [
    "Program.fs",
  ],
  target_frameworks = [
    "net6.0",
  ],
  deps = [
    "@paket.main//microsoft.netcore.app.ref",
    "@paket.main//fsharp.core",
  ],
  visibility = [
    "//visibility:public",
  ],
)

publish_binary(
  name = "app_linux_x64",
  binary = ":app",
  target_framework = "net6.0",
  self_contained = True,
  runtime_identifier = "linux-x64",
  runtime_packs = [
    "@paket.main//microsoft.netcore.app.runtime.linux-x64",
  ],
)

pkg_tar(
  name = "app_linux_x64_archive",
  srcs = [
    ":app_linux_x64",
  ],
  strip_prefix = "app_linux_x64/publish/linux-x64",
  include_runfiles = True,
)

container_image(
  name = "image_debian",
  base = "@dotnet_runtime_deps_6_0_10//image",
  directory = "/home",
  workdir = "/home",
  entrypoint = "/home/app",
  tars = [
    ":app_linux_x64_archive",
  ],
)

publish_binary(
  name = "app_alpine_x64",
  binary = ":app",
  target_framework = "net6.0",
  self_contained = True,
  runtime_identifier = "alpine-x64",
  runtime_packs = [
    "@paket.main//microsoft.netcore.app.runtime.linux-musl-x64",
  ],
)

pkg_tar(
  name = "app_alpine_x64_archive",
  srcs = [
    ":app_alpine_x64",
  ],
  strip_prefix = "app_alpine_x64/publish/alpine-x64",
  include_runfiles = True,
)

container_image(
  name = "image_alpine",
  base = "@dotnet_runtime_deps_alpine_3_16//image",
  directory = "/home",
  workdir = "/home",
  entrypoint = "/home/app",
  tars = [
    ":app_alpine_x64_archive",
  ],
)

The Debian based image works fine:

$ bazel run //:image_debian

$ docker run -it --rm bazel:image_debian                  
Hello, world. 

However, running the Alpine image gives an error:

$ bazel run //:image_alpine

$ docker run -it --rm bazel:image_alpine
/bin/sh: line 0: /home/app: not found

Note that the file does exist, this is the error Alpine gives when the binary format is not properly recognized!

njlr avatar Dec 06 '22 18:12 njlr

Ah yeah, this makes sense. Currently we are only using the app host that is part of the host SDK. We need to stop doing to that and instead download the correct nuget package depending on the RID e.g. https://www.nuget.org/packages/runtime.linux-x64.Microsoft.NETCore.DotNetAppHost for linux-x64

By doing that we also solve cross compiling as this is pretty much the same issue.

purkhusid avatar Dec 07 '22 10:12 purkhusid

I'm thinking that we might just want to start with just having an apphost attribute to begin with and the end user supplies the correct apphost nuget package. At some point I would like the rules to automatically resolve these "infrastructure" NuGet packages .

purkhusid avatar Dec 07 '22 10:12 purkhusid

This should not be working in the latest release.

purkhusid avatar Apr 04 '24 10:04 purkhusid

Thanks!

I believe that some runtime packs are missing to enable this.

The alpine-x64 RID maps to linux-musl-x64, which has the runtime pack Microsoft.NETCore.App.Runtime.linux-musl-x64

Would it simply be a matter of adding these to here? https://github.com/bazelbuild/rules_dotnet/blob/master/dotnet/private/sdk/gen/runtime-packs.json

njlr avatar Apr 04 '24 12:04 njlr

@njlr I just merged a change that adds the packs, I'm doing a 0.15.1 release in a bit.

purkhusid avatar Apr 04 '24 12:04 purkhusid

@njlr I just merged a change that adds the packs, I'm doing a 0.15.1 release in a bit.

This works! Thank you :+1:

njlr avatar Apr 04 '24 18:04 njlr