diff --git a/crates/uv-resolver/src/resolution/display.rs b/crates/uv-resolver/src/resolution/display.rs index 777f98555342..cbd3ac4a73c7 100644 --- a/crates/uv-resolver/src/resolution/display.rs +++ b/crates/uv-resolver/src/resolution/display.rs @@ -207,11 +207,19 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> { }; match self.annotation_style { - AnnotationStyle::Line => { - if !edges.is_empty() { + AnnotationStyle::Line => match edges.as_slice() { + [] if source.is_empty() => {} + [] if source.len() == 1 => { + let separator = if has_hashes { "\n " } else { " " }; + let comment = format!("# via {}", source.iter().next().unwrap()) + .green() + .to_string(); + annotation = Some((separator, comment)); + } + edges => { let separator = if has_hashes { "\n " } else { " " }; let deps = edges - .into_iter() + .iter() .map(|dependency| format!("{}", dependency.name())) .chain(source.iter().map(std::string::ToString::to_string)) .collect::>() @@ -219,7 +227,7 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> { let comment = format!("# via {deps}").green().to_string(); annotation = Some((separator, comment)); } - } + }, AnnotationStyle::Split => match edges.as_slice() { [] if source.is_empty() => {} [] if source.len() == 1 => { diff --git a/crates/uv/tests/pip_compile.rs b/crates/uv/tests/pip_compile.rs index 76f4793c1e67..5bd0119474f1 100644 --- a/crates/uv/tests/pip_compile.rs +++ b/crates/uv/tests/pip_compile.rs @@ -64,7 +64,7 @@ fn compile_requirements_in_annotation_line() -> Result<()> { ----- stdout ----- # This file was autogenerated by uv via the following command: # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z --annotation-style=line requirements.in - anyio==3.7.0 + anyio==3.7.0 # via -r requirements.in idna==3.6 # via anyio sniffio==1.3.1 # via anyio @@ -193,6 +193,43 @@ dependencies = [ Ok(()) } +/// Resolve a specific version of `anyio` from a `pyproject.toml` file with `--annotation-style=line`. +#[test] +fn compile_pyproject_toml_with_line_annotation() -> Result<()> { + let context = TestContext::new("3.12"); + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#"[build-system] +requires = ["setuptools", "wheel"] + +[project] +name = "project" +dependencies = [ + "anyio==3.7.0", +] +"#, + )?; + + uv_snapshot!(context.compile() + .arg("--annotation-style=line") + .arg("pyproject.toml"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by uv via the following command: + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z --annotation-style=line pyproject.toml + anyio==3.7.0 # via project (pyproject.toml) + idna==3.6 # via anyio + sniffio==1.3.1 # via anyio + + ----- stderr ----- + Resolved 3 packages in [TIME] + "### + ); + + Ok(()) +} + /// Resolve a package from a `requirements.in` file, with a `constraints.txt` file. #[test] fn compile_constraints_txt() -> Result<()> { @@ -880,7 +917,7 @@ fn compile_python_312_annotation_line() -> Result<()> { ----- stdout ----- # This file was autogenerated by uv via the following command: # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z --annotation-style=line requirements.in --python-version 3.12 - black==23.10.1 + black==23.10.1 # via -r requirements.in click==8.1.7 # via black mypy-extensions==1.0.0 # via black packaging==24.0 # via black @@ -2208,9 +2245,9 @@ optional-dependencies.bar = [ anyio==3.7.0 # via httpcore, project (pyproject.toml) certifi==2024.2.2 # via httpcore h11==0.14.0 # via httpcore - httpcore==0.18.0 + httpcore==0.18.0 # via project (pyproject.toml) idna==3.6 # via anyio - iniconfig==1.1.1 + iniconfig==1.1.1 # via project (pyproject.toml) sniffio==1.3.1 # via anyio, httpcore ----- stderr ----- @@ -8324,7 +8361,7 @@ fn emit_index_annotation_line() -> Result<()> { # from https://pypi.org/simple idna==3.6 # via requests # from https://pypi.org/simple - requests==2.31.0 + requests==2.31.0 # via -r requirements.in # from https://pypi.org/simple urllib3==2.2.1 # via requests # from https://pypi.org/simple