• David Z. Chen's avatar
    Bazel build: Keep generated sources and Python runtime in the same directory. · 02cd45ca
    David Z. Chen authored
    Users often encounter a Python import error when trying to build Python
    protos if protobuf is installed locally on the machine. In this case,
    Python ends up looking in the wrong directory when importing files (see
    bazelbuild/bazel#1209 and tensorflow/tensorflow#2021). It seems that the
    problem is caused by Python getting confused when there are Python
    source files that are meant to be part of the same package but are
    in separate directories.
    
    Prior to #1233, the Bazel build setup would copy the Python
    runtime sources and all generated sources for the builtin protos into
    the root directory (assuming that the protobuf tree is vendored in a
    google/protobuf directory).
    
    With #1233, the two sets of sources are kept in their respective
    directories but both `src/` and `python/` are added to the `PYTHONPATH`
    using the new `imports` attribute of the Bazel Python rules. However,
    both the runtime sources and the generated sources are under the same
    package: `google.protobuf`, causing Python to become confused when
    trying to import modules that are in the other directory.
    
    This patch adds a workaround to the Bazel build to add a modified
    version of the original `internal_copied_filegroup` macro to copy the
    `.proto` files under `src/` to `python/` before building the
    `py_proto_library` targets for the builtin protos. This ensures that the
    generated sources for the builtin protos will be in the same directory
    as the corresponding runtime sources.
    
    This patch was tested with the following:
    * All Python tests in protobuf
    * All Python tests in tensorflow
    * All tests in [Skydoc](https://github.com/bazelbuild/skydoc)
    * Importing protobuf as `//google/protobuf`
    * Importing and binding targets under `//external`
    * Importing protobuf as `//third_party/protobuf`
    02cd45ca
Name
Last commit
Last update
benchmarks Loading commit data...
cmake Loading commit data...
conformance Loading commit data...
csharp Loading commit data...
docs Loading commit data...
editors Loading commit data...
examples Loading commit data...
java Loading commit data...
javanano Loading commit data...
jenkins Loading commit data...
js Loading commit data...
m4 Loading commit data...
more_tests Loading commit data...
objectivec Loading commit data...
php Loading commit data...
protoc-artifacts Loading commit data...
python Loading commit data...
ruby Loading commit data...
src Loading commit data...
util/python Loading commit data...
.gitignore Loading commit data...
.travis.yml Loading commit data...
BUILD Loading commit data...
CHANGES.txt Loading commit data...
CONTRIBUTORS.txt Loading commit data...
LICENSE Loading commit data...
Makefile.am Loading commit data...
Protobuf.podspec Loading commit data...
README.md Loading commit data...
WORKSPACE Loading commit data...
appveyor.bat Loading commit data...
appveyor.yml Loading commit data...
autogen.sh Loading commit data...
configure.ac Loading commit data...
generate_descriptor_proto.sh Loading commit data...
gmock.BUILD Loading commit data...
post_process_dist.sh Loading commit data...
protobuf-lite.pc.in Loading commit data...
protobuf.bzl Loading commit data...
protobuf.pc.in Loading commit data...
six.BUILD Loading commit data...
tests.sh Loading commit data...
update_file_lists.sh Loading commit data...