From 9e715c89607786c41391452d984d7085a17f73e8 Mon Sep 17 00:00:00 2001 From: tjquillan Date: Thu, 22 Aug 2024 22:16:51 -0700 Subject: [PATCH 1/6] Revert modifications to pyproject.toml if sync fails --- crates/uv/src/commands/project/add.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 034726f675a5..6a6f13c84276 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -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, @@ -613,7 +613,17 @@ pub(crate) async fn add( cache, printer, ) - .await?; + .await + { + Err(_) => { + // Revert the changes to the `pyproject.toml`, if necessary. + if modified { + fs_err::write(project.root().join("pyproject.toml"), existing)?; + } + return Ok(ExitStatus::Failure); + } + _ => (), + } Ok(ExitStatus::Success) } From 2ae71222c3f467703e8287779a8ba0703bb879f7 Mon Sep 17 00:00:00 2001 From: tjquillan Date: Thu, 22 Aug 2024 23:03:51 -0700 Subject: [PATCH 2/6] Improve error printing --- crates/uv/src/commands/project/add.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 6a6f13c84276..c39c6ffbf4c7 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -615,12 +615,13 @@ pub(crate) async fn add( ) .await { - Err(_) => { + Err(err) => { // Revert the changes to the `pyproject.toml`, if necessary. if modified { fs_err::write(project.root().join("pyproject.toml"), existing)?; } - return Ok(ExitStatus::Failure); + eprint!("{err:?}"); + return Err(err.into()); } _ => (), } From e045ba278cb268bd05bb434f430014f4b359c13e Mon Sep 17 00:00:00 2001 From: tjquillan Date: Thu, 22 Aug 2024 23:04:19 -0700 Subject: [PATCH 3/6] Add test for uv add failure --- crates/uv/tests/edit.rs | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index 536dadcbeb19..c76e584b3e01 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -3873,3 +3873,87 @@ 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] + Operation(Anyhow(Failed to prepare distributions + + Caused by: + 0: Failed to fetch wheel: pytorch==1.0.2 + 1: Build backend failed to build wheel through `build_wheel()` with exit status: 1 + --- stdout: + + --- stderr: + Traceback (most recent call last): + File "", line 11, in + 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 "", line 15, in + Exception: You tried to install "pytorch". The package named for PyTorch is "torch" + ---))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 "", line 11, in + 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 "", line 15, in + 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(()) +} From 9c7e0b8da6c120c80fb0568187c6cfa4c9968f39 Mon Sep 17 00:00:00 2001 From: tjquillan Date: Fri, 23 Aug 2024 06:50:02 -0700 Subject: [PATCH 4/6] Don't print error now that we return it upwards --- crates/uv/src/commands/project/add.rs | 1 - crates/uv/tests/edit.rs | 23 +---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index c39c6ffbf4c7..0317142a4134 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -620,7 +620,6 @@ pub(crate) async fn add( if modified { fs_err::write(project.root().join("pyproject.toml"), existing)?; } - eprint!("{err:?}"); return Err(err.into()); } _ => (), diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index c76e584b3e01..8a2194ca7acb 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -3896,28 +3896,7 @@ fn fail_to_add_revert_project() -> Result<()> { ----- stderr ----- Resolved 2 packages in [TIME] - Operation(Anyhow(Failed to prepare distributions - - Caused by: - 0: Failed to fetch wheel: pytorch==1.0.2 - 1: Build backend failed to build wheel through `build_wheel()` with exit status: 1 - --- stdout: - - --- stderr: - Traceback (most recent call last): - File "", line 11, in - 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 "", line 15, in - Exception: You tried to install "pytorch". The package named for PyTorch is "torch" - ---))error: Failed to prepare distributions + 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: From 75c2c85a04304e314e9f4da3362fd11cdbf6d54a Mon Sep 17 00:00:00 2001 From: tjquillan Date: Fri, 23 Aug 2024 09:03:13 -0700 Subject: [PATCH 5/6] Switch to if let from match --- crates/uv/src/commands/project/add.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 0317142a4134..c5d41942639c 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -597,7 +597,7 @@ pub(crate) async fn add( // Initialize any shared state. let state = SharedState::default(); - match project::sync::do_sync( + if let Err(err) = project::sync::do_sync( &project, &venv, &lock, @@ -615,14 +615,11 @@ pub(crate) async fn add( ) .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()); + // 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) From a6b620a86bf348e408f1015886957a22a52e42a6 Mon Sep 17 00:00:00 2001 From: tjquillan Date: Fri, 23 Aug 2024 09:47:32 -0700 Subject: [PATCH 6/6] Update filter for windows tests Also fixed comment to be accurate --- crates/uv/tests/edit.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index 8a2194ca7acb..87971a33af2b 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -3888,8 +3888,11 @@ fn fail_to_add_revert_project() -> Result<()> { dependencies = [] "#})?; - // Adding `anyio` should include a lower-bound. - uv_snapshot!(context.filters(), context.add(&["pytorch==1.0.2"]), @r###" + // Adding `pytorch==1.0.2` should produce an error + let filters = std::iter::once((r"exit code: 1", "exit status: 1")) + .chain(context.filters()) + .collect::>(); + uv_snapshot!(filters, context.add(&["pytorch==1.0.2"]), @r###" success: false exit_code: 2 ----- stdout -----