Skip to content

Commit

Permalink
Revert pyproject.toml modifications on Ctrl-C (#7024)
Browse files Browse the repository at this point in the history
## Summary

Not perfect, but an improvement at least for an interactive experience.

Closes #6818.
  • Loading branch information
charliermarsh authored Sep 4, 2024
1 parent 2b294b9 commit ae144e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ anstream = { workspace = true }
anyhow = { workspace = true }
axoupdater = { workspace = true, features = ["github_releases", "tokio"], optional = true }
clap = { workspace = true, features = ["derive", "string", "wrap_help"] }
ctrlc = { workspace = true }
flate2 = { workspace = true, default-features = false }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
Expand Down
23 changes: 21 additions & 2 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,25 @@ pub(crate) async fn add(
.with_pyproject_toml(toml::from_str(&content).map_err(ProjectError::TomlParse)?)
.ok_or(ProjectError::TomlUpdate)?;

// Set the Ctrl-C handler to revert changes on exit.
let _ = ctrlc::set_handler({
let root = root.clone();
let existing = existing.clone();
move || {
// Revert the changes to the `pyproject.toml`, if necessary.
if modified {
let _ = fs_err::write(root.join("pyproject.toml"), &existing);
}

#[allow(clippy::exit, clippy::cast_possible_wrap)]
std::process::exit(if cfg!(windows) {
0xC000_013A_u32 as i32
} else {
130
});
}
});

match lock_and_sync(
project,
&mut toml,
Expand Down Expand Up @@ -518,15 +537,15 @@ pub(crate) async fn add(

// Revert the changes to the `pyproject.toml`, if necessary.
if modified {
fs_err::write(root.join("pyproject.toml"), existing)?;
fs_err::write(root.join("pyproject.toml"), &existing)?;
}

Ok(ExitStatus::Failure)
}
Err(err) => {
// Revert the changes to the `pyproject.toml`, if necessary.
if modified {
fs_err::write(root.join("pyproject.toml"), existing)?;
fs_err::write(root.join("pyproject.toml"), &existing)?;
}
Err(err.into())
}
Expand Down

0 comments on commit ae144e0

Please sign in to comment.