bazel-contrib / rules_oci

Bazel rules for building OCI containers
Apache License 2.0
280 stars 146 forks source link

Migrating to Bazel 7.0 cause issue with the CC compiler not being found #472

Closed supercairos closed 5 months ago

supercairos commented 7 months ago

I'm using this code to generate docker image. It works perfectly on Bazel 6.4.0 but when I run it on Bazel 7.0 I get this error:

ERROR: /private/var/tmp/_bazel_romain/aab898f10bfeb90def8d69f3c9616e24/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @@bazel_tools//tools/cpp:current_cc_toolchain: Traceback (most recent call last): File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 219, column 17, in _find_cpp_toolchain Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//tools/bazel:linux_arm64, Exec platform: @@local_config_platform//:host ERROR: /private/var/tmp/_bazel_romain/aab898f10bfeb90def8d69f3c9616e24/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed

load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_image_layer")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_push", "oci_tarball")

def oci_js_binary_image(name, binary_data, binary_entry_point = None, binary_args = [], **kwargs):
    """Simplify the use of oci_image building properly the associated targets

    Args:
        name: name of this target. used to derive the name of the final targets
        binary_data: list of labels to use in js_binary data field
        binary_entry_point: label of the binary entrypoint
        binary_args: list of arguments passed to the binary when running on the image
        **kwargs: Passed to oci_image.
    """
    js_binary(
        name = "{}_img-bin".format(name),
        data = binary_data,
        entry_point = ":src/index.js" if (binary_entry_point == None) else binary_entry_point,
    )

    oci_js_image(
        binary_target = ":{}_img-bin".format(name),
        binary_args = binary_args,
        name = name,
        **kwargs
    )

def oci_js_image(name, binary_target, binary_args = [], **kwargs):
    """Simplify the use of oci_image building properly the associated targets

    Args:
        name: name of this target. used to derive the name of the final targets
        binary_target: label to use in js_image_layer binary
        binary_args: list of arguments passed to the binary when running on the image
        **kwargs: Passed to oci_image.
    """

    js_image_layer(
        name = "{}_img-layer".format(name),
        binary = binary_target,
        platform = select({
            "@platforms//cpu:arm64": "//tools/bazel:linux_arm64",
            "@platforms//cpu:x86_64": "//tools/bazel:linux_amd64",
        }),
        root = "/app",
        tags = [
            "manual",
        ],
        visibility = ["//visibility:__pkg__"],
    )

    oci_image(
        name = "{}_img".format(name),
        base = "@debian",
        # This is going to be /{root of js_image_layer}/{package_name()}/{name of js_binary}.sh
        cmd = [
            "/app/{}/{}".format(
                native.package_name(),
                Label(binary_target).name,
            ),
        ] + binary_args,
        entrypoint = ["bash"],
        tags = [
            "manual",
        ],
        tars = [
            ":{}_img-layer".format(name),
        ],
        user = "www-data",
        workdir = "/app",
        **kwargs
    )

    expand_template(
        name = "{}_img-stamp".format(name),
        out = "_stamped.tags.txt",
        template = "//tools/bazel:img_tags_tmpl",
        substitutions = {
            "STABLE_BUILD_TAG": "registry.gitlab.com/xxx/{}:latest".format(
                native.package_name(),
            ),
        },
        stamp_substitutions = {
            "STABLE_BUILD_TAG": "registry.gitlab.com/xxx/{}:{{STABLE_BUILD_TAG}}".format(
                native.package_name(),
            ),
        },
    )

    oci_tarball(
        name = "{}_img-tar".format(name),
        image = ":{}_img".format(name),
        repo_tags = ":{}_img-stamp".format(name),
        visibility = ["//visibility:public"],
    )

    oci_push(
        name = "{}_img-push".format(name),
        image = ":{}_img".format(name),
        repository = "registry.gitlab.com/blade-group/dev/cbp-shadow/oidc/{}".format(
            native.package_name(),
        ),
        remote_tags = ":{}_img-stamp".format(name),
        visibility = ["//visibility:public"],
    )
supercairos commented 7 months ago

I'm using rules_oci 1.6.0.

mortenmj commented 7 months ago

Could you test this with --noincompatible_enable_cc_toolchain_resolution? That flag was flipped in 7.0.0.

supercairos commented 7 months ago

Hi @mortenmj,

It works perfectly with the flag --noincompatible_enable_cc_toolchain_resolution Does it means my machine have incompatible cc toolchains ?

I'm on a Apple PC 14" with a M1 (arm64)

I have clang installed via xcode:

 ✘ romain@MacBook-Pro-de-Romain  ~  clang --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
thesayyn commented 5 months ago

This is not related to rules_oci. Can you file an issue in Bazel instead?