diff --git a/crates/uv-workspace/src/pyproject_mut.rs b/crates/uv-workspace/src/pyproject_mut.rs index 248bcc103..10d7591ed 100644 --- a/crates/uv-workspace/src/pyproject_mut.rs +++ b/crates/uv-workspace/src/pyproject_mut.rs @@ -1272,15 +1272,11 @@ fn reformat_array_multiline(deps: &mut Array) { .map(|(s, _)| s) .unwrap_or(decor_prefix); - // If there is no indentation, use four-space. - indentation_prefix = Some(if decor_prefix.is_empty() { - " ".to_string() - } else { - decor_prefix.to_string() - }); + indentation_prefix = (!decor_prefix.is_empty()).then_some(decor_prefix.to_string()); } - let indentation_prefix_str = format!("\n{}", indentation_prefix.as_ref().unwrap()); + let indentation_prefix_str = + format!("\n{}", indentation_prefix.as_deref().unwrap_or(" ")); for comment in find_comments(decor.prefix()).chain(find_comments(decor.suffix())) { match comment.comment_type { @@ -1306,7 +1302,7 @@ fn reformat_array_multiline(deps: &mut Array) { match comment.comment_type { CommentType::OwnLine => { let indentation_prefix_str = - format!("\n{}", indentation_prefix.as_ref().unwrap()); + format!("\n{}", indentation_prefix.as_deref().unwrap_or(" ")); rv.push_str(&indentation_prefix_str); } CommentType::EndOfLine => { diff --git a/crates/uv/tests/it/edit.rs b/crates/uv/tests/it/edit.rs index 537f2f83d..84f8cd2ce 100644 --- a/crates/uv/tests/it/edit.rs +++ b/crates/uv/tests/it/edit.rs @@ -9024,3 +9024,64 @@ fn remove_requirement() -> Result<()> { Ok(()) } + +/// Remove all dependencies with remaining comments +#[test] +fn remove_all_with_comments() -> 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 = [ + "duct", + "minilog", + # foo + # bar + ] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + "#})?; + + uv_snapshot!(context.filters(), context.remove().arg("duct").arg("minilog"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 1 package in [TIME] + Prepared 1 package in [TIME] + Installed 1 package in [TIME] + + project==0.1.0 (from file://[TEMP_DIR]/) + "###); + + let pyproject_toml = context.read("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 = [ + # foo + # bar + ] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + "### + ); + }); + + Ok(()) +}