-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invoking cargo run
from a build script
#6412
Comments
Thanks for the report! I don't think I quite follow though what this is a report about though? Is there an example of an error you're hitting? Or is this a feature request you'd like Cargo to add? |
Feature request for a really awkward use case. And probably a little XY problem obfuscated as well. The actual "problem" is that it's not possible to invoke a cargo command that involves building a crate in the workspace from a buildscript, due to the lock on the target directory. (Doing The direct solution is to give buildscripts some way to invoke The reason I want to use This is a messy fringe use case, so I can understand if it's not something cargo wants to support. I'm already breaking two taboos: writing to the source directory in a buildscript (but only when building from git sources) and publishing files that aren't committed. This executable based "dynamic linking" works even if it isn't the prettiest thing in the world. (I'm probably going to end up writing a blog post about the strategy, as ugly as it is, because it is, all things considered, a very good example of how bootstrapping works and doesn't take infinite time.) TL;DR feature request: some way to |
You can work around this by specifying |
Good to see other people having similar issues. I followed the same approach in my project. Thanks @CAD97 ! |
Any updates? I think this would be really useful. In my case, I want to integrate Wasm compilation into my regular build:
Which currently results in a deadlock too. |
Strangely enough I've not run into this problem myself recently, despite running cargo inside cargo; perhaps it's because I have |
Ran into the same issue today: secondary build script does not terminate presumably because of the lock. I have a parent project that should load The following example, as mentioned by @jethrogb, works for me. // build.rs
// ..
let mut opts = wasm_pack::command::build::BuildOptions::default();
opts.release = !cfg!(debug_assertions);
opts.path = Some(path.clone());
opts.extra_options = vec![
"--target-dir".to_owned(),
format!("{}/different_target", env!("CARGO_MANIFEST_DIR")),
];
wasm_pack::command::build::Build::try_from_opts(opts)
.unwrap()
.run()
.unwrap();
// .. |
Ran into this with windows-rs where I need to build and run other tools from within the same workspace from build scripts. Seems like an oversight that you cannot easily do so. The workaround works ok, but it breaks down when you want to ensure a rebuild and |
Can artifact-dependencies help with this matter? |
A sort-of workaround is to set the target dir to a sub directory of the workspace target dir. |
Any progress? I tried to use the above method to separate target_dir, but this will increase the disk space usage. I think it is possible to introduce unlock command in build.rs to temporarily provide the lock to the cargo command in build.rs example: fn main() {
// Unlock Cargo
println!("cargo:unlock");
// Run Cargo Command Here
let output = std::process::Command::new("cargo")
.args(["build", "--package", "other_package", "--release"])
.output()
.expect("Failed to run cargo build");
// Lock Again
println!("cargo:lock");
} |
#8938 is a more general issue about build scripts calling into Cargo. As was mentioned, it seems like artifact dependencies would be the solution to this problem. |
In the
pest
project, I currently have "solved" circular dependencies of bootstrap by "dynamically linking" to a version built from crates-io in order to do codegen, which is then published withcargo publish
and the codegen isn't done when building from crates-io. (So when building from git sources, you first build the current crates-io version, use that to pre-process the self-dependencies in the project, which generates some.rs
files which are dumped in the appropriate places in the workspace. This state is what is on crates-io, and skips the generation step.)This is done so in a very brittle way
because we cannot just use execute
cargo run --package pest_bootstrap
(or the aliascargo bootstrap
) as a childCommand
, as this needs to lock the build directory, which is already locked because we're inbuild.rs
.Ideally, I'd like some way to run
cargo bootstrap
before building the package that currently has this build script. But equally important is that this isn't run when building from crates-io, as a)pest_bootstrap
is a repository-local non-published script and b) it would result in endless dependency recursion if published to crates-io.A smaller improvement is a way to run
--package pest_bootstrap
without relying on this fragile "find the built binary", even if it still requires the user to build it beforehand. (I'm currently debugging it not working under tarpaulin. (As it turns out, I just missed that it does a clean first, so of course the file isn't going to be there.))The text was updated successfully, but these errors were encountered: