-
Notifications
You must be signed in to change notification settings - Fork 691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue compiling go_image on macOS #690
Comments
hi @obeattie,
|
Lightning fast! ⚡️ That's interesting that it shouldn't have worked before – I might be mistaken but I am 90% sure. In any case I don't think it matters too much; it sounds like plumbing through If I can be of any help, I'm very happy to try and help make that a reality. |
I'll sync up with @katre to ask if this is possible /easy now or if we should wait for some of his platform features to roll out and ping back this issue. |
A couple of questions: is the problem that you cannot build the go_binary without setting Adding |
With the |
I have worded my question poorly.
|
I am trying to determine if the problem is with the go rules, or the docker rules. |
Yep, both of these commands build successfully. |
In that case, I suspect the problem is simply that the @nlopezgi: You should probably talk to @jayconrod about the right way to handle this. Most likely, you want to do a configuration transition to set the platforms flag based on goarch and goos. Alternately, you should remove the attributes, and instruct users of passing appropriate values for the Using |
I think There are plans to reimplement For now, the |
I hacked around this with bazel-contrib/rules_go@master...rickypai:rpai/fix_grpc_darwin
|
update: our plan is to set up some examples that run on CI to exercise this use case and provide better advice / docs as to how to do this properly with the current features. From there we can move on to see effort / viability of restricting this from running with the incorrect platform. |
Hi @obeattie, |
@alex1545 👋 I've put together a minimal proof of concept in obeattie/poc-bazel-690. If you clone this repository and try to build the
|
@obeattie, I was now able to reproduce your failure. Thank you for the provided sample. To answer @katre very first question
It's both. The problem is that I cannot build the go_binary without setting --platforms to a linux platform and hence the build of the go_image target also fails with the same error.
@jayconrod, I also tried it with Bazel 0.23.2 and I still get the same failure. So it seems that As @jayconrod mentioned, since I'll update our docs to advice that |
My issue is similar. go_binary(
name = "sample_binary",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
go_image(
name = "sample_image",
srcs = ["main.go"],
importpath = "com.something.sample",
) I'm able to run But running I'm able to generate the image via a Dockerfile, but I'm eager to do it the Bazel way. My setup is as below
Can someone help? |
Hi @Munukutla, can you please provide a minimal example for which I can reproduce your issue? From your comment I can only assume that you use the same |
We are experiencing the same problem. Anything with dependency on grpc built with goarch = "amd64" goos = "linux" on mac is failing with similar error. |
I don't think its possible at all to do what you are trying to do: build with a single command both local (for mac) and linux (for container) targets. You need to do this in two commands. @katre for comments on status of doing a multi platform build that produces outputs for 2 platforms in a single bazel command. |
@nlopezgi It is certainly possible and we are currently using this in production. |
Right now we don't support multi-platform builds in the same Bazel command. This is on our roadmap (https://bazel.build/roadmaps/configuration.html) but not being worked on currently. |
@katre I would like to correct myself. It is working at least for go and java - two languages that cross compile natively (mostly) without special support in a build system, so the stated goal is every day reality for us and it is beautiful. You are talking about solving in in general for all languages, which is completely different story. It would be fantastic to see it working. |
thanks for clarifying @katre, From the perspective of the original ask, iiuc, until multi platform builds in Bazel are possible, we will not be able to support building (from a mac) a |
@nlopezgi but what about host tools? We have a lot of DSL tools in our project. In our case when we are building linux image we also need to build some OSX binaries as stage of the build final binary. How it supposed to work? |
@excavador |
Now that transitions have been out for a bit, it seems like one of the linked PRs: https://github.com/Yolk-HQ/rules_docker/pull/1 might be worth revisiting. With diff --git a/lang/image.bzl b/lang/image.bzl
index c2db8f4..3d53bd1 100644
--- a/lang/image.bzl
+++ b/lang/image.bzl
@@ -34,8 +34,8 @@ def _binary_name(ctx):
# /app/foo/bar/baz/blah
return "/".join([
ctx.attr.directory,
- ctx.attr.binary.label.package,
- ctx.attr.binary.label.name,
+ ctx.attr.binary[0].label.package,
+ ctx.attr.binary[0].label.name,
])
def _runfiles_dir(ctx):
@@ -148,7 +148,7 @@ def _app_layer_impl(ctx, runfiles = None, emptyfiles = None):
parent_parts = _get_layers(ctx, ctx.attr.name, ctx.attr.base)
filepath = _final_file_path if ctx.attr.binary else layer_file_path
emptyfilepath = _final_emptyfile_path if ctx.attr.binary else _layer_emptyfile_path
- dep = ctx.attr.dep or ctx.attr.binary
+ dep = (ctx.attr.dep or ctx.attr.binary)[0]
top_layer = ctx.attr.binary and not ctx.attr.dep
if ctx.attr.create_empty_workspace_dir:
@@ -239,6 +239,20 @@ def _app_layer_impl(ctx, runfiles = None, emptyfiles = None):
null_cmd = args == [],
)
+def _container_transition_impl(settings, attr):
+ _ignore = (settings, attr)
+ return {
+ "//command_line_option:platforms": "@io_bazel_rules_go//go/toolchain:linux_amd64",
+ }
+
+container_transition = transition(
+ implementation = _container_transition_impl,
+ inputs = [],
+ outputs = [
+ "//command_line_option:platforms",
+ ],
+)
+
image = struct(
attrs = dicts.add(_container.image.attrs, {
# The base image on which to overlay the dependency layers.
@@ -250,7 +264,7 @@ image = struct(
# the runfiles dir.
"binary": attr.label(
executable = True,
- cfg = "target",
+ cfg = container_transition,
),
# Set this to true to create an empty workspace directory under the
# app directory specified as the 'directory' attribute.
@@ -263,11 +277,16 @@ image = struct(
# The dependency whose runfiles we're appending.
# If not specified, then the layer will be treated as the top layer,
# and all remaining deps of "binary" will be added under runfiles.
- "dep": attr.label(),
+ "dep": attr.label(
+ cfg = container_transition,
+ ),
"directory": attr.string(default = "/app"),
"entrypoint": attr.string_list(default = []),
"legacy_run_behavior": attr.bool(default = False),
"workdir": attr.string(default = ""),
+ "_allowlist_function_transition": attr.label(
+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
+ ),
}),
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"], I've tried this out on a Would a PR to make this change be the way to move forward, or is a transition not the thing to do here? |
I'm excited for transitions to be part of rules_docker. It will simplify the creation of docker images and continue to strengthen the community. I've been using a patch like this locally along with an experimental cross compiler toolchain to make it much easier to do hermetic builds for docker images. (https://github.com/ajbouh/bazel-zig-cc for the curious.) I'm also using this approach to get proper cgo support for my |
Hey all. I don't currently have access to a mac to test these fixes so I wouldn't really feel comfortable putting together a PR for this. If someone sends one in I'd review it. We do have MacOS CI runners that could test this so if the suggestion in this comment works we should go for it. |
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
Transition to the target platform associated with the `architecture` and `operating_system` attributes on container image rules. This change allows for container image rules to build the correct binary for the target platform, regardless of the host platform. Container image rules would require the use of the `target_compatible_with` attribute to prevent mismatching host and target platforms building dependencies incorrectly. Additionally, hosts which did not match the target platform had to explicitly specify the target platform with the `--platforms` command-line option. This change fixes the aforementioned issues and bazelbuild#690. Massive thank you to @joneshf for the initial source. It has been adapted to automatically select the target platform associated with the container image, as opposed to always using `@io_bazel_rules_go//go/toolchain:linux_amd64`.
I am trying to compile a
go_image
target on macOS, and this is failing with an error about missing imports:The BUILD file that I am building this from specifies
goarch
andgoos
, which I think is as you recommend it:However, if I specify the platform explicitly like so, it does work:
$ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //service.api.ping:image
It's a little difficult to unpick precisely what's happening, but the failing compilation rule looks to be defined in
rules_go
, and it does look like the dependency on@org_golang_x_sys//unix:go_default_library
is properly specified for the Linux platform:https://github.com/bazelbuild/rules_go/blob/f5cfc31d4e8de28bf19d0fb1da2ab8f4be0d2cde/third_party/org_golang_google_grpc-gazelle.patch#L1532-L1535
I am pretty sure that this worked without needing to explicitly specify
--platforms
in a previous release, so I wonder:linux_amd64
platform? The "build platforms roadmap" seems to indicate that this is possible ("execution platform filtering") but I can't work out how to actually make use of this.Thanks 🙏
The text was updated successfully, but these errors were encountered: