Build fails with "protoc_wrapper failed"

I’m trying to compile anki from source, but I keep getting the following error message:

ERROR: /home/b3nj4m1n/Documents/Github/anki/pylib/anki/BUILD.bazel:111:9: Action pylib/anki/backend_pb2.py failed: (Exit 1): protoc_wrapper failed: error executing command bazel-out/k8-opt-exec-2B5CBBC6/bin/pylib/tools/protoc_wrapper external/protoc_bin_linux_x86_64/bin/protoc bazel-out/k8-opt-exec-2B5CBBC6/bin/pylib/tools/protoc-gen-mypy ... (remaining 20 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
Traceback (most recent call last):
  File "/home/b3nj4m1n/.cache/bazel/_bazel_b3nj4m1n/ee6625df9ac32592700069683600f2ec/sandbox/linux-sandbox/288/execroot/ankidesktop/bazel-out/k8-opt-exec-2B5CBBC6/bin/pylib/tools/protoc_wrapper.runfiles/ankidesktop/pylib/tools/protoc_wrapper.py", line 23, in <module>
    subprocess.run(
  File "/home/b3nj4m1n/.cache/bazel/_bazel_b3nj4m1n/ee6625df9ac32592700069683600f2ec/external/python/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['external/protoc_bin_linux_x86_64/bin/protoc', '--plugin=protoc-gen-mypy=bazel-out/k8-opt-exec-2B5CBBC6/bin/pylib/tools/protoc-gen-mypy', '--python_out=bazel-out/k8-fastbuild/bin/pylib', '--mypy_out=bazel-out/k8-fastbuild/bin/pylib', '-Iproto/', '-Iexternal/ankidesktop/proto/', 'proto/anki/backend.proto', 'proto/anki/card_rendering.proto', 'proto/anki/cards.proto', 'proto/anki/collection.proto', 'proto/anki/config.proto', 'proto/anki/deckconfig.proto', 'proto/anki/decks.proto', 'proto/anki/generic.proto', 'proto/anki/i18n.proto', 'proto/anki/import_export.proto', 'proto/anki/links.proto', 'proto/anki/media.proto', 'proto/anki/notes.proto', 'proto/anki/notetypes.proto', 'proto/anki/scheduler.proto', 'proto/anki/search.proto', 'proto/anki/stats.proto', 'proto/anki/sync.proto', 'proto/anki/tags.proto']' returned non-zero exit status 1.
Target //qt:runanki failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/b3nj4m1n/Documents/Github/anki/qt/BUILD.bazel:123:10 Middleman _middlemen/qt_Srunanki-runfiles failed: (Exit 1): protoc_wrapper failed: error executing command bazel-out/k8-opt-exec-2B5CBBC6/bin/pylib/tools/protoc_wrapper external/protoc_bin_linux_x86_64/bin/protoc bazel-out/k8-opt-exec-2B5CBBC6/bin/pylib/tools/protoc-gen-mypy ... (remaining 20 arguments skipped)

Unfortunately, I don’t know anything about bazel, so I’m not sure how to debug this.

I’ve tried deleting bazels cache, and I’ve gone over all the dependencies several times.

I also created a docker file to set up a fresh arch environment, install all dependencies and built anki, that is working fine, so I suspect this is due to something like a preexisting package or something like that.

Here’s the dockerfile:

FROM archlinux:base-devel-20220821.0.76637

RUN sudo pacman -S --needed --noconfirm base-devel git wget unzip

RUN useradd -m anki
RUN echo "anki ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
USER anki
ENTRYPOINT ["sleep"]
CMD ["1000"]

RUN cd / && sudo mkdir -p "/anki" && sudo chown "anki" -R "/anki" && git clone "https://github.com/ankitects/anki"
RUN cd /anki && git checkout 2.1.54

RUN sudo pacman -S --needed --noconfirm \
    autoconf \
    curl \
    findutils \
    gcc \
    git \
    grep \
    make \
    rsync \
    zstd \
    pkgconf \
    portaudio \
    python

RUN sudo curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.7.4/bazelisk-linux-amd64 \
    -o /usr/local/bin/bazel \
    && sudo chmod +x /usr/local/bin/bazel

RUN sudo pacman -S --needed --noconfirm libglvnd
RUN sudo pacman -S --needed --noconfirm fontconfig
RUN sudo pacman -S --needed --noconfirm gstreamer gst-plugins-good gst-plugins-base-libs gst-plugins-base
RUN sudo pacman -S --needed --noconfirm nss
RUN sudo pacman -S --needed --noconfirm libpulse
RUN sudo pacman -S --needed --noconfirm openssl
RUN sudo pacman -S --needed --noconfirm libxcomposite
RUN sudo pacman -S --needed --noconfirm libxcursor
RUN sudo pacman -S --needed --noconfirm libxi
RUN sudo pacman -S --needed --noconfirm libxkbcommon-x11 libxkbcommon libxkbfile
RUN sudo pacman -S --needed --noconfirm libxrandr
RUN sudo pacman -S --needed --noconfirm libxrender
RUN sudo pacman -S --needed --noconfirm libxtst
RUN sudo pacman -S --needed --noconfirm dbus

RUN cd /anki && ./run

I’ve done every command from this dockerfile in order on my main machine, but I still get the same error.

You can remove the DEVNULL line in protoc_wrapper.py to possibly get some more info.

I tried that, the error seems to be that it can’t find the google.protobuf module. I found a stackoverflow post (that I can’t link to) and tried a few of the most upvoted answers to resolve this issue, however the build is still failing with the same error message.

When I try to import the package in my normal python interpreter, there doesn’t seem to be an issue:

In [3]: from google import protobuf

In [4]: protobuf
Out[4]: <module 'google.protobuf' from '/home/b3nj4m1n/.local/lib/python3.10/site-packages/google/protobuf/__init__.py'>

Is there a reason why maybe bazel can’t find the module that I’m overlooking? I tried installing the module using pip both for my user & globally, and I also tried in a virtual environment.

What’s the full error you’re getting?

Here’s the full log:

Starting local Bazel server and connecting to it...
WARNING: ignoring _JAVA_OPTIONS in environment.
INFO: Analyzed target //qt:runanki (720 packages loaded, 31083 targets configured).
INFO: Found 1 target...
ERROR: /home/b3nj4m1n/Documents/Github/anki/pylib/anki/BUILD.bazel:111:9: Action pylib/anki/backend_pb2.py [for host] failed: (Exit 1): protoc_wrapper failed: error executing command bazel-out/host/bin/pylib/tools/protoc_wrapper external/protoc_bin_linux_x86_64/bin/protoc bazel-out/host/bin/pylib/tools/protoc-gen-mypy bazel-out/host/bin/pylib/anki proto/anki/backend.proto ... (remaining 18 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
external/ankidesktop/proto/: warning: directory does not exist.
Traceback (most recent call last):
  File "/home/b3nj4m1n/.cache/bazel/_bazel_b3nj4m1n/ee6625df9ac32592700069683600f2ec/sandbox/linux-sandbox/3/execroot/ankidesktop/bazel-out/host/bin/pylib/tools/protoc-gen-mypy.runfiles/ankidesktop/pylib/tools/protoc-gen-mypy.py", line 5, in <module>
    from mypy_protobuf.main import main
  File "/home/b3nj4m1n/.cache/bazel/_bazel_b3nj4m1n/ee6625df9ac32592700069683600f2ec/sandbox/linux-sandbox/3/execroot/ankidesktop/bazel-out/host/bin/pylib/tools/protoc-gen-mypy.runfiles/py_deps_mypy_protobuf/mypy_protobuf/main.py", line 22, in <module>
    import google.protobuf.descriptor_pb2 as d
ModuleNotFoundError: No module named 'google.protobuf'
----------------
Note: The failure of target //pylib/tools:protoc-gen-mypy (with exit code 1) may have been caused by the fact that it is running under Python 3 instead of Python 2. Examine the error to determine if that appears to be the problem. Since this target is built in the host configuration, the only way to change its version is to set --host_force_python=PY2, which affects the entire build.

If this error started occurring in Bazel 0.27 and later, it may be because the Python toolchain now enforces that targets analyzed as PY2 and PY3 run under a Python 2 and Python 3 interpreter, respectively. See https://github.com/bazelbuild/bazel/issues/7899 for more information.
----------------
--mypy_out: protoc-gen-mypy: Plugin failed with status code 1.
Traceback (most recent call last):
  File "/home/b3nj4m1n/.cache/bazel/_bazel_b3nj4m1n/ee6625df9ac32592700069683600f2ec/sandbox/linux-sandbox/3/execroot/ankidesktop/bazel-out/host/bin/pylib/tools/protoc_wrapper.runfiles/ankidesktop/pylib/tools/protoc_wrapper.py", line 23, in <module>
    subprocess.run(
  File "/home/b3nj4m1n/.cache/bazel/_bazel_b3nj4m1n/ee6625df9ac32592700069683600f2ec/external/python/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['external/protoc_bin_linux_x86_64/bin/protoc', '--plugin=protoc-gen-mypy=bazel-out/host/bin/pylib/tools/protoc-gen-mypy', '--python_out=bazel-out/host/bin/pylib', '--mypy_out=bazel-out/host/bin/pylib', '-Iproto/', '-Iexternal/ankidesktop/proto/', 'proto/anki/backend.proto', 'proto/anki/card_rendering.proto', 'proto/anki/cards.proto', 'proto/anki/collection.proto', 'proto/anki/config.proto', 'proto/anki/deckconfig.proto', 'proto/anki/decks.proto', 'proto/anki/generic.proto', 'proto/anki/i18n.proto', 'proto/anki/import_export.proto', 'proto/anki/links.proto', 'proto/anki/media.proto', 'proto/anki/notes.proto', 'proto/anki/notetypes.proto', 'proto/anki/scheduler.proto', 'proto/anki/search.proto', 'proto/anki/stats.proto', 'proto/anki/sync.proto', 'proto/anki/tags.proto']' returned non-zero exit status 1.
----------------
Note: The failure of target //pylib/tools:protoc_wrapper (with exit code 1) may have been caused by the fact that it is running under Python 3 instead of Python 2. Examine the error to determine if that appears to be the problem. Since this target is built in the host configuration, the only way to change its version is to set --host_force_python=PY2, which affects the entire build.

If this error started occurring in Bazel 0.27 and later, it may be because the Python toolchain now enforces that targets analyzed as PY2 and PY3 run under a Python 2 and Python 3 interpreter, respectively. See https://github.com/bazelbuild/bazel/issues/7899 for more information.
----------------
Target //qt:runanki failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/b3nj4m1n/Documents/Github/anki/pylib/anki/_backend/BUILD.bazel:7:10 Middleman _middlemen/pylib_Sanki_S_Ubackend_Sgenbackend-runfiles failed: (Exit 1): protoc_wrapper failed: error executing command bazel-out/host/bin/pylib/tools/protoc_wrapper external/protoc_bin_linux_x86_64/bin/protoc bazel-out/host/bin/pylib/tools/protoc-gen-mypy bazel-out/host/bin/pylib/anki proto/anki/backend.proto ... (remaining 18 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
INFO: Elapsed time: 129.957s, Critical Path: 3.97s
INFO: 58 processes: 56 internal, 2 linux-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully

Ok, unfortunately that didn’t reveal anything. You could try adding a requirement(“protobuf”) to the BUILD file in pylib/tools, or checking if something in .local is causing conflicts.