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

Hide index URLs from header if not emitted #1835

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 32 additions & 9 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)?;
}

Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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 -----
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/pip_compile_scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
DrJackilD marked this conversation as resolved.
Show resolved Hide resolved
albatross==1.0.0

----- stderr -----
Expand Down Expand Up @@ -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 -----
Expand Down Expand Up @@ -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 -----
Expand Down