mirror of https://github.com/astral-sh/uv
Split UV_INDEX on all whitespace (#12820)
## Summary Closes #12806 Split `UV_INDEX` by any whitespace rather than only ASCII 32, which does not align with the behavior of `PIP_EXTRA_INDEX_URL` and can possibly lead to difficulties when migrating from pip to uv. Clap unfortunately does not support passing multiple delimiters, writing a custom parsing function involved parsing index into a Vec<Vec<Index>> and flattening it afterwards in order to avoid breaking the --index command line option. There might be a prettier solution I overlooked, let me know if there is anything I should change!
This commit is contained in:
parent
ef68fca591
commit
73fd0aedd1
|
|
@ -906,20 +906,27 @@ fn parse_find_links(input: &str) -> Result<Maybe<PipFindLinks>, String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an `--index` argument into an [`Index`], mapping the empty string to `None`.
|
/// Parse an `--index` argument into a [`Vec<Index>`], mapping the empty string to an empty Vec.
|
||||||
fn parse_index(input: &str) -> Result<Maybe<Index>, String> {
|
///
|
||||||
if input.is_empty() {
|
/// This function splits the input on all whitespace characters rather than a single delimiter,
|
||||||
Ok(Maybe::None)
|
/// which is necessary to parse environment variables like `PIP_EXTRA_INDEX_URL`.
|
||||||
} else {
|
/// The standard `clap::Args` `value_delimiter` only supports single-character delimiters.
|
||||||
match Index::from_str(input) {
|
fn parse_indices(input: &str) -> Result<Vec<Maybe<Index>>, String> {
|
||||||
Ok(index) => Ok(Maybe::Some(Index {
|
if input.trim().is_empty() {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
let mut indices = Vec::new();
|
||||||
|
for token in input.split_whitespace() {
|
||||||
|
match Index::from_str(token) {
|
||||||
|
Ok(index) => indices.push(Maybe::Some(Index {
|
||||||
default: false,
|
default: false,
|
||||||
origin: Some(Origin::Cli),
|
origin: Some(Origin::Cli),
|
||||||
..index
|
..index
|
||||||
})),
|
})),
|
||||||
Err(err) => Err(err.to_string()),
|
Err(e) => return Err(e.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(indices)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a `--default-index` argument into an [`Index`], mapping the empty string to `None`.
|
/// Parse a `--default-index` argument into an [`Index`], mapping the empty string to `None`.
|
||||||
|
|
@ -4897,8 +4904,12 @@ pub struct IndexArgs {
|
||||||
/// All indexes provided via this flag take priority over the index specified by
|
/// All indexes provided via this flag take priority over the index specified by
|
||||||
/// `--default-index` (which defaults to PyPI). When multiple `--index` flags are provided,
|
/// `--default-index` (which defaults to PyPI). When multiple `--index` flags are provided,
|
||||||
/// earlier values take priority.
|
/// earlier values take priority.
|
||||||
#[arg(long, env = EnvVars::UV_INDEX, value_delimiter = ' ', value_parser = parse_index, help_heading = "Index options")]
|
//
|
||||||
pub index: Option<Vec<Maybe<Index>>>,
|
// The nested Vec structure (`Vec<Vec<Maybe<Index>>>`) is required for clap's
|
||||||
|
// value parsing mechanism, which processes one value at a time, in order to handle
|
||||||
|
// `UV_INDEX` the same way pip handles `PIP_EXTRA_INDEX_URL`.
|
||||||
|
#[arg(long, env = EnvVars::UV_INDEX, value_parser = parse_indices, help_heading = "Index options")]
|
||||||
|
pub index: Option<Vec<Vec<Maybe<Index>>>>,
|
||||||
|
|
||||||
/// The URL of the default package index (by default: <https://pypi.org/simple>).
|
/// The URL of the default package index (by default: <https://pypi.org/simple>).
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -199,9 +199,13 @@ impl From<IndexArgs> for PipOptions {
|
||||||
index: default_index
|
index: default_index
|
||||||
.and_then(Maybe::into_option)
|
.and_then(Maybe::into_option)
|
||||||
.map(|default_index| vec![default_index])
|
.map(|default_index| vec![default_index])
|
||||||
.combine(
|
.combine(index.map(|index| {
|
||||||
index.map(|index| index.into_iter().filter_map(Maybe::into_option).collect()),
|
index
|
||||||
),
|
.iter()
|
||||||
|
.flat_map(std::clone::Clone::clone)
|
||||||
|
.filter_map(Maybe::into_option)
|
||||||
|
.collect()
|
||||||
|
})),
|
||||||
index_url: index_url.and_then(Maybe::into_option),
|
index_url: index_url.and_then(Maybe::into_option),
|
||||||
extra_index_url: extra_index_url.map(|extra_index_urls| {
|
extra_index_url: extra_index_url.map(|extra_index_urls| {
|
||||||
extra_index_urls
|
extra_index_urls
|
||||||
|
|
@ -260,11 +264,13 @@ pub fn resolver_options(
|
||||||
.default_index
|
.default_index
|
||||||
.and_then(Maybe::into_option)
|
.and_then(Maybe::into_option)
|
||||||
.map(|default_index| vec![default_index])
|
.map(|default_index| vec![default_index])
|
||||||
.combine(
|
.combine(index_args.index.map(|index| {
|
||||||
index_args
|
index
|
||||||
.index
|
.into_iter()
|
||||||
.map(|index| index.into_iter().filter_map(Maybe::into_option).collect()),
|
.flat_map(|v| v.clone())
|
||||||
),
|
.filter_map(Maybe::into_option)
|
||||||
|
.collect()
|
||||||
|
})),
|
||||||
index_url: index_args.index_url.and_then(Maybe::into_option),
|
index_url: index_args.index_url.and_then(Maybe::into_option),
|
||||||
extra_index_url: index_args.extra_index_url.map(|extra_index_url| {
|
extra_index_url: index_args.extra_index_url.map(|extra_index_url| {
|
||||||
extra_index_url
|
extra_index_url
|
||||||
|
|
@ -352,9 +358,13 @@ pub fn resolver_installer_options(
|
||||||
.default_index
|
.default_index
|
||||||
.and_then(Maybe::into_option)
|
.and_then(Maybe::into_option)
|
||||||
.map(|default_index| vec![default_index]);
|
.map(|default_index| vec![default_index]);
|
||||||
let index = index_args
|
let index = index_args.index.map(|index| {
|
||||||
.index
|
index
|
||||||
.map(|index| index.into_iter().filter_map(Maybe::into_option).collect());
|
.into_iter()
|
||||||
|
.flat_map(|v| v.clone())
|
||||||
|
.filter_map(Maybe::into_option)
|
||||||
|
.collect()
|
||||||
|
});
|
||||||
|
|
||||||
ResolverInstallerOptions {
|
ResolverInstallerOptions {
|
||||||
index: default_index.combine(index),
|
index: default_index.combine(index),
|
||||||
|
|
|
||||||
|
|
@ -1279,6 +1279,7 @@ impl AddSettings {
|
||||||
.index
|
.index
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.flat_map(|v| v.clone())
|
||||||
.flatten()
|
.flatten()
|
||||||
.filter_map(Maybe::into_option),
|
.filter_map(Maybe::into_option),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -9945,6 +9945,112 @@ fn repeated_index_cli_environment_variable() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If an index is repeated on the CLI, the last-provided index should take precedence.
|
||||||
|
/// Newlines in `UV_INDEX` should be treated as separators.
|
||||||
|
///
|
||||||
|
/// The index that appears in the `pyproject.toml` should also be consistent with the index that
|
||||||
|
/// appears in the `uv.lock`.
|
||||||
|
///
|
||||||
|
/// See: <https://github.com/astral-sh/uv/issues/11312>
|
||||||
|
#[test]
|
||||||
|
fn repeated_index_cli_environment_variable_newline() -> 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 = []
|
||||||
|
"#})?;
|
||||||
|
|
||||||
|
uv_snapshot!(context.filters(), context
|
||||||
|
.add()
|
||||||
|
.env(EnvVars::UV_INDEX, "https://test.pypi.org/simple\nhttps://test.pypi.org/simple/")
|
||||||
|
.arg("iniconfig"), @r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
Resolved 2 packages in [TIME]
|
||||||
|
Prepared 1 package in [TIME]
|
||||||
|
Installed 1 package in [TIME]
|
||||||
|
+ iniconfig==2.0.0
|
||||||
|
"###);
|
||||||
|
|
||||||
|
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 = [
|
||||||
|
"iniconfig>=2.0.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[tool.uv.index]]
|
||||||
|
url = "https://test.pypi.org/simple"
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
let lock = context.read("uv.lock");
|
||||||
|
|
||||||
|
insta::with_settings!({
|
||||||
|
filters => context.filters(),
|
||||||
|
}, {
|
||||||
|
assert_snapshot!(
|
||||||
|
lock, @r#"
|
||||||
|
version = 1
|
||||||
|
revision = 1
|
||||||
|
requires-python = ">=3.12"
|
||||||
|
|
||||||
|
[options]
|
||||||
|
exclude-newer = "2024-03-25T00:00:00Z"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iniconfig"
|
||||||
|
version = "2.0.0"
|
||||||
|
source = { registry = "https://test.pypi.org/simple" }
|
||||||
|
sdist = { url = "https://test-files.pythonhosted.org/packages/d7/4b/cbd8e699e64a6f16ca3a8220661b5f83792b3017d0f79807cb8708d33913/iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3", size = 4646 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://test-files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 },
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "project"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = { virtual = "." }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "iniconfig" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.metadata]
|
||||||
|
requires-dist = [{ name = "iniconfig", specifier = ">=2.0.0" }]
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Install from the lockfile.
|
||||||
|
uv_snapshot!(context.filters(), context.sync().arg("--frozen"), @r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
Audited 1 package in [TIME]
|
||||||
|
"###);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// If an index is repeated on the CLI, the last-provided index should take precedence.
|
/// If an index is repeated on the CLI, the last-provided index should take precedence.
|
||||||
///
|
///
|
||||||
/// The index that appears in the `pyproject.toml` should also be consistent with the index that
|
/// The index that appears in the `pyproject.toml` should also be consistent with the index that
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue