Skip to content

Commit

Permalink
Avoid validating workspace members when --no-sources is provided (#…
Browse files Browse the repository at this point in the history
…7599)

## Summary

Closes #7572.
  • Loading branch information
charliermarsh authored Sep 20, 2024
1 parent d6c9603 commit 445d1c0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
67 changes: 38 additions & 29 deletions crates/uv-distribution/src/metadata/requires_dist.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::BTreeMap;
use std::path::Path;

use crate::metadata::{LoweredRequirement, MetadataError};
use crate::Metadata;
use pypi_types::Requirement;
use std::collections::BTreeMap;
use std::path::Path;
use uv_configuration::SourceStrategy;
use uv_normalize::{ExtraName, GroupName, PackageName, DEV_DEPENDENCIES};
use uv_workspace::pyproject::ToolUvSources;
Expand Down Expand Up @@ -54,11 +54,11 @@ impl RequiresDist {
fn from_project_workspace(
metadata: pypi_types::RequiresDist,
project_workspace: &ProjectWorkspace,
sources: SourceStrategy,
source_strategy: SourceStrategy,
) -> Result<Self, MetadataError> {
// Collect any `tool.uv.sources` and `tool.uv.dev_dependencies` from `pyproject.toml`.
let empty = BTreeMap::default();
let sources = match sources {
let sources = match source_strategy {
SourceStrategy::Enabled => project_workspace
.current_project()
.pyproject_toml()
Expand All @@ -81,7 +81,37 @@ impl RequiresDist {
.and_then(|uv| uv.dev_dependencies.as_ref())
.into_iter()
.flatten()
.cloned()
.cloned();
let dev_dependencies = match source_strategy {
SourceStrategy::Enabled => dev_dependencies
.map(|requirement| {
let requirement_name = requirement.name.clone();
LoweredRequirement::from_requirement(
requirement,
&metadata.name,
project_workspace.project_root(),
sources,
project_workspace.workspace(),
)
.map(LoweredRequirement::into_inner)
.map_err(|err| MetadataError::LoweringError(requirement_name.clone(), err))
})
.collect::<Result<Vec<_>, _>>()?,
SourceStrategy::Disabled => dev_dependencies
.into_iter()
.map(Requirement::from)
.collect(),
};
if dev_dependencies.is_empty() {
BTreeMap::default()
} else {
BTreeMap::from([(DEV_DEPENDENCIES.clone(), dev_dependencies)])
}
};

let requires_dist = metadata.requires_dist.into_iter();
let requires_dist = match source_strategy {
SourceStrategy::Enabled => requires_dist
.map(|requirement| {
let requirement_name = requirement.name.clone();
LoweredRequirement::from_requirement(
Expand All @@ -94,31 +124,10 @@ impl RequiresDist {
.map(LoweredRequirement::into_inner)
.map_err(|err| MetadataError::LoweringError(requirement_name.clone(), err))
})
.collect::<Result<Vec<_>, _>>()?;
if dev_dependencies.is_empty() {
BTreeMap::default()
} else {
BTreeMap::from([(DEV_DEPENDENCIES.clone(), dev_dependencies)])
}
.collect::<Result<_, _>>()?,
SourceStrategy::Disabled => requires_dist.into_iter().map(Requirement::from).collect(),
};

let requires_dist = metadata
.requires_dist
.into_iter()
.map(|requirement| {
let requirement_name = requirement.name.clone();
LoweredRequirement::from_requirement(
requirement,
&metadata.name,
project_workspace.project_root(),
sources,
project_workspace.workspace(),
)
.map(LoweredRequirement::into_inner)
.map_err(|err| MetadataError::LoweringError(requirement_name.clone(), err))
})
.collect::<Result<_, _>>()?;

Ok(Self {
name: metadata.name,
requires_dist,
Expand Down
51 changes: 51 additions & 0 deletions crates/uv/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2752,3 +2752,54 @@ fn sync_invalid_environment() -> Result<()> {

Ok(())
}

/// Avoid validating workspace members when `--no-sources` is provided. Rather than reporting that
/// `./anyio` is missing, install `anyio` from the registry.
#[test]
fn sync_no_sources_missing_member() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "root"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio"]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
[tool.uv.sources]
anyio = { workspace = true }
[tool.uv.workspace]
members = ["anyio"]
"#,
)?;

let src = context.temp_dir.child("src").child("albatross");
src.create_dir_all()?;

let init = src.child("__init__.py");
init.touch()?;

uv_snapshot!(context.filters(), context.sync().arg("--no-sources"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 4 packages in [TIME]
Prepared 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==4.3.0
+ idna==3.6
+ root==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

Ok(())
}

0 comments on commit 445d1c0

Please sign in to comment.