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

Revert changes to pyproject.toml when sync fails duing uv add #6526

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ pub(crate) async fn add(
// Initialize any shared state.
let state = SharedState::default();

project::sync::do_sync(
match project::sync::do_sync(
&project,
&venv,
&lock,
Expand All @@ -613,7 +613,17 @@ pub(crate) async fn add(
cache,
printer,
)
.await?;
.await
{
Err(err) => {
// Revert the changes to the `pyproject.toml`, if necessary.
if modified {
fs_err::write(project.root().join("pyproject.toml"), existing)?;
}
return Err(err.into());
}
_ => (),
}

Ok(ExitStatus::Success)
}
Expand Down
63 changes: 63 additions & 0 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3873,3 +3873,66 @@ fn add_git_to_script() -> Result<()> {
});
Ok(())
}

// Revert changes to pyproject.toml if add fails
#[test]
fn fail_to_add_revert_project() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
"#})?;

// Adding `anyio` should include a lower-bound.
uv_snapshot!(context.filters(), context.add(&["pytorch==1.0.2"]), @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
Resolved 2 packages in [TIME]
error: Failed to prepare distributions
Caused by: Failed to fetch wheel: pytorch==1.0.2
Caused by: Build backend failed to build wheel through `build_wheel()` with exit status: 1
--- stdout:

--- stderr:
Traceback (most recent call last):
File "<string>", line 11, in <module>
File "[CACHE_DIR]/builds-v0/[TMP]/build_meta.py", line 410, in build_wheel
return self._build_with_temp_dir(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "[CACHE_DIR]/builds-v0/[TMP]/build_meta.py", line 395, in _build_with_temp_dir
self.run_setup()
File "[CACHE_DIR]/builds-v0/[TMP]/build_meta.py", line 487, in run_setup
super().run_setup(setup_script=setup_script)
File "[CACHE_DIR]/builds-v0/[TMP]/build_meta.py", line 311, in run_setup
exec(code, locals())
File "<string>", line 15, in <module>
Exception: You tried to install "pytorch". The package named for PyTorch is "torch"
---
"###);

let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
"###
);
});

Ok(())
}
Loading