built icon indicating copy to clipboard operation
built copied to clipboard

File NotFound error when building package in cargo workspace

Open peter9477 opened this issue 1 year ago • 4 comments

I'm encountering a problem which I believe is from get_build_deps() trying to retrieve the Cargo.lock file from the manifest folder, inside a package using the new Rust 1.64.0 workspace inheritance feature.

I'm not certain of my analysis... still a bit of a Rust noob, but I've been able to narrow the problem down to that line. My build.rs code is identical to the example in the pyo3-built README .

I can build the example_project code as a crate within my workspace (listing it in workspace.members) as long as I leave its Cargo.lock file in its folder. As soon as I remove that file, I get the same NotFound error from get_build_deps(), which I believe should be looking in the parent folder when its in a workspace package.

peter9477 avatar Oct 24 '22 02:10 peter9477

You are correct. I've described the problem a bit at https://stackoverflow.com/questions/75052086/how-to-locate-the-cargo-lock-from-build-rs , along with a solution to use https://crates.io/crates/project-root , which needs to be used with https://docs.rs/built/latest/built/fn.write_built_file_with_opts.html in order to specify the appropriate directory explicitly.

jayvdb avatar Jan 09 '23 00:01 jayvdb

i.e. something like

fn main() {
    let dest = std::path::Path::new(&env::var("OUT_DIR").expect("OUT_DIR not set")).join("built.rs");
    let workspace_root = ...get_workspace_root()...;
    built::write_built_file_with_opts(
        Some(&workspace_root),
        &dest,
    ).expect("Failed to acquire build-time information");
}

where get_workspace_root is your own function that returns the directory containing Cargo.lock

jayvdb avatar Oct 20 '23 10:10 jayvdb

Recent commit bd869d1a38b375655243a47bcc6583fa7244c5d3 should work for most scenarios, although it doesn't actually implement cargo's workspace-behavior. Mind giving current HEAD a try if it works for you?

lukaslueg avatar Apr 11 '24 20:04 lukaslueg

Thanks @lukaslueg , I've tested master - it works well.

jayvdb avatar Apr 11 '24 22:04 jayvdb