Skip to content
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
19 changes: 16 additions & 3 deletions crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use uv_pypi_types::{
Conflicts, DependencyGroups, SchemaConflicts, SupportedEnvironments, VerbatimParsedUrl,
};
use uv_redacted::DisplaySafeUrl;
use uv_warnings::warn_user_once;

#[derive(Error, Debug)]
pub enum PyprojectTomlError {
Expand Down Expand Up @@ -276,17 +277,19 @@ pub struct Tool {
pub uv: Option<ToolUv>,
}

/// Validates that index names in the `tool.uv.index` field are unique.
/// Validates the `tool.uv.index` field.
///
/// This custom deserializer function checks for duplicate index names
/// and returns an error if any duplicates are found.
/// This custom deserializer function checks for:
/// - Duplicate index names
/// - Multiple indexes marked as default
fn deserialize_index_vec<'de, D>(deserializer: D) -> Result<Option<Vec<Index>>, D::Error>
where
D: Deserializer<'de>,
{
let indexes = Option::<Vec<Index>>::deserialize(deserializer)?;
if let Some(indexes) = indexes.as_ref() {
let mut seen_names = FxHashSet::with_capacity_and_hasher(indexes.len(), FxBuildHasher);
let mut seen_default = false;
for index in indexes {
if let Some(name) = index.name.as_ref() {
if !seen_names.insert(name) {
Expand All @@ -295,6 +298,16 @@ where
)));
}
}
if index.default {
if seen_default {
warn_user_once!(
"Found multiple indexes with `default = true`; \
only one index may be marked as default. \
This will become an error in the future.",
);
}
seen_default = true;
}
}
}
Ok(indexes)
Expand Down
41 changes: 41 additions & 0 deletions crates/uv/tests/it/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19250,6 +19250,45 @@ fn lock_repeat_named_index() -> Result<()> {
Ok(())
}

/// If multiple indexes are marked as default within a single file, we should raise an error.
#[test]
fn lock_multiple_default_indexes() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["iniconfig"]

[[tool.uv.index]]
name = "first"
url = "https://pypi.org/simple"
default = true

[[tool.uv.index]]
name = "second"
url = "https://example.com"
default = true
"#,
)?;

uv_snapshot!(context.filters(), context.lock(), @"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
warning: Found multiple indexes with `default = true`; only one index may be marked as default. This will become an error in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
warning: Found multiple indexes with `default = true`; only one index may be marked as default. This will become an error in the future.
warning: Found multiple indexes with `default = true`; only one index may be marked as default. This will become an error in a future release.

(just for consistency with some other warnings)

Resolved 2 packages in [TIME]
");

Ok(())
}

/// If a name is defined in both the workspace root and the member, prefer the index in the member.
#[test]
fn lock_repeat_named_index_member() -> Result<()> {
Expand Down Expand Up @@ -33274,6 +33313,7 @@ fn lock_check_multiple_default_indexes_explicit_assignment_dependency_group() ->
----- stdout -----

----- stderr -----
warning: Found multiple indexes with `default = true`; only one index may be marked as default. This will become an error in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have coverage for the case where there are multiple default indexes in a workspace defined in the root and a member? What do we expect there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently just don't load indexes from workspace members if we're in a workspace. The only time we look at the indexes is when they're referenced as a source. So basically all indexes in a member implicitly act like they have "explicit" set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is in here:

uv/crates/uv/src/lib.rs

Lines 144 to 178 in b73f6cb

// Load configuration from the filesystem, prioritizing (in order):
// 1. The configuration file specified on the command-line.
// 2. The nearest configuration file (`uv.toml` or `pyproject.toml`) above the workspace root.
// If found, this file is combined with the user configuration file.
// 3. The nearest configuration file (`uv.toml` or `pyproject.toml`) in the directory tree,
// starting from the current directory.
let workspace_cache = WorkspaceCache::default();
let filesystem = if let Some(config_file) = cli.top_level.config_file.as_ref() {
if config_file
.file_name()
.is_some_and(|file_name| file_name == "pyproject.toml")
{
warn_user!(
"The `--config-file` argument expects to receive a `uv.toml` file, not a `pyproject.toml`. If you're trying to run a command from another project, use the `--project` argument instead."
);
}
Some(FilesystemOptions::from_file(config_file)?)
} else if deprecated_isolated || cli.top_level.no_config {
None
} else if matches!(&*cli.command, Commands::Tool(_) | Commands::Self_(_)) {
// For commands that operate at the user-level, ignore local configuration.
FilesystemOptions::user()?.combine(FilesystemOptions::system()?)
} else if let Ok(workspace) =
Workspace::discover(&project_dir, &DiscoveryOptions::default(), &workspace_cache).await
{
let project = FilesystemOptions::find(workspace.install_path())?;
let system = FilesystemOptions::system()?;
let user = FilesystemOptions::user()?;
project.combine(user).combine(system)
} else {
let project = FilesystemOptions::find(&project_dir)?;
let system = FilesystemOptions::system()?;
let user = FilesystemOptions::user()?;
project.combine(user).combine(system)
};

Resolved 2 packages in [TIME]
");

Expand Down Expand Up @@ -33324,6 +33364,7 @@ fn lock_check_multiple_default_indexes_explicit_assignment_dependency_group() ->
----- stdout -----

----- stderr -----
warning: Found multiple indexes with `default = true`; only one index may be marked as default. This will become an error in the future.
Resolved 2 packages in [TIME]
");

Expand Down
Loading