rules_dotnet icon indicating copy to clipboard operation
rules_dotnet copied to clipboard

Is there an equivalent to dotnet publish?

Open njlr opened this issue 2 years ago • 7 comments

Using dotnet publish it is possible to generate a folder with a DLL and all of its dependencies.

Is there an equivalent in rules_dotnet?

njlr avatar Mar 26 '22 16:03 njlr

@njlr There is no such support at the moment in these rules. Can I ask what the exact use case you have is?

purkhusid avatar Mar 30 '22 09:03 purkhusid

For example to deploy an AWS Lambda it is necessary to build a .zip that contains the .NET library and any dependencies.

This can be done outside Bazel with dotnet publish and an appropriate zip command on the publish folder in bin.

njlr avatar Mar 30 '22 09:03 njlr

I do something similar for docker where I use a filegroup to gather the runfiles.

load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

def dotnet_lambda_image(name, bin, **kwargs):
    pkg_tar(
        name = "%s_app_layer" % name,
        srcs = [bin],
        include_runfiles = True,
        mode = "0755",
        package_dir = "/var/task",
    )

    bin_label = Label(bin)

    container_image(
        name = name,
        base = "@dotnet_lambda_base//image",
        entrypoint = [
            "/bootstrap.sh",
            "%s" % bin_label.name,
        ],
        env = {
            "DOTNET_ROOT": "/var/lang/bin",
        },
        tars = ["%s_app_layer" % name],
        **kwargs
    )

You can possibly do something similar using https://github.com/bazelbuild/rules_pkg to construct the zip file.

purkhusid avatar Mar 30 '22 12:03 purkhusid

I went with using containers and AWS lambda instead of the ZIP since I already had the whole container building workflow setup in our repo

purkhusid avatar Mar 30 '22 12:03 purkhusid

Thanks for your help.

Unfortunately I ran into 2 issues:

  1. pkg_zip does not have an include_runfiles argument
  2. using pkg_tar, the set of runfiles seems to be too big, e.g. System.Collections.dll should not be included as that is provided by AWS Lambda

njlr avatar Mar 30 '22 21:03 njlr

@njlr Sorry, I forgot to respond to you and was going through the issues now.

Regarding the runfiles list being too large then I think I'll try to find a way to have 2 different fsharp/csharp_binary rules where one behaves like dotnet publish -r <os-arch> which would include the runtime and the other one would not include the runtime.

I don't think I'll add it to the master branch but the next branch since we are actively working on all kinds of improvements there.

purkhusid avatar Jun 15 '22 09:06 purkhusid

Thanks @purkhusid.

I was thinking a design like this could work well:

fsharp_library(
  name = "foo",
  srcs = [
    "Library.fs",
  ],
)

dotnet_publish(
  name = "foo_pub",
  lib = ":foo",
)

njlr avatar Jun 20 '22 20:06 njlr

There is now an publish_binary rule on the next branch that can publish self contained and framework dependent binaries. Examples are in the examples folder.

purkhusid avatar Aug 31 '22 15:08 purkhusid