2

I have a project that holds some protobuf definitions and builds code for multiple (Python and Rust) languages. The folder structure is like this:

  • root/
    • proto/
      • my.proto
    • python/
    • rust/
      • Cargo.toml
      • build.rs
      • ...

I'm using prost to generate Rust code out of the proto files. My build.rs looks like this:

use std::io::Result;
fn main() -> Result<()> {
    prost_build::compile_protos(
        //  Files to be compiled
        &["my.proto"],
        //  Include folder for protoc
        &["../proto/"])?;
    Ok(())
}

This works fine if I run cargo build, but it does not work with cargo publish. In the output I see that publish seems to create a dedicated package subfolder in the target folder. protoc also tells me:

ignoring ../proto/ since it does not exist

I wonder why this works with build, but not publish. Can somebody explain? Can it be solved or is this kind of accessing files in relative pathes a bad idea?

Achim
  • 15,415
  • 15
  • 80
  • 144

1 Answers1

0

You can use the environment variable Cargo sets:

use std::io::Result;

fn main() -> Result<()> {
    let folder_path = Path::new(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("../proto");
    prost_build::compile_protos(
        //  Files to be compiled
        &["my.proto"],
        //  Include folder for protoc
        &[folder_path],
    )?;
    Ok(())
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77