Search code examples
rustrust-cargo

Published cargo package using include_str does not install without specifying --path


My rust project includes a .csv data file that is read in the main program. For this purpose I use the include_str macro, so the file is just available in the program as a &str; in src/lib.rs:

let csv = include_str!("../resources/tla-unicode.csv")

This generated some code in build.rs:

fn main() {
    println!("cargo:rerun-if-changed=resources/tla-unicode.csv");
    let input_path = Path::new("resources/tla-unicode.csv");
    let output_path = Path::new(&get_output_path()).join("tla-unicode.csv");
    if let Err(e) = std::fs::copy(input_path, output_path) {
        println!("cargo:warning={:?}", e);
        std::process::exit(-1);
    }
}

fn get_output_path() -> PathBuf {
    //<root or manifest path>/target/<profile>/
    let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap();
    let build_type = env::var("PROFILE").unwrap();
    let path = Path::new(&manifest_dir_string)
        .join("target")
        .join(build_type);
    path
}

Things that work just fine:

  • Local build with cargo build
  • Local package with cargo package
  • Local install with cargo install tlauc --path ...
  • Manual download of package from crates.io and install with cargo install tlauc --path ...
  • Installation of any other package with cargo install packagename

Things that do not work: actually running cargo install tlauc, which gives the following error (tested on two separate computers, one running linux and another macos):

The following warnings were emitted during compilation:

warning: Os { code: 2, kind: NotFound, message: "No such file or directory" }

error: failed to run custom build command for `tlauc v0.1.0`

Caused by:
  process didn't exit successfully: `/tmp/cargo-installk53a0L/release/build/tlauc-517ed431b263ef48/build-script-build` (exit status: 255)
  --- stdout
  cargo:rerun-if-changed=resources/tla-unicode.csv
  cargo:warning=Os { code: 2, kind: NotFound, message: "No such file or directory" }
warning: build failed, waiting for other jobs to finish...
error: failed to compile `tlauc v0.1.0`, intermediate artifacts can be found at `/tmp/cargo-installk53a0L`

Why am I getting this error? The project in question is here and the package is here.


Solution

  • I fixed this issue by simply deleting the generated build.rs file. Nothing bad seems to have happened as a result, all works well.