使用 Bazel 的 `java_proto_library` 在 Alpine 上

huangapple go评论89阅读模式
英文:

Using Bazel java_proto_library on Alpine

问题

使用bazel alpine package时,在尝试使用java_proto_library()时会失败(而在archlinux、centos、fedora、debian、opensuse、ubuntu上可以工作)。

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

# Protobuf
git_repository(
    name = "com_google_protobuf",
    commit = "fde7cf7",  # release v3.13.0
    remote = "https://github.com/protocolbuffers/protobuf.git",
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
# Load common dependencies.
protobuf_deps()

test/BUILD:

# not needed
#load("@rules_cc//cc:defs.bzl", "cc_proto_library")
#load("@rules_java//java:defs.bzl", "java_proto_library")

package(
    default_visibility = ["//visibility:public"],
)

proto_library(
    name = "test_message_proto",
    srcs = ["test_message.proto"],
    deps = ["@com_google_protobuf//:duration_proto"],
)

cc_proto_library(
    name = "test_message_cc_proto",
    deps = [":test_message_proto"],
)

java_proto_library(
    name = "test_message_java_proto",
    deps = [":test_message_proto"],
)

Dockerfile:

# 创建一个包含所有工具的虚拟环境
# 引用:https://hub.docker.com/_/alpine
FROM alpine:edge AS env
# 安装系统构建依赖项
ENV PATH=/usr/local/bin:$PATH
RUN apk add --no-cache git build-base linux-headers zlib-dev
RUN apk add --no-cache -X http://dl-cdn.alpinelinux.org/alpine/edge/testing bazel

# 安装 OpenJDK11
#RUN apk add --no-cache openjdk11
# 移除无限循环,因为jre指向当前目录
# 否则bazel会报错并停止...
#RUN rm /usr/lib/jvm/default-jvm/jre

ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

FROM env AS devel
WORKDIR /home/lib
COPY . .

FROM devel as build
RUN bazel build --curses=no --copt='-Wno-sign-compare' //...:all

FROM build as test
RUN bazel test -c opt --curses=no --copt='-Wno-sign-compare' //...:all

错误信息:

$ docker build --target=build --tag test/bazel:alpine_build -f alpine/Dockerfile .
...
Step 12/12 : RUN bazel build --curses=no --copt='-Wno-sign-compare' //...:all
 ---> Running in c932e97d87d8
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
Loading: 
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
DEBUG: Rule 'com_google_protobuf' indicated that a canonical reproducible form can be obtained by modifying arguments commit = "fde7cf7358ec7cd69e8db9be4f1fa6a5c431386a", shallow_since = "1597443653 -0700"
DEBUG: Call stack for the definition of repository 'com_google_protobuf' which is a git_repository (rule definition at /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/build_defs/repo/git.bzl:195:18):
 - <builtin>
 - /home/lib/WORKSPACE:4:1
Analyzing: 3 targets (2 packages loaded, 0 targets configured)
Analyzing: 3 targets (5 packages loaded, 5 targets configured)
...
INFO: Analyzed 3 targets (22 packages loaded, 1014 targets configured).
INFO: Found 3 targets...
[0 / 192] [Prepa] BazelWorkspaceStatusAction stable-status.txt
ERROR: /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/jdk/BUILD:314:1: Action external/bazel_tools/tools/jdk/platformclasspath_classes/DumpPlatformClassPath.class failed (Exit 1) javac failed: error executing command external/remotejdk11_linux/bin/javac -source 8 -target 8 -Xlint:-options -cp external/remotejdk11_linux/lib/tools.jar -d ... (remaining 2 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
src/main/tools/process-wrapper-legacy.cc:58: "execvp(external/remotejdk11_linux/bin/javac, ...)": 文件或目录不存在
INFO: Elapsed time: 47.427s, Critical Path: 1.42s
INFO: 3 processes: 3 processwrapper-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
The command '/bin/sh -c bazel build --curses=no --copt='-Wno-sign-compare' //...:all' returned a non-zero code: 1
make: *** [Makefile:116: alpine_build] Error 1

备注:Alpine软件包依赖于openJDK-8,但即使我尝试安装openJDK-11(因为日志错误包含external/remotejdk11_linux/bin/javac),仍然无法正常工作。

英文:

Using the bazel alpine package, when trying to use the java_proto_library(), it will fails (while it works on archlinux, centos, fedora, debian, opensuse, ubuntu)

WORKSPACE:

load(&quot;@bazel_tools//tools/build_defs/repo:git.bzl&quot;, &quot;git_repository&quot;)

# Protobuf
git_repository(
    name = &quot;com_google_protobuf&quot;,
    commit = &quot;fde7cf7&quot;,  # release v3.13.0
    remote = &quot;https://github.com/protocolbuffers/protobuf.git&quot;,
)

load(&quot;@com_google_protobuf//:protobuf_deps.bzl&quot;, &quot;protobuf_deps&quot;)
# Load common dependencies.
protobuf_deps()

test/BUILD:

# not needed
#load(&quot;@rules_cc//cc:defs.bzl&quot;, &quot;cc_proto_library&quot;)
#load(&quot;@rules_java//java:defs.bzl&quot;, &quot;java_proto_library&quot;)

package(
    default_visibility = [&quot;//visibility:public&quot;],
)

proto_library(
    name = &quot;test_message_proto&quot;,
    srcs = [&quot;test_message.proto&quot;],
    deps = [&quot;@com_google_protobuf//:duration_proto&quot;],
)

cc_proto_library(
    name = &quot;test_message_cc_proto&quot;,
    deps = [&quot;:test_message_proto&quot;],
)

java_proto_library(
    name = &quot;test_message_java_proto&quot;,
    deps = [&quot;:test_message_proto&quot;],
)

Dockerfile:

# Create a virtual environment with all tools installed
# ref: https://hub.docker.com/_/alpine
FROM alpine:edge AS env
# Install system build dependencies
ENV PATH=/usr/local/bin:$PATH
RUN apk add --no-cache git build-base linux-headers zlib-dev
RUN apk add --no-cache -X http://dl-cdn.alpinelinux.org/alpine/edge/testing bazel

# Install OpenJDK11
#RUN apk add --no-cache openjdk11
# Remove infinite loop since jre point to the current directory
# otherwise bazel issue an error and stop...
#RUN rm /usr/lib/jvm/default-jvm/jre

ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

FROM env AS devel
WORKDIR /home/lib
COPY . .

FROM devel as build
RUN bazel build --curses=no --copt=&#39;-Wno-sign-compare&#39; //...:all

FROM build as test
RUN bazel test -c opt --curses=no --copt=&#39;-Wno-sign-compare&#39; //...:all

the error:

$ docker build --target=build --tag test/bazel:alpine_build -f alpine/Dockerfile .
...
Step 12/12 : RUN bazel build --curses=no --copt=&#39;-Wno-sign-compare&#39; //...:all
 ---&gt; Running in c932e97d87d8
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
Loading: 
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
DEBUG: Rule &#39;com_google_protobuf&#39; indicated that a canonical reproducible form can be obtained by modifying arguments commit = &quot;fde7cf7358ec7cd69e8db9be4f1fa6a5c431386a&quot;, shallow_since = &quot;1597443653 -0700&quot;
DEBUG: Call stack for the definition of repository &#39;com_google_protobuf&#39; which is a git_repository (rule definition at /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/build_defs/repo/git.bzl:195:18):
 - &lt;builtin&gt;
 - /home/lib/WORKSPACE:4:1
Analyzing: 3 targets (2 packages loaded, 0 targets configured)
Analyzing: 3 targets (5 packages loaded, 5 targets configured)
Analyzing: 3 targets (5 packages loaded, 5 targets configured)
Analyzing: 3 targets (15 packages loaded, 133 targets configured)
Analyzing: 3 targets (20 packages loaded, 866 targets configured)
Analyzing: 3 targets (20 packages loaded, 866 targets configured)
Analyzing: 3 targets (21 packages loaded, 895 targets configured)
Analyzing: 3 targets (21 packages loaded, 895 targets configured)
Analyzing: 3 targets (21 packages loaded, 895 targets configured)
Analyzing: 3 targets (21 packages loaded, 895 targets configured)
INFO: Analyzed 3 targets (22 packages loaded, 1014 targets configured).
INFO: Found 3 targets...
[0 / 192] [Prepa] BazelWorkspaceStatusAction stable-status.txt
ERROR: /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/jdk/BUILD:314:1: Action external/bazel_tools/tools/jdk/platformclasspath_classes/DumpPlatformClassPath.class failed (Exit 1) javac failed: error executing command external/remotejdk11_linux/bin/javac -source 8 -target 8 -Xlint:-options -cp external/remotejdk11_linux/lib/tools.jar -d ... (remaining 2 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
src/main/tools/process-wrapper-legacy.cc:58: &quot;execvp(external/remotejdk11_linux/bin/javac, ...)&quot;: No such file or directory
INFO: Elapsed time: 47.427s, Critical Path: 1.42s
INFO: 3 processes: 3 processwrapper-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
The command &#39;/bin/sh -c bazel build --curses=no --copt=&#39;-Wno-sign-compare&#39; //...:all&#39; returned a non-zero code: 1
make: *** [Makefile:116: alpine_build] Error 1

Note: Alpine package depends on openJDK-8 but even if I try to install openJDK-11 (ed since log error contains external/remotejdk11_linux/bin/javac) it still won't work.

答案1

得分: 3

虽然 Alpine 软件包依赖于 openJDK8,但你需要安装 openJDK11 才能使其正常工作。

...
# 安装 OpenJDK11
# 注意:default-jvm 现在会指向 java-11-openjdk
RUN apk add --no-cache openjdk11
# 移除无限循环符号链接,因为 jre 指向当前目录
# 否则 bazel 会报错并停止...
RUN rm /usr/lib/jvm/default-jvm/jre

ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

...
RUN bazel build --host_javabase=@local_jdk//:jdk //...:all

常见问题解答:

  • 如果你看到以下内容:
ERROR: 检测到无限符号链接扩展
[符号链接链的开始]
/usr/lib/jvm/java-11-openjdk/jre
/usr/lib/jvm/java-11-openjdk
[符号链接链的结束]
ERROR: /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/jdk/BUILD:66:1: @bazel_tools//tools/jdk:legacy_current_java_runtime 依赖于无法提取的存储库 @local_jdk//:jdk 中的 @local_jdk// 包。没有此包 &#39;@local_jdk//&#39;:无限符号链接扩展
ERROR: 分析目标 &#39;//test:test_message_java_proto&#39; 失败;构建中止:分析失败

这表示你忘记了在安装 openjdk11 时抑制生成的符号链接...

RUN rm /usr/lib/jvm/default-jvm/jre
英文:

While alpine package depend on openJDK8, you'll have to install openJDK11 to make it works.

...
# Install OpenJDK11
# note: default-jvm will now point to java-11-openjdk
RUN apk add --no-cache openjdk11
# Remove infinite loop since jre point to the current directory
# otherwise bazel issue an error and stop...
RUN rm /usr/lib/jvm/default-jvm/jre

ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

...
RUN bazel build --host_javabase=@local_jdk//:jdk //...:all

FAQ:

  • If you see this:
ERROR: infinite symlink expansion detected
[start of symlink chain]
/usr/lib/jvm/java-11-openjdk/jre
/usr/lib/jvm/java-11-openjdk
[end of symlink chain]
ERROR: /root/.cache/bazel/_bazel_root/86fee77ec27da0053940f3f327a6fd59/external/bazel_tools/tools/jdk/BUILD:66:1: @bazel_tools//tools/jdk:legacy_current_java_runtime depends on @local_jdk//:jdk in repository @local_jdk which failed to fetch. no such package &#39;@local_jdk//&#39;: Infinite symlink expansion
ERROR: Analysis of target &#39;//test:test_message_java_proto&#39; failed; build aborted: Analysis failed

It means you forget to suppress the symlink generated when installing openjdk11...

RUN rm /usr/lib/jvm/default-jvm/jre

答案2

得分: 1

Bazel附带的JDK是针对glibc而不是musl编译的,因此在Alpine上无法正常工作。必须使用--host_javabase=@local_jdk//:jdk显式指定系统的JDK。

英文:

The JDK shipped with Bazel is compiled against glibc not musl, and consequently does not work on Alpine. The system JDk must be explicitly specified with --host_javabase=@local_jdk//:jdk.

huangapple
  • 本文由 发表于 2020年8月29日 03:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63640050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定