diff --git a/crates/uv/src/commands/pip_compile.rs b/crates/uv/src/commands/pip_compile.rs index 602fe2900947..0532663ba7bd 100644 --- a/crates/uv/src/commands/pip_compile.rs +++ b/crates/uv/src/commands/pip_compile.rs @@ -352,14 +352,7 @@ pub(crate) async fn pip_compile( writeln!( writer, "{}", - format!( - "# uv {}", - env::args_os() - .skip(1) - .map(|arg| arg.normalized_display().to_string()) - .join(" ") - ) - .green() + format!("# {}", cmd(include_index_url)).green() )?; } @@ -398,13 +391,43 @@ pub(crate) async fn pip_compile( &resolution, generate_hashes, include_annotations, - annotation_style + annotation_style, ) )?; Ok(ExitStatus::Success) } +/// Format the `uv` command used to generate the output file. +fn cmd(include_index_url: bool) -> String { + let args = env::args_os() + .skip(1) + .map(|arg| arg.normalized_display().to_string()) + .scan(None, move |skip_next, arg| { + if let Some(true) = skip_next { + // Reset state; skip this iteration. + *skip_next = None; + Some(None) + } else if !include_index_url + && (arg.starts_with("--extra-index-url=") || arg.starts_with("--index-url=")) + { + // Reset state; skip this iteration. + *skip_next = None; + Some(None) + } else if !include_index_url && (arg == "--extra-index-url" || arg == "--index-url") { + // Mark the next item as (to be) skipped. + *skip_next = Some(true); + Some(None) + } else { + // Return the argument. + Some(Some(arg)) + } + }) + .flatten() + .join(" "); + format!("uv {args}") +} + /// Whether to allow package upgrades. #[derive(Debug)] pub(crate) enum Upgrade { diff --git a/crates/uv/tests/pip_compile.rs b/crates/uv/tests/pip_compile.rs index 2857465e06b0..7f4a7531b802 100644 --- a/crates/uv/tests/pip_compile.rs +++ b/crates/uv/tests/pip_compile.rs @@ -650,9 +650,9 @@ fn compile_python_37() -> Result<()> { "", ), ] - .into_iter() - .chain(INSTA_FILTERS.to_vec()) - .collect(); + .into_iter() + .chain(INSTA_FILTERS.to_vec()) + .collect(); uv_snapshot!(filters, context.compile() .arg("requirements.in") @@ -2608,7 +2608,7 @@ fn compile_html() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile requirements.in --cache-dir [CACHE_DIR] --index-url https://download.pytorch.org/whl + # uv pip compile requirements.in --cache-dir [CACHE_DIR] jinja2==3.1.2 markupsafe==2.1.3 # via jinja2 @@ -2636,7 +2636,7 @@ fn trailing_slash() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://test.pypi.org/simple + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in jinja2==3.1.2 markupsafe==2.1.3 # via jinja2 @@ -2654,7 +2654,7 @@ fn trailing_slash() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://test.pypi.org/simple/ + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in jinja2==3.1.2 markupsafe==2.1.3 # via jinja2 @@ -3393,6 +3393,7 @@ fn resolver_legacy() -> Result<()> { } /// Emit the `--index-url` and `--extra-index-url` locations. +/// Also, preserve the `--index-url` and `--extra-index-url` flags in the command in the header. #[test] fn emit_index_urls() -> Result<()> { let context = TestContext::new("3.12"); @@ -3499,7 +3500,8 @@ fn no_index_requirements_txt() -> Result<()> { } /// Prefer the `--index-url` from the command line over the `--index-url` in a `requirements.txt` -/// file. +/// file. Also, `--index-url` and `--extra-index-url` should not be presented in the output +/// unless we specify `--emit-index-url`. #[test] fn index_url_requirements_txt() -> Result<()> { let context = TestContext::new("3.12"); @@ -3514,7 +3516,7 @@ fn index_url_requirements_txt() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://pypi.org/simple + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in tqdm==4.66.1 ----- stderr ----- @@ -3905,7 +3907,7 @@ fn index_url_from_command_line() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://pypi.org/simple + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in anyio==3.7.1 idna==3.4 # via anyio diff --git a/crates/uv/tests/pip_compile_scenarios.rs b/crates/uv/tests/pip_compile_scenarios.rs index 865ce378e9e0..8fb0548b940e 100644 --- a/crates/uv/tests/pip_compile_scenarios.rs +++ b/crates/uv/tests/pip_compile_scenarios.rs @@ -83,7 +83,7 @@ fn requires_incompatible_python_version_compatible_override() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile requirements.in --index-url https://test.pypi.org/simple --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11 + # uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11 albatross==1.0.0 ----- stderr ----- @@ -244,7 +244,7 @@ fn requires_incompatible_python_version_compatible_override_no_wheels_available_ exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile requirements.in --index-url https://test.pypi.org/simple --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11 + # uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11 albatross==1.0.0 ----- stderr ----- @@ -464,7 +464,7 @@ fn requires_python_patch_version_override_patch_compatible() -> Result<()> { exit_code: 0 ----- stdout ----- # This file was autogenerated by uv via the following command: - # uv pip compile requirements.in --index-url https://test.pypi.org/simple --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.8.0 + # uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.8.0 albatross==1.0.0 ----- stderr -----