bazel-contrib / rules_oci

Bazel rules for building OCI containers
Apache License 2.0
305 stars 159 forks source link

Building oci_image on Windows (Git Bash) - Results in "No matching toolchains found for types @@aspect_bazel_lib~//lib:zstd_toolchain_type" #716

Open jasonbdly opened 1 month ago

jasonbdly commented 1 month ago

I'm working on migrating a bunch of disparate shell build scripts to Bazel. The project consists of a set of grpc servers written in Go and exposed as their own binaries, each of which are subsequently built into Containerfile images running on top of debian:bookworm-slim, and spun up in a local Kubernetes cluster via Docker Desktop.

The problem I'm running into occurs when attempting to build the oci_image target for any of these grpc servers (with the recommended toolchain_resolution_debug flag enabled):

$ bazelisk build //src/services/identity/exec:image --toolchain_resolution_debug='@@aspect_bazel_lib~//lib:zstd_toolchain_type'
WARNING: Build option --toolchain_resolution_debug has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
INFO: ToolchainResolution: Performing resolution of @@aspect_bazel_lib~//lib:zstd_toolchain_type for target platform @@platforms//host:host
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_darwin_amd64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: osx
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_darwin_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: osx, aarch64
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_amd64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_arm64//:zstd_toolchain is compatible with target plaform, searching for execution p      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_darwin_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: osx, aarch64
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_amd64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_arm64//:zstd_toolchain is compatible with target plaform, searching for execution p      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: osx, aarch64
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_amd64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
latforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux, aarch64
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_darwin_amd64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: osx
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_darwin_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: osx, aarch64
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_amd64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux
      ToolchainResolution:   Toolchain @@aspect_bazel_lib~~toolchains~zstd_linux_arm64//:zstd_toolchain is compatible with target plaform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: linux, aarch64
      ToolchainResolution: No @@aspect_bazel_lib~//lib:zstd_toolchain_type toolchain found for target platform @@platforms//host:host.
ERROR: C:/dev/go/curtain/src/services/identity/exec/BUILD:39:10: While resolving toolchains for target //src/services/identity/exec:image (b433496): No matching toolchains found for types @@aspect_bazel_lib~//lib:zstd_toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@@aspect_bazel_lib~//lib:zstd_toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
ERROR: Analysis of target '//src/services/identity/exec:image' failed; build aborted
INFO: Elapsed time: 1.956s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

IIUC, it looks like aspect_bazel_lib doesn't provide a toolchain for zstd that works on Windows? Is this a legitimate support issue for Windows, or have I misconfigured something somewhere? Are there any workarounds I can attempt to use? Is there any additional information I can provide to narrow this down? This may be at least partially related to https://github.com/bazel-contrib/rules_oci/issues/714, but I'm not totally sure.

Here's the relevant BUILD file for reference:

load("@rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_push")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

go_binary(
    name = "main",
    embed = [":exec_lib"],
    visibility = ["//visibility:public"],
)

go_library(
    name = "exec_lib",
    srcs = ["main.go"],
    importpath = "gitlab.com/jasonbdly/curtain/src/services/identity/exec",
    visibility = ["//visibility:private"],
    deps = [
        "//src/core/cell",
        "//src/core/service",
        "//src/services/identity",
        "//src/services/identity/models",
        "@io_gorm_driver_mysql//:mysql",
        "@io_gorm_gorm//:gorm",
    ],
)

pkg_tar(
    name = "tar",
    srcs = [":main"],
)

oci_image(
    name = "image",
    base = "@distroless_base",
    entrypoint = ["/identity-service"],
    tars = [":tar"],
)

# oci_push(
#     name = "push",
#     image = ":image",
#     # remote_tags = [""],
#     repository = "",
# )

And the MODULE.bazel file:

module(name = "curtain")

bazel_dep(name = "rules_go", version = "0.50.1")
bazel_dep(name = "rules_proto", version = "6.0.2")
bazel_dep(name = "aspect_bazel_lib", version = "2.9.1")
bazel_dep(name = "gazelle", version = "0.39.1")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.22.4")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")

# All *direct* Go dependencies of the module have to be listed explicitly.
use_repo(
    go_deps,
    "io_gorm_driver_mysql",
    "io_gorm_gorm",
    "org_golang_google_grpc",
    "org_golang_google_protobuf",
    "org_golang_x_crypto",
)

bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "rules_oci", version = "2.0.0")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
    name = "distroless_base",
    image = "gcr.io/distroless/base",
    platforms = ["linux/amd64"],
    # 'latest' is not reproducible, but it's convenient.
    # During the build we print a WARNING message that includes recommended 'digest' and 'platforms'
    # values which you can use here in place of 'tag' to pin for reproducibility.
    tag = "latest",
)
use_repo(oci, "distroless_base", "distroless_base_linux_amd64")

The output from bazelisk version:

$ bazelisk version
Bazelisk version: v1.22.0
Build label: 7.3.2
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Tue Oct 1 17:49:08 2024 (1727804948)
Build timestamp: 1727804948
Build timestamp as int: 1727804948
jasonbdly commented 1 month ago

Since this seems to be more closely related to the upstream bazel_lib package, I submitted an issue there as well: https://github.com/bazel-contrib/bazel-lib/issues/964. Hopefully this gets some discussion started about how Windows support could be added.

thesayyn commented 1 month ago

Yes, this needs to be fixed in bazel-lib before it can be fixed here.

FYI windows support here is close to non-existent as we don't have the resources to develop for windows. i would not expect this to work even if we fix zstd toolchain.

jasonbdly commented 2 weeks ago

Got it, thanks for clarifying!

For any other readers having similar issues, I was able to work around the lack of direct support for Windows with a few tweaks to my setup and workflow:

  1. Enabled WSL Integration for my primary WSL2 linux distro: image

    • This exposes the Docker CLI for your host machine's installation in your WSL instance.
  2. Cloned my repo into my WSL2 distro and started VSCode from that directory via code ..

    • I'm pretty sure you need the WSL extension installed in VSCode for this to work.
    • This spins up VSCode in your host machine with a remote connection to your WSL instance in the folder in which VSCode was started.

With these settings the development experience is pretty close to developing directly on the host machine, but the build process occurs within a linux environment, including the execution of the rules in this package. With the Docker CLI exposed to WSL, the linux environment can also push the built images to your local registry and otherwise interact normally with the Docker Desktop installation.

Some things to note:

The only thing I haven't figured out yet is how best to expose the directory within WSL to other host machine apps. I prefer to use Sublime Merge for repo management (I'll probably just install it directly in the WSL instance in the end), but for now VSCode's built-in repo management utilities are good enough.