cargo: cargo sometimes fails to run a build.rs that generates the src/lib.rs
This came up in the context of tango, specifically the tango-demo project, which attempts to show off that one can have all checked-files under src/ be markdown source rather than Rust source.
The intention is that the build script will always regenerate the src/lib.rs if it is out of date or has been deleted.
However, this sometimes does not happen. It is hard for me to tell exactly what the scenario is that is causing it to fail.
Here is a sample project that hopefully captures the essence of what I am trying to do:
Two files (only) at outset:
gen-lib/
|
+- Cargo.toml
|
+- build.rs
Cargo.toml:
[package]
name = "gen-lib"
version = "0.1.0"
authors = ["Felix S. Klock II <pnkfelix@pnkfx.org>"]
[dependencies]
[lib]
name = "gen_lib"
path = "src/lib.rs"
build.rs:
use std::fs::{self, File};
use std::io::{self, Write};
fn main() {
generate_lib_rs().unwrap();
}
fn generate_lib_rs() -> Result<(), io::Error> {
match fs::create_dir("src") {
Ok(_) => (), // great, we built it.
Err(_) => (), // okay, lets assume that was a file-exists error...
}
let mut f = File::create("src/lib.rs")?;
write!(f, "{}", "\
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
")
}
Here is an example of a troubling interaction. Sometimes cargo build seems to know to run the build script, but other times it gives up as soon as it sees that the src/lib.rs does not exist. And I do not see any hints as to why there is a difference in the --verbose output.
% rm -rf src target build.rs~ Cargo.lock Cargo.toml~
% ls && echo && date && echo && cargo build --verbose -vv && echo && date
build.rs Cargo.toml
Mon Sep 4 15:44:05 CEST 2017
Compiling gen-lib v0.1.0 (file:///home/pnkfelix/Dev/Rust/gen-lib)
Running `rustc --crate-name build_script_build build.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=66f9d79c9066950c -C extra-filename=-66f9d79c9066950c --out-dir /home/pnkfelix/Dev/Rust/gen-lib/\
target/debug/build/gen-lib-66f9d79c9066950c -L dependency=/home/pnkfelix/Dev/Rust/gen-lib/target/debug/deps`
Running `/home/pnkfelix/Dev/Rust/gen-lib/target/debug/build/gen-lib-66f9d79c9066950c/build-script-build`
Running `rustc --crate-name gen_lib src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=8fb3f0fed1d71daa -C extra-filename=-8fb3f0fed1d71daa --out-dir /home/pnkfelix/Dev/Rust/gen-lib/target/de\
bug/deps -L dependency=/home/pnkfelix/Dev/Rust/gen-lib/target/debug/deps`
Finished dev [unoptimized + debuginfo] target(s) in 0.45 secs
Mon Sep 4 15:44:06 CEST 2017
% ls && echo && date && echo && cargo build --verbose -vv && echo && date
build.rs Cargo.lock Cargo.toml src target
Mon Sep 4 15:44:10 CEST 2017
Compiling gen-lib v0.1.0 (file:///home/pnkfelix/Dev/Rust/gen-lib)
Running `/home/pnkfelix/Dev/Rust/gen-lib/target/debug/build/gen-lib-66f9d79c9066950c/build-script-build`
Running `rustc --crate-name gen_lib src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=8fb3f0fed1d71daa -C extra-filename=-8fb3f0fed1d71daa --out-dir /home/pnkfelix/Dev/Rust/gen-lib/target/de\
bug/deps -L dependency=/home/pnkfelix/Dev/Rust/gen-lib/target/debug/deps`
Finished dev [unoptimized + debuginfo] target(s) in 0.12 secs
Mon Sep 4 15:44:10 CEST 2017
% rm src/lib.rs # can we recover from removing the generated file first?
% ls && echo && date && echo && cargo build --verbose -vv && echo && date
build.rs Cargo.lock Cargo.toml src target
Mon Sep 4 15:44:14 CEST 2017
Compiling gen-lib v0.1.0 (file:///home/pnkfelix/Dev/Rust/gen-lib)
Running `/home/pnkfelix/Dev/Rust/gen-lib/target/debug/build/gen-lib-66f9d79c9066950c/build-script-build`
Running `rustc --crate-name gen_lib src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=8fb3f0fed1d71daa -C extra-filename=-8fb3f0fed1d71daa --out-dir /home/pnkfelix/Dev/Rust/gen-lib/target/de\
bug/deps -L dependency=/home/pnkfelix/Dev/Rust/gen-lib/target/debug/deps`
Finished dev [unoptimized + debuginfo] target(s) in 0.12 secs
Mon Sep 4 15:44:14 CEST 2017
% rm src/lib.rs # We happened to recover above. But does a second attempt fail?
% ls && echo && date && echo && cargo build --verbose -vv && echo && date
build.rs Cargo.lock Cargo.toml src target
Mon Sep 4 15:44:18 CEST 2017
Compiling gen-lib v0.1.0 (file:///home/pnkfelix/Dev/Rust/gen-lib)
Running `rustc --crate-name gen_lib src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=8fb3f0fed1d71daa -C extra-filename=-8fb3f0fed1d71daa --out-dir /home/pnkfelix/Dev/Rust/gen-lib/target/de\
bug/deps -L dependency=/home/pnkfelix/Dev/Rust/gen-lib/target/debug/deps`
error: couldn't read "src/lib.rs": No such file or directory (os error 2)
error: Could not compile `gen-lib`.
Caused by:
process didn't exit successfully: `rustc --crate-name gen_lib src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=8fb3f0fed1d71daa -C extra-filename=-8fb3f0fed1d71daa --out-dir /home/pnkfelix/Dev\
/Rust/gen-lib/target/debug/deps -L dependency=/home/pnkfelix/Dev/Rust/gen-lib/target/debug/deps` (exit code: 101)
%
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (11 by maintainers)
Considering this is running counter to documented practices and there seem to be ways to make those documented practices work in these situations, I lean towards closing this issue. If there is a reason for us to keep it open, let us know!
@CobaltCause Thanks a lot for pointing it out!
I’ve changed my (previously misleading) comment accordingly.
I had this issue as well. My problem was that
/build.rswas missing from the files. See the fix here: https://github.com/jdxcode/rtx/commit/41160c29c0fb0b194f388c2b0ada6d70804e4743Same error on
cargo publish, even thebuild.rsdoesn’t generatesrc/lib.rs.cargo buildworks fine.