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

Resolve relative tool.uv.sources relative to containing project #6045

Merged
merged 1 commit into from
Aug 12, 2024
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
32 changes: 25 additions & 7 deletions crates/uv-distribution/src/metadata/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ use uv_workspace::Workspace;
#[derive(Debug, Clone)]
pub struct LoweredRequirement(Requirement);

#[derive(Debug, Clone, Copy)]
enum Origin {
/// The `tool.uv.sources` were read from the project.
Project,
/// The `tool.uv.sources` were read from the workspace root.
Workspace,
}

impl LoweredRequirement {
/// Combine `project.dependencies` or `project.optional-dependencies` with `tool.uv.sources`.
pub(crate) fn from_requirement(
Expand All @@ -30,10 +38,14 @@ impl LoweredRequirement {
workspace: &Workspace,
preview: PreviewMode,
) -> Result<Self, LoweringError> {
let source = project_sources
.get(&requirement.name)
.or(workspace.sources().get(&requirement.name))
.cloned();
let (source, origin) = if let Some(source) = project_sources.get(&requirement.name) {
(Some(source), Origin::Project)
} else if let Some(source) = workspace.sources().get(&requirement.name) {
(Some(source), Origin::Workspace)
} else {
(None, Origin::Project)
};
let source = source.cloned();

let workspace_package_declared =
// We require that when you use a package that's part of the workspace, ...
Expand Down Expand Up @@ -97,6 +109,7 @@ impl LoweredRequirement {
}
path_source(
path,
origin,
project_dir,
workspace.install_path(),
editable.unwrap_or(false),
Expand Down Expand Up @@ -202,7 +215,7 @@ impl LoweredRequirement {
if matches!(requirement.version_or_url, Some(VersionOrUrl::Url(_))) {
return Err(LoweringError::ConflictingUrls);
}
path_source(path, dir, dir, editable.unwrap_or(false))?
path_source(path, Origin::Project, dir, dir, editable.unwrap_or(false))?
}
Source::Registry { index } => registry_source(&requirement, index)?,
Source::Workspace { .. } => {
Expand Down Expand Up @@ -355,15 +368,20 @@ fn registry_source(
/// Convert a path string to a file or directory source.
fn path_source(
path: impl AsRef<Path>,
origin: Origin,
project_dir: &Path,
workspace_root: &Path,
editable: bool,
) -> Result<RequirementSource, LoweringError> {
let path = path.as_ref();
let url = VerbatimUrl::parse_path(path, project_dir)?.with_given(path.to_string_lossy());
let base = match origin {
Origin::Project => project_dir,
Origin::Workspace => workspace_root,
};
let url = VerbatimUrl::parse_path(path, base)?.with_given(path.to_string_lossy());
let absolute_path = path
.to_path_buf()
.absolutize_from(project_dir)
.absolutize_from(base)
.map_err(|err| LoweringError::Absolutize(path.to_path_buf(), err))?
.to_path_buf();
let relative_to_workspace = if path.is_relative() {
Expand Down
198 changes: 197 additions & 1 deletion crates/uv/tests/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::process::Command;
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::fixture::{FileWriteStr, PathChild};
use assert_fs::prelude::FileTouch;
use indoc::indoc;
use insta::assert_json_snapshot;
use insta::{assert_json_snapshot, assert_snapshot};
use serde::{Deserialize, Serialize};

use crate::common::{copy_dir_ignore, make_project, uv_snapshot, TestContext};
Expand Down Expand Up @@ -955,3 +956,198 @@ fn workspace_hidden_member() -> Result<()> {

Ok(())
}

/// Ensure workspace members inherit sources from the root, if not specified in the member.
///
/// In such cases, relative paths should be resolved relative to the workspace root, rather than
/// relative to the member.
#[test]
fn workspace_inherit_sources() -> Result<()> {
let context = TestContext::new("3.12");

// Create the workspace root.
let workspace = context.temp_dir.child("workspace");
workspace.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "workspace"
version = "0.1.0"
dependencies = []
requires-python = ">=3.12"

[tool.uv.workspace]
members = ["packages/*"]
"#})?;
workspace.child("src/__init__.py").touch()?;

// Create a package.
let leaf = workspace.child("packages").child("leaf");
leaf.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "leaf"
version = "0.1.0"
dependencies = ["library"]
"#})?;
leaf.child("src/__init__.py").touch()?;

// Create a peripheral library.
let library = context.temp_dir.child("library");
library.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "library"
version = "0.1.0"
dependencies = []
"#})?;
library.child("src/__init__.py").touch()?;

// As-is, resolving should fail.
uv_snapshot!(context.filters(), context.lock().arg("--preview").arg("--offline").current_dir(&workspace), @r###"
success: false
exit_code: 1
----- stdout -----

----- stderr -----
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
× No solution found when resolving dependencies:
╰─▶ Because library was not found in the cache and leaf==0.1.0 depends on library, we can conclude that leaf==0.1.0 cannot be used.
And because only leaf==0.1.0 is available and you require leaf, we can conclude that the requirements are unsatisfiable.

hint: Packages were unavailable because the network was disabled
"###
);

// Update the leaf to include the source.
leaf.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "leaf"
version = "0.1.0"
dependencies = ["library"]

[tool.uv.sources]
library = { path = "../../../library", editable = true }
"#})?;
leaf.child("src/__init__.py").touch()?;

// Resolving should succeed.
uv_snapshot!(context.filters(), context.lock().arg("--preview").arg("--offline").current_dir(&workspace), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
Resolved 3 packages in [TIME]
"###
);

// Revert that change.
leaf.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "leaf"
version = "0.1.0"
dependencies = ["library"]
"#})?;

// Update the root to include the source.
workspace.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "workspace"
version = "0.1.0"
dependencies = []
requires-python = ">=3.12"

[tool.uv.sources]
library = { path = "../library", editable = true }

[tool.uv.workspace]
members = ["packages/*"]
"#})?;

// Resolving should succeed.
uv_snapshot!(context.filters(), context.lock().arg("--preview").arg("--offline").current_dir(&workspace), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
Resolved 3 packages in [TIME]
"###
);

let lock = fs_err::read_to_string(workspace.join("uv.lock")).unwrap();

// The lockfile should use a path relative to the workspace root.
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[options]
exclude-newer = "2024-03-25 00:00:00 UTC"

[[package]]
name = "leaf"
version = "0.1.0"
source = { editable = "packages/leaf" }
dependencies = [
{ name = "library" },
]

[[package]]
name = "library"
version = "0.1.0"
source = { editable = "../library" }

[[package]]
name = "workspace"
version = "0.1.0"
source = { editable = "." }
"###
);
});

// Update the root to include the source again.
workspace.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "workspace"
version = "0.1.0"
dependencies = []
requires-python = ">=3.12"

[tool.uv.sources]
library = { path = "../library", editable = true }

[tool.uv.workspace]
members = ["packages/*"]
"#})?;

// Update the member to include a _different_ source.
leaf.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "leaf"
version = "0.1.0"
dependencies = ["library"]

[tool.uv.sources]
application = { path = "../application", editable = true }

"#})?;

// Resolving should succeed; the member should still use the root's source, despite defining
// some of its own
uv_snapshot!(context.filters(), context.lock().arg("--preview").arg("--offline").current_dir(&workspace), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Using Python 3.12.[X] interpreter at: [PYTHON-3.12]
Resolved 3 packages in [TIME]
"###
);

Ok(())
}
Loading