google / cargo-raze

Generate Bazel BUILD from Cargo dependencies!
Apache License 2.0
478 stars 104 forks source link

cargo package from a git workspace uses wrong paths #433

Open rbtcollins opened 3 years ago

rbtcollins commented 3 years ago

This is similar - perhaps a dupe of #399

Background: cargo does not permit specifying a crate in git in a git repository that uses workspace configuration; instead the whole workspace is specified, and then cargo picks out the desired crate by introspection.

Consider this example: example.zip

This uses the protobuf and protoc-rust crates, but fails at compile time: ``` bazel build //cargo:protobuf Starting local Bazel server and connecting to it... INFO: Analyzed target //cargo:protobuf (40 packages loaded, 30634 targets configured). INFO: Found 1 target... ERROR: /home/robertc/.cache/bazel/_bazel_robertc/b142519a5cb9939c6f80c208e4bddbd6/external/raze__protobuf__3_0_0_pre/BUILD.bazel:39:19: Creating runfiles tree bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles failed: build-runfiles failed: error executing command (cd /home/robertc/.cache/bazel/_bazel_robertc/b142519a5cb9939c6f80c208e4bddbd6/execroot/t && \ exec env - \ PATH=/home/robertc/.cache/bazelisk/downloads/bazelbuild/bazel-4.1.0-linux-x86_64/bin:/home/robertc/go/1.16.5/bin:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.goenv/libexec:/home/robertc/.goenv/plugins/go-build/bin:/home/robertc/.goenv/plugins/go-build/bin:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.goenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.cargo/bin:/home/robertc/.local/bin:/home/robertc/.nodenv/bin:/home/robertc/.goenv/bin:/home/robertc/.pyenv/shims:/home/robertc/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin \ /home/robertc/.cache/bazel/_bazel_robertc/install/f95ca91ebc34d56aa0f8ad499de91220/build-runfiles bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles_manifest bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles): Process exited with status 1: Process exited with status 1 /home/robertc/.cache/bazel/_bazel_robertc/install/f95ca91ebc34d56aa0f8ad499de91220/build-runfiles (args bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles_manifest bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles): link or target filename contains space on line 304: 'raze__protobuf__3_0_0_pre/protobuf-test/src/v2/test_special~characters file{name}_pb.proto /home/robertc/.cache/bazel/_bazel_robertc/b142519a5cb9939c6f80c208e4bddbd6/external/raze__protobuf__3_0_0_pre/protobuf-test/src/v2/test_special~characters file{name}_pb.proto' Target @raze__protobuf__3_0_0_pre//:protobuf failed to build Use --verbose_failures to see the command lines of failed build steps. ERROR: /home/robertc/.cache/bazel/_bazel_robertc/b142519a5cb9939c6f80c208e4bddbd6/external/raze__protobuf__3_0_0_pre/BUILD.bazel:72:13 Compiling Rust rlib protobuf v3.0.0-pre (253 files) failed: build-runfiles failed: error executing command (cd /home/robertc/.cache/bazel/_bazel_robertc/b142519a5cb9939c6f80c208e4bddbd6/execroot/t && \ exec env - \ PATH=/home/robertc/.cache/bazelisk/downloads/bazelbuild/bazel-4.1.0-linux-x86_64/bin:/home/robertc/go/1.16.5/bin:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.goenv/libexec:/home/robertc/.goenv/plugins/go-build/bin:/home/robertc/.goenv/plugins/go-build/bin:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.goenv/versions/1.16.5/bin:/home/robertc/.nodenv/shims:/home/robertc/.goenv/shims:/home/robertc/.pyenv/plugins/pyenv-virtualenv/shims:/home/robertc/.cargo/bin:/home/robertc/.local/bin:/home/robertc/.nodenv/bin:/home/robertc/.goenv/bin:/home/robertc/.pyenv/shims:/home/robertc/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin:/home/robertc/go/1.16.5/bin \ /home/robertc/.cache/bazel/_bazel_robertc/install/f95ca91ebc34d56aa0f8ad499de91220/build-runfiles bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles_manifest bazel-out/k8-opt-exec-2B5CBBC6/bin/external/raze__protobuf__3_0_0_pre/protobuf_build_script_script_.runfiles): Process exited with status 1: Process exited with status 1 INFO: Elapsed time: 12.275s, Critical Path: 0.15s INFO: 2 processes: 2 internal. FAILED: Build did NOT complete successfully ```

I'm not 100% sure - still learning bazel - but I think what is happening is that the protobuf-test crate build.rs is being compiled instead of the protobuf crate build.rs. I think this because if you look at the -test build.rs, it outputs build instructions for that file that is being complained about.

(Note - I didn't add local targets as they aren't needed to show the problem. bazel query ...:* - bazel build @raze__protoc_rust__3_0_0_pre//:protoc_rust will fail with the correct error)

rdelfin commented 2 years ago

I've been able to replicate this with prost. I've had to fork off their repo to apply a branch that added some needed introspection for using the Any type. Unfortunately, as soon as I pointed cargo raze at the new repo, it keeps on trying to build the crate at the root instead of the sub-packages. I've been able to confirm this is what's happening by looking at the generated bazel files and, sure enough, both the targets and the download are using the whole repo. There's a surprising number of crates that do this kind of split within the same repo, mostly ones with a main and a sys crate, but also ones with one package for use in build scripts like the aformentioned prost. This does make using these libraries quite difficult without doing things like setting up our own crates registry.