skip to Main Content

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("@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:

# 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='-Wno-sign-compare' //...:all

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

the error:

$ 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)
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: "execvp(external/remotejdk11_linux/bin/javac, ...)": 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 '/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

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.

2

Answers


  1. Chosen as BEST ANSWER

    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 '@local_jdk//': Infinite symlink expansion
    ERROR: Analysis of target '//test:test_message_java_proto' 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. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search