bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.77k stars 4k forks source link

Fail to build bazel android example: output jar appears to be empty #8051

Closed adamhowardprice closed 5 years ago

adamhowardprice commented 5 years ago

Description of the problem / feature request:

Android bazel builds are failing for me with 0.24.1. Seen on my project and reproduced with the Android example Hello World project in this repo.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

I run: $ bazel build --verbose_failures //examples/android/java/bazel:hello_world

and the output is

INFO: Analysed target //examples/android/java/bazel:hello_world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /Users/adamprice/Code/bazel/examples/android/java/bazel/BUILD:12:1: Generating unsigned apk failed (Exit 1) singlejar failed: error executing command
  (cd /private/var/tmp/_bazel_adamprice/20945e1b7ee3437c8e51e114c3bfb210/sandbox/darwin-sandbox/1/execroot/io_bazel && \
  exec env - \
  external/bazel_tools/tools/jdk/singlejar/singlejar --exclude_build_data --dont_change_compression --normalize --sources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/compressed_hello_world_unsigned.apk --output bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/hello_world_unsigned.apk --sources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar --resources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/nativedeps_filename/hello_world/nativedeps:nativedeps --sources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/hello_world.ap_ --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/appcompat-v7-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/animated-vector-drawable-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-vector-drawable-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-compat-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-v4-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-core-ui-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-core-utils-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-fragment-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-media-compat-25.0.0/native_libs.zip)
Execution platform: @bazel_tools//platforms:host_platform

Use --sandbox_debug to see verbose messages from the sandbox: singlejar failed: error executing command
  (cd /private/var/tmp/_bazel_adamprice/20945e1b7ee3437c8e51e114c3bfb210/sandbox/darwin-sandbox/1/execroot/io_bazel && \
  exec env - \
  external/bazel_tools/tools/jdk/singlejar/singlejar --exclude_build_data --dont_change_compression --normalize --sources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/compressed_hello_world_unsigned.apk --output bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/hello_world_unsigned.apk --sources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar --resources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/nativedeps_filename/hello_world/nativedeps:nativedeps --sources bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/hello_world.ap_ --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/appcompat-v7-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/animated-vector-drawable-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-vector-drawable-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-compat-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-v4-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-core-ui-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-core-utils-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-fragment-25.0.0/native_libs.zip --sources bazel-out/android-armeabi-v7a-fastbuild/bin/external/androidsdk/com.android.support/_aar/support-media-compat-25.0.0/native_libs.zip)
Execution platform: @bazel_tools//platforms:host_platform

Use --sandbox_debug to see verbose messages from the sandbox
singlejar: src/tools/singlejar/input_jar.cc:32: bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar is only 0x0 bytes long, should be at least 0x16 bytes long
Target //examples/android/java/bazel:hello_world failed to build
INFO: Elapsed time: 0.426s, Critical Path: 0.10s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

What operating system are you running Bazel on?

MacOS Mojave 10.14.4 Beta

What's the output of bazel info release?

release 0.24.1

What's the output of git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?

https://github.com/bazelbuild/bazel.git e91ac8d44fb994dfbd0437c8b98f5dded78a2f2e e91ac8d44fb994dfbd0437c8b98f5dded78a2f2e

Any other information, logs, or outputs that you want to share?

In another Android project, when I attempted to build, I got this error:

singlejar: src/tools/singlejar/input_jar.cc:32: bazel-out/android-armeabi-v7a-fastbuild/bin/bazel/deps/_aar/com_android_support__multidex__1_0_2/native_libs.zip is only 0x0 bytes long, should be at least 0x16 bytes long
jin commented 5 years ago

I am unable to reproduce this on macOS with Bazel 0.24.1, although I can see that bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar is an empty jar, which is probably causing singlejar to choke here: https://source.bazel.build/bazel/+/master:src/tools/singlejar/input_jar.cc;l=31?q=f:singlejar%20%22bytes%20long%22

$ zipinfo bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar
Archive:  bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar
Zip file size: 22 bytes, number of entries: 0
Empty zipfile.
jin commented 5 years ago

@adamhowardprice what does zipinfo return on the file on your side? My empty jar is 22bytes long, which is the expected size of an empty jar.

adamhowardprice commented 5 years ago
zipinfo ../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar
Archive:  ../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar
[../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar]
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
note:  ../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar may be a plain executable, not an archive
zipinfo:  cannot find zipfile directory in one of ../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar or
          ../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar.zip, and cannot find ../../bazel-out/darwin-fastbuild/bin/examples/android/java/bazel/_dx/hello_world/extracted_hello_world_deploy.jar.ZIP, period.
jin commented 5 years ago

Just to confirm, is it a 0-byte file?

adamhowardprice commented 5 years ago

Yes

jin commented 5 years ago

Do you happen to have any kind of system-wide programs that can affect file content? Like an anti-virus or backup software?

adamhowardprice commented 5 years ago

I do have some anti-virus software that is running against my will :)

adamhowardprice commented 5 years ago

Interesting theory -- how can I figure out if that's interfering with this?

adamhowardprice commented 5 years ago

I reached out to my IT team

jin commented 5 years ago

AFAIK, that's typically the cause of involuntary 0-byte files. Do you have a way to disable it? Does the problem also occur on other Macs running the same base macOS image?

adamhowardprice commented 5 years ago

Deleted the antivirus program and this is still failing

jin commented 5 years ago

Does this occur on other Macs?

jin commented 5 years ago

Unfortunately, I'm not able to reproduce this on Mojave 10.14. Curious that this only happens to jar files. What's your local JDK version (javac -version)?

jin commented 5 years ago

Figured it out offline with @adamhowardprice: this will occur if you are using Python versions earlier than 2.7.1.

https://stackoverflow.com/questions/25195495/how-to-create-an-empty-zip-file

For Python tools that create zipfiles, if running on Python versions earlier than 2.7.1, the zipfile library will not create the base zip structure in the empty zip file, leading to a 0 byte file.

On and after 2.7.1, the empty zip file has a valid zip structure.

The workaround is to upgrade the system Python to 2.7.1 or later. But note that Python 2.7 is EOLing soon, so it's unlikely we'll backport support for this.