Skip to content
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

Extend CommandError::ExecutionFailed to optionally include stderr #91

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.18.0] - 2024-10-13

- **BREAKING** Extend `CommandError::ExecutionFailed` and `PrepareError` to optionally include stderr

## [0.17.0] - 2024-06-26

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustwide"
version = "0.17.0"
version = "0.18.0"
edition = "2018"
build = "build.rs"

Expand Down
14 changes: 11 additions & 3 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ pub enum CommandError {
Timeout(u64),

/// The command failed to execute.
#[error("command failed: {0}")]
ExecutionFailed(ExitStatus),
#[error("command failed: {status}\n\n{stderr}")]
ExecutionFailed {
/// the exit status we got from the command
status: ExitStatus,
/// the stderr output, if it was captured via `.run_capture()`
stderr: String,
},

/// Killing the underlying process after the timeout failed.
#[error("{0}")]
Expand Down Expand Up @@ -496,7 +501,10 @@ impl<'w, 'pl> Command<'w, 'pl> {
if out.status.success() {
Ok(out.into())
} else {
Err(CommandError::ExecutionFailed(out.status))
Err(CommandError::ExecutionFailed {
status: out.status,
stderr: out.stderr.join("\n"),
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl Container<'_> {
// Return a different error if the container was killed due to an OOM
if details.state.oom_killed {
Err(match res {
Ok(_) | Err(CommandError::ExecutionFailed(_)) => CommandError::SandboxOOM,
Ok(_) | Err(CommandError::ExecutionFailed { .. }) => CommandError::SandboxOOM,
Err(err) => err,
})
} else {
Expand Down
42 changes: 23 additions & 19 deletions src/prepare.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmd::Command;
use crate::cmd::{Command, CommandError};
use crate::{build::CratePatch, Crate, Toolchain, Workspace};
use anyhow::Context as _;
use log::info;
Expand Down Expand Up @@ -113,7 +113,8 @@ impl<'a> Prepare<'a> {
.args(&["-Zno-index-update"])
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
}
let res = cmd

match cmd
.cd(self.source_dir)
.process_lines(&mut |line, _| {
if line.contains("failed to select a version for the requirement") {
Expand All @@ -124,17 +125,17 @@ impl<'a> Prepare<'a> {
missing_deps = true;
}
})
.run();
match res {
Err(_) if yanked_deps => {
return Err(PrepareError::YankedDependencies.into());
.run_capture()
{
Ok(_) => Ok(()),
Err(CommandError::ExecutionFailed { status: _, stderr }) if yanked_deps => {
Err(PrepareError::YankedDependencies(stderr).into())
}
Err(_) if missing_deps => {
return Err(PrepareError::MissingDependencies.into());
Err(CommandError::ExecutionFailed { status: _, stderr }) if missing_deps => {
Err(PrepareError::MissingDependencies(stderr).into())
}
other => other?,
Err(err) => Err(err.into()),
}
Ok(())
}

fn fetch_deps(&mut self) -> anyhow::Result<()> {
Expand All @@ -161,17 +162,20 @@ pub(crate) fn fetch_deps(
for target in fetch_build_std_targets {
cmd = cmd.args(&["--target", target]);
}
let res = cmd

match cmd
.process_lines(&mut |line, _| {
if line.contains("failed to load source for dependency") {
missing_deps = true;
}
})
.run();
match res {
.run_capture()
{
Ok(_) => Ok(()),
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),
err => err.map_err(|e| e.into()),
Err(CommandError::ExecutionFailed { status: _, stderr }) if missing_deps => {
Err(PrepareError::MissingDependencies(stderr).into())
}
Err(err) => Err(err.into()),
}
}

Expand Down Expand Up @@ -381,11 +385,11 @@ pub enum PrepareError {
#[error("invalid Cargo.toml syntax")]
InvalidCargoTomlSyntax,
/// Some of this crate's dependencies were yanked, preventing Crater from fetching them.
#[error("the crate depends on yanked dependencies")]
YankedDependencies,
#[error("the crate depends on yanked dependencies: \n\n{0}")]
YankedDependencies(String),
/// Some of the dependencies do not exist anymore.
#[error("the crate depends on missing dependencies")]
MissingDependencies,
#[error("the crate depends on missing dependencies: \n\n{0}")]
MissingDependencies(String),
}

#[cfg(test)]
Expand Down
22 changes: 15 additions & 7 deletions tests/buildtest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,30 @@ test_prepare_error!(
InvalidCargoTomlSyntax
);

test_prepare_error!(test_yanked_deps, "yanked-deps", YankedDependencies);
test_prepare_error_stderr!(
test_yanked_deps,
"yanked-deps",
YankedDependencies,
r#"failed to select a version for the requirement `ring = "^0.2"`"#
);

test_prepare_error!(
test_prepare_error_stderr!(
test_missing_deps_git,
"missing-deps-git",
MissingDependencies
MissingDependencies,
"failed to get `not-a-git-repo` as a dependency of package `missing-deps v0.1.0"
);

test_prepare_error!(
test_prepare_error_stderr!(
test_missing_deps_git_locked,
"missing-deps-git-locked",
MissingDependencies
MissingDependencies,
"failed to get `not-a-git-repo` as a dependency of package `missing-deps-git-locked v0.1.0"
);

test_prepare_error!(
test_prepare_error_stderr!(
test_missing_deps_registry,
"missing-deps-registry",
MissingDependencies
MissingDependencies,
"error: no matching package named `macro` found"
);
22 changes: 22 additions & 0 deletions tests/buildtest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ macro_rules! test_prepare_error {
}
};
}

macro_rules! test_prepare_error_stderr {
($name:ident, $krate:expr, $expected:ident, $expected_output:expr) => {
#[test]
fn $name() {
runner::run($krate, |run| {
let res = run.run(
rustwide::cmd::SandboxBuilder::new().enable_networking(false),
|_| Ok(()),
);
if let Some(rustwide::PrepareError::$expected(output)) =
res.err().and_then(|err| err.downcast().ok())
{
assert!(output.contains($expected_output), "output: {:?}", output);
} else {
panic!("didn't get the error {}", stringify!($expected));
}
Ok(())
});
}
};
}