Skip to content

Commit 9f79821

Browse files
committed
Pin named indexes
1 parent b27fe75 commit 9f79821

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

crates/uv-workspace/src/pyproject.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ impl Source {
544544
source: RequirementSource,
545545
workspace: bool,
546546
editable: Option<bool>,
547+
index: Option<String>,
547548
rev: Option<String>,
548549
tag: Option<String>,
549550
branch: Option<String>,
@@ -581,7 +582,16 @@ impl Source {
581582
}
582583

583584
let source = match source {
584-
RequirementSource::Registry { .. } => return Ok(None),
585+
RequirementSource::Registry { index: Some(_), .. } => {
586+
return Ok(None);
587+
}
588+
RequirementSource::Registry { index: None, .. } => {
589+
if let Some(index) = index {
590+
Source::Registry { index }
591+
} else {
592+
return Ok(None);
593+
}
594+
}
585595
RequirementSource::Path { install_path, .. }
586596
| RequirementSource::Directory { install_path, .. } => Source::Path {
587597
editable,

crates/uv/src/commands/project/add.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ pub(crate) async fn add(
346346
requirements
347347
};
348348

349+
// If the user provides a single, named index, pin all requirements to that index.
350+
let index = indexes
351+
.first()
352+
.as_ref()
353+
.and_then(|index| index.name.as_ref())
354+
.filter(|_| indexes.len() == 1);
355+
349356
// Add the requirements to the `pyproject.toml` or script.
350357
let mut toml = match &target {
351358
Target::Script(script, _) => {
@@ -374,6 +381,7 @@ pub(crate) async fn add(
374381
requirement,
375382
false,
376383
editable,
384+
index.cloned(),
377385
rev.clone(),
378386
tag.clone(),
379387
branch.clone(),
@@ -389,6 +397,7 @@ pub(crate) async fn add(
389397
requirement,
390398
workspace,
391399
editable,
400+
index.cloned(),
392401
rev.clone(),
393402
tag.clone(),
394403
branch.clone(),
@@ -655,7 +664,11 @@ async fn lock_and_sync(
655664
};
656665

657666
// Only set a minimum version for registry requirements.
658-
if edit.source.is_some() {
667+
if edit
668+
.source
669+
.as_ref()
670+
.is_some_and(|source| !matches!(source, Source::Registry { .. }))
671+
{
659672
continue;
660673
}
661674

@@ -846,6 +859,7 @@ fn resolve_requirement(
846859
requirement: pypi_types::Requirement,
847860
workspace: bool,
848861
editable: Option<bool>,
862+
index: Option<String>,
849863
rev: Option<String>,
850864
tag: Option<String>,
851865
branch: Option<String>,
@@ -856,6 +870,7 @@ fn resolve_requirement(
856870
requirement.source.clone(),
857871
workspace,
858872
editable,
873+
index,
859874
rev,
860875
tag,
861876
branch,

crates/uv/tests/edit.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5067,7 +5067,7 @@ fn add_no_warn_index_url() -> Result<()> {
50675067

50685068
/// Add an index provided via `--index`.
50695069
#[test]
5070-
fn add_index_url() -> Result<()> {
5070+
fn add_index() -> Result<()> {
50715071
let context = TestContext::new("3.12");
50725072

50735073
let pyproject_toml = context.temp_dir.child("pyproject.toml");
@@ -5152,6 +5152,7 @@ fn add_index_url() -> Result<()> {
51525152
----- stdout -----
51535153
51545154
----- stderr -----
5155+
warning: Missing version constraint (e.g., a lower bound) for `jinja2`
51555156
Resolved 4 packages in [TIME]
51565157
Prepared 2 packages in [TIME]
51575158
Installed 2 packages in [TIME]
@@ -5181,6 +5182,9 @@ fn add_index_url() -> Result<()> {
51815182
51825183
[[tool.uv.index]]
51835184
url = "https://pypi.org/simple"
5185+
5186+
[tool.uv.sources]
5187+
jinja2 = { index = "pytorch" }
51845188
"###
51855189
);
51865190
});
@@ -5240,7 +5244,7 @@ fn add_index_url() -> Result<()> {
52405244
[package.metadata]
52415245
requires-dist = [
52425246
{ name = "iniconfig", specifier = "==2.0.0" },
5243-
{ name = "jinja2", specifier = ">=3.1.3" },
5247+
{ name = "jinja2", specifier = ">=3.1.3", index = "https://download.pytorch.org/whl/cu121" },
52445248
]
52455249
"###
52465250
);
@@ -5279,6 +5283,9 @@ fn add_index_url() -> Result<()> {
52795283
52805284
[[tool.uv.index]]
52815285
url = "https://pypi.org/simple"
5286+
5287+
[tool.uv.sources]
5288+
jinja2 = { index = "pytorch" }
52825289
"###
52835290
);
52845291
});
@@ -5344,7 +5351,7 @@ fn add_index_url() -> Result<()> {
53445351
[package.metadata]
53455352
requires-dist = [
53465353
{ name = "iniconfig", specifier = "==2.0.0" },
5347-
{ name = "jinja2", specifier = ">=3.1.3" },
5354+
{ name = "jinja2", specifier = ">=3.1.3", index = "https://test.pypi.org/simple" },
53485355
]
53495356
"###
53505357
);

0 commit comments

Comments
 (0)