-
Notifications
You must be signed in to change notification settings - Fork 45
Add extra_cargo_args support for prepare phase #119
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ pub struct BuildBuilder<'a> { | |
| krate: &'a Crate, | ||
| sandbox: SandboxBuilder, | ||
| patches: Vec<CratePatch>, | ||
| extra_cargo_args: Vec<String>, | ||
| } | ||
|
|
||
| /// Output of a completed build together with build-level statistics. | ||
|
|
@@ -131,6 +132,36 @@ impl BuildBuilder<'_> { | |
| self | ||
| } | ||
|
|
||
| /// Add extra arguments passed to cargo commands during the prepare phase | ||
| /// (manifest validation, lockfile generation, dependency fetching). | ||
| /// | ||
| /// This is useful for passing unstable cargo flags (e.g. `-Zbindeps`) that | ||
| /// are required for cargo to parse the crate's manifest. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```no_run | ||
| /// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::SandboxBuilder}; | ||
| /// # use std::error::Error; | ||
| /// # fn main() -> anyhow::Result<(), Box<dyn Error>> { | ||
| /// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?; | ||
| /// # let toolchain = Toolchain::dist(""); | ||
| /// # let krate = Crate::local("".as_ref()); | ||
| /// # let sandbox = SandboxBuilder::new(); | ||
| /// let mut build_dir = workspace.build_dir("foo"); | ||
| /// build_dir.build(&toolchain, &krate, sandbox) | ||
| /// .extra_cargo_args(vec!["-Zbindeps".into()]) | ||
| /// .run(|build| { | ||
| /// build.cargo().args(&["test", "--all"]).run()?; | ||
| /// Ok(()) | ||
| /// })?; | ||
| /// # Ok(()) | ||
| /// # } | ||
| pub fn extra_cargo_args(mut self, args: Vec<String>) -> Self { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, the pattern from pub fn extra_cargo_args<S: Into<OsString>>(
mut self,
args: impl IntoIterator<Item = S>,
) -> Self {
self.extra_cargo_args
.extend(args.into_iter().map(Into::into));
self
} |
||
| self.extra_cargo_args = args; | ||
| self | ||
| } | ||
|
|
||
| /// Run a sandboxed build of the provided crate with the provided toolchain. The closure will | ||
| /// be provided an instance of [`Build`](struct.Build.html) that allows spawning new processes | ||
| /// inside the sandbox. | ||
|
|
@@ -162,8 +193,14 @@ impl BuildBuilder<'_> { | |
| self, | ||
| f: F, | ||
| ) -> anyhow::Result<BuildResult<R>> { | ||
| self.build_dir | ||
| .run(self.toolchain, self.krate, self.sandbox, self.patches, f) | ||
| self.build_dir.run( | ||
| self.toolchain, | ||
| self.krate, | ||
| self.sandbox, | ||
| self.patches, | ||
| self.extra_cargo_args, | ||
| f, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -208,6 +245,7 @@ impl BuildDirectory { | |
| krate, | ||
| sandbox, | ||
| patches: Vec::new(), | ||
| extra_cargo_args: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -229,14 +267,22 @@ impl BuildDirectory { | |
| krate: &Crate, | ||
| sandbox: SandboxBuilder, | ||
| patches: Vec<CratePatch>, | ||
| extra_cargo_args: Vec<String>, | ||
| f: F, | ||
| ) -> anyhow::Result<BuildResult<R>> { | ||
| let source_dir = self.source_dir(); | ||
| if source_dir.exists() { | ||
| crate::utils::remove_dir_all(&source_dir)?; | ||
| } | ||
|
|
||
| let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches); | ||
| let mut prepare = Prepare::new( | ||
| &self.workspace, | ||
| toolchain, | ||
| krate, | ||
| &source_dir, | ||
| patches, | ||
| extra_cargo_args, | ||
| ); | ||
| prepare.prepare().map_err(|err| { | ||
| if err.downcast_ref::<PrepareError>().is_none() { | ||
| err.context(PrepareError::Uncategorized) | ||
|
|
@@ -407,6 +453,7 @@ impl<'ws> Build<'ws> { | |
| self.toolchain, | ||
| &self.host_source_dir(), | ||
| targets, | ||
| &[], | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,6 +341,37 @@ fn test_cargo_workspace() { | |
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_extra_cargo_args() { | ||
| runner::run("hello-world", |run| { | ||
| run.build(SandboxBuilder::new().enable_networking(false), |builder| { | ||
| builder | ||
| .extra_cargo_args(vec!["--quiet".into()]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we somehow assert that this cargo arg is actually working? Like, checking stdout for the output that this suppresses? |
||
| .run(|build| { | ||
| build.cargo().args(&["run"]).run()?; | ||
| Ok(()) | ||
| }) | ||
| })?; | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_extra_cargo_args_invalid() { | ||
| runner::run("hello-world", |run| { | ||
| let res = run.build(SandboxBuilder::new().enable_networking(false), |builder| { | ||
| builder | ||
| .extra_cargo_args(vec!["--invalid-flag-that-does-not-exist".into()]) | ||
| .run(|_build| Ok(())) | ||
| }); | ||
| assert!( | ||
| res.is_err(), | ||
| "expected extra cargo args to cause a prepare failure" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we assert something that ensures that the invalid flag is the actual error? perhaps just reading stdout and checking what's in it? |
||
| ); | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| test_prepare_error!( | ||
| test_missing_cargotoml, | ||
| "missing-cargotoml", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's follow the pattern we have in
cmd::Command