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

Reflect exit code in uv tool run and uv run #6994

Merged
merged 1 commit into from
Sep 4, 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 crates/uv/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub(crate) enum ExitStatus {

/// The command failed with an unexpected error.
Error,

/// The command's exit status is propagated from an external command.
External(u8),
}

impl From<ExitStatus> for ExitCode {
Expand All @@ -88,6 +91,7 @@ impl From<ExitStatus> for ExitCode {
ExitStatus::Success => Self::from(0),
ExitStatus::Failure => Self::from(1),
ExitStatus::Error => Self::from(2),
ExitStatus::External(code) => Self::from(code),
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,20 @@ pub(crate) async fn run(
let status = handle.wait().await.context("Child process disappeared")?;

// Exit based on the result of the command
// TODO(zanieb): Do we want to exit with the code of the child process? Probably.
if status.success() {
Ok(ExitStatus::Success)
if let Some(code) = status.code() {
debug!("Command exited with code: {code}");
if let Ok(code) = u8::try_from(code) {
Ok(ExitStatus::External(code))
} else {
#[allow(clippy::exit)]
std::process::exit(code);
}
} else {
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
debug!("Command exited with signal: {:?}", status.signal());
}
Ok(ExitStatus::Failure)
}
}
Expand Down
16 changes: 13 additions & 3 deletions crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,20 @@ pub(crate) async fn run(
let status = handle.wait().await.context("Child process disappeared")?;

// Exit based on the result of the command
// TODO(zanieb): Do we want to exit with the code of the child process? Probably.
if status.success() {
Ok(ExitStatus::Success)
if let Some(code) = status.code() {
debug!("Command exited with code: {code}");
if let Ok(code) = u8::try_from(code) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand when this could fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, I wrote this previously

// Fail with 2 if the status cannot be cast to an exit code
Ok(status) => u8::try_from(status.code().unwrap_or(2)).unwrap_or(2).into(),
Err(err) => {
eprintln!("error: {err}");
ExitCode::from(2)
}

Ok(ExitStatus::External(code))
} else {
#[allow(clippy::exit)]
std::process::exit(code);
}
} else {
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
debug!("Command exited with signal: {:?}", status.signal());
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently on Unix you get None if it exits with a signal.

Ok(ExitStatus::Failure)
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,3 +1774,22 @@ fn run_compiled_python_file() -> Result<()> {

Ok(())
}

#[test]
fn run_exit_code() -> Result<()> {
let context = TestContext::new("3.12");

let test_script = context.temp_dir.child("script.py");
test_script.write_str(indoc! { r#"
# /// script
# requires-python = ">=3.11"
# ///
exit(42)
"#
})?;

context.run().arg("script.py").assert().code(42);

Ok(())
}
2 changes: 1 addition & 1 deletion crates/uv/tests/tool_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn tool_run_warn_executable_not_in_from() {
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: false
exit_code: 1
exit_code: 2
----- stdout -----
----- stderr -----
Expand Down
Loading