Revert pyproject.toml modifications on Ctrl-C (#7024)

## Summary

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

Closes #6818.
This commit is contained in:
Charlie Marsh 2024-09-04 11:04:00 -04:00 committed by GitHub
parent 2b294b90f2
commit ae144e05ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 2 deletions

1
Cargo.lock generated
View file

@ -4463,6 +4463,7 @@ dependencies = [
"byteorder", "byteorder",
"cache-key", "cache-key",
"clap", "clap",
"ctrlc",
"distribution-types", "distribution-types",
"etcetera", "etcetera",
"filetime", "filetime",

View file

@ -48,6 +48,7 @@ anstream = { workspace = true }
anyhow = { workspace = true } anyhow = { workspace = true }
axoupdater = { workspace = true, features = ["github_releases", "tokio"], optional = true } axoupdater = { workspace = true, features = ["github_releases", "tokio"], optional = true }
clap = { workspace = true, features = ["derive", "string", "wrap_help"] } clap = { workspace = true, features = ["derive", "string", "wrap_help"] }
ctrlc = { workspace = true }
flate2 = { workspace = true, default-features = false } flate2 = { workspace = true, default-features = false }
fs-err = { workspace = true, features = ["tokio"] } fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true } futures = { workspace = true }

View file

@ -488,6 +488,25 @@ pub(crate) async fn add(
.with_pyproject_toml(toml::from_str(&content).map_err(ProjectError::TomlParse)?) .with_pyproject_toml(toml::from_str(&content).map_err(ProjectError::TomlParse)?)
.ok_or(ProjectError::TomlUpdate)?; .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( match lock_and_sync(
project, project,
&mut toml, &mut toml,
@ -518,7 +537,7 @@ pub(crate) async fn add(
// Revert the changes to the `pyproject.toml`, if necessary. // Revert the changes to the `pyproject.toml`, if necessary.
if modified { if modified {
fs_err::write(root.join("pyproject.toml"), existing)?; fs_err::write(root.join("pyproject.toml"), &existing)?;
} }
Ok(ExitStatus::Failure) Ok(ExitStatus::Failure)
@ -526,7 +545,7 @@ pub(crate) async fn add(
Err(err) => { Err(err) => {
// Revert the changes to the `pyproject.toml`, if necessary. // Revert the changes to the `pyproject.toml`, if necessary.
if modified { if modified {
fs_err::write(root.join("pyproject.toml"), existing)?; fs_err::write(root.join("pyproject.toml"), &existing)?;
} }
Err(err.into()) Err(err.into())
} }