Overview

This is a minimal viable set of C# bindings for building C# code with .NET Core.

Caveats

Bazel creates long paths. Therefore it is recommended to increase the length limit using newer version of Windows. Please see here.

However, some Windows programs do not handle long path names. Most notably - Microsoft C compiler (cl.exe). Therefore TMP env variable should be set to something short (like X:\ or c:\TEMP).

Bazel and dotnet_rules rely on symbolic linking. On Windows it, typically, requires elevated permissions. However, newer versions of Windows have a workaround.

Please see here for more details.

Setup

  • The rules take full advantage of Bazel platforms and toolchains

  • When building any project the platform has to be specified. For example:

        bazel build --host_platform //dotnet/toolchain:linux_amd64_2.2.402 --platforms //dotnet/toolchain:linux_amd64_2.2.402 //...
    
  • The platform specification has the form of //dotnet/toolchain:. The available values are listed in dotnet/platform/list.bzl in variables DOTNET_OS_ARCH and DOTNET_CORE_FRAMEWORKS. Typically the --host_platform and --platforms values are set in .bazelrc file.

  • Add the following to your WORKSPACE file to add the external repositories:

    
      # A newer version should be fine
      load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
      git_repository(
          name = "io_bazel_rules_dotnet",
          remote = "https://github.com/bazelbuild/rules_dotnet",
          branch = "master",
      )
    
      load("@io_bazel_rules_dotnet//dotnet:deps.bzl", "dotnet_repositories")
      dotnet_repositories()
    
      load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "core_register_sdk", "dotnet_register_toolchains", "dotnet_repositories_nugets")
      dotnet_register_toolchains()
      dotnet_repositories_nugets()
    

    The dotnet_repositories rule fetches external dependencies which have to be defined before loading any other file of rules_dotnet. dotnet_repositories_nugets loads nuget packages required by test rules.

    The dotnet_register_toolchains configures toolchains.

  • Add a file named BUILD.bazel in the root directory of your project. In general, you need one of these files in every directory with dotnet code. At the top of the file used rules should be imported:

      load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "csharp_library", "csharp_binary")
    
  • Depending on .NET Core version used it may be necessary to install libunwind-devel on Linux systems.

  • See nuget2bazel for handling Nuget dependencies.

Advanced usage