Brightspace / rules_csharp

Bazel rules for C#
Apache License 2.0
8 stars 5 forks source link

Compilation referencing classes in specific framework namespaces fails under Bazel #132

Closed jimevans closed 4 years ago

jimevans commented 4 years ago

Consider the following C# source file (CompileIssue.cs):

using System.Runtime.InteropServices;

namespace BazelCompileIssue
{
    public class CompileIssue
    {
        public string ShowIssue()
        {
            string platformValue = "Not macOS";
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                platformValue = "macOS";
            }

            return platformValue;
        }
    }
}

Further consider the following Bazel build definition file (in the BazelCompileIssue directory):

load("@d2l_rules_csharp//csharp:defs.bzl", "csharp_library")

csharp_library(
    name = "CompileIssue",
    srcs = [
        "CompileIssue.cs",
    ],
    target_frameworks = [
        "net47",
    ],
)

Attempting to build such a project using bazel build //BazelCompileIssue:CompileIssue yields the following output:

INFO: Analyzed target //BazelCompileIssue:CompileIssue (1 packages loaded, 2 targets configured). INFO: Found 1 target... ERROR: C:/projects/csharp-bazel-rules-issue/BazelCompileIssue/BUILD.bazel:3:1: Compiling CompileIssue failed (Exit -1) dotnet failed: Invalid argument BazelCompileIssue\CompileIssue.cs(15,17): error CS0103: The name 'RuntimeInformation' does not exist in the current context BazelCompileIssue\CompileIssue.cs(15,49): error CS0103: The name 'OSPlatform' does not exist in the current context Target //BazelCompileIssue:CompileIssue failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 1.458s, Critical Path: 0.59s INFO: 0 processes. FAILED: Build did NOT complete successfully

This same file compiles fine within Visual Studio. Removing any calls to classes in the System.Runtime.InteropServices namespace allows successful compilation under Bazel. A sample Git repo with the above code demonstrating the problem can be found on GitHub.

jimevans commented 4 years ago

Okay, never mind. These types are documented as only being introduced in .NET Framework 4.7.1. The framework moniker net47 won't find those types. This is working as intended.

j3parker commented 4 years ago

Thanks for the detailed issue + the follow-up!