Skip to content
Open
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
13 changes: 7 additions & 6 deletions crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tracing::instrument;
use uv_build_backend::BuildBackendSettings;
use uv_configuration::GitLfsSetting;
use uv_distribution_types::{Index, IndexName, RequirementSource};
use uv_fs::{PortablePathBuf, relative_to};
use uv_fs::{PortablePathBuf, try_relative_to_if};
use uv_git_types::GitReference;
use uv_macros::OptionsMetadata;
use uv_normalize::{DefaultGroups, ExtraName, GroupName, PackageName};
Expand Down Expand Up @@ -1724,12 +1724,13 @@ impl Source {
return Ok(None);
}
}
RequirementSource::Path { install_path, .. } => Self::Path {
RequirementSource::Path {
install_path, url, ..
} => Self::Path {
editable: None,
package: None,
path: PortablePathBuf::from(
relative_to(&install_path, root)
.or_else(|_| std::path::absolute(&install_path))
try_relative_to_if(&install_path, root, !url.was_given_absolute())
.map_err(SourceError::Absolute)?
.into_boxed_path(),
),
Expand All @@ -1740,13 +1741,13 @@ impl Source {
RequirementSource::Directory {
install_path,
editable: is_editable,
url,
..
} => Self::Path {
editable: editable.or(is_editable),
package: None,
path: PortablePathBuf::from(
relative_to(&install_path, root)
.or_else(|_| std::path::absolute(&install_path))
try_relative_to_if(&install_path, root, !url.was_given_absolute())
.map_err(SourceError::Absolute)?
.into_boxed_path(),
),
Expand Down
28 changes: 16 additions & 12 deletions crates/uv/tests/it/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3064,7 +3064,7 @@ fn add_path_adjacent_directory() -> Result<()> {
]

[tool.uv.sources]
dependency = { path = "../dependency" }
dependency = { path = "[TEMP_DIR]/dependency" }
"#
);
});
Expand All @@ -3087,7 +3087,7 @@ fn add_path_adjacent_directory() -> Result<()> {
[[package]]
name = "dependency"
version = "0.1.0"
source = { directory = "../dependency" }
source = { directory = "[TEMP_DIR]/dependency" }

[[package]]
name = "project"
Expand All @@ -3098,7 +3098,7 @@ fn add_path_adjacent_directory() -> Result<()> {
]

[package.metadata]
requires-dist = [{ name = "dependency", directory = "../dependency" }]
requires-dist = [{ name = "dependency", directory = "[TEMP_DIR]/dependency" }]
"#
);
});
Expand All @@ -3108,7 +3108,10 @@ fn add_path_adjacent_directory() -> Result<()> {

/// Check relative and absolute path handling with `uv add`.
///
/// BUG: Currently `uv add` always relativizes paths in `pyproject.toml`
/// When a user provides an absolute path or `file://` URL, it should be preserved as absolute
/// in pyproject.toml and uv.lock. Relative paths should remain relative.
///
/// See: <https://github.com/astral-sh/uv/issues/17307>
#[test]
fn add_relative_and_absolute_paths() -> Result<()> {
let context = uv_test::test_context!("3.12");
Expand Down Expand Up @@ -3221,7 +3224,8 @@ fn add_relative_and_absolute_paths() -> Result<()> {
+ file-url-dep==0.1.0 (from file://[TEMP_DIR]/file_url_dep)
");

// Check pyproject.toml.
// Check pyproject.toml - relative paths stay relative, absolute paths and file:// URLs
// stay absolute.
let pyproject_toml = fs_err::read_to_string(project.join("pyproject.toml"))?;

insta::with_settings!({
Expand All @@ -3241,13 +3245,13 @@ fn add_relative_and_absolute_paths() -> Result<()> {

[tool.uv.sources]
relative-dep = { path = "../relative_dep" }
absolute-dep = { path = "../absolute_dep" }
file-url-dep = { path = "../file_url_dep" }
absolute-dep = { path = "[TEMP_DIR]/absolute_dep" }
file-url-dep = { path = "[TEMP_DIR]/file_url_dep" }
"#
);
});

// Check uv.lock.
// Check uv.lock - relative paths stay relative, absolute paths stay absolute.
let lock = fs_err::read_to_string(project.join("uv.lock"))?;

insta::with_settings!({
Expand All @@ -3265,12 +3269,12 @@ fn add_relative_and_absolute_paths() -> Result<()> {
[[package]]
name = "absolute-dep"
version = "0.1.0"
source = { directory = "../absolute_dep" }
source = { directory = "[TEMP_DIR]/absolute_dep" }

[[package]]
name = "file-url-dep"
version = "0.1.0"
source = { directory = "../file_url_dep" }
source = { directory = "[TEMP_DIR]/file_url_dep" }

[[package]]
name = "project"
Expand All @@ -3284,8 +3288,8 @@ fn add_relative_and_absolute_paths() -> Result<()> {

[package.metadata]
requires-dist = [
{ name = "absolute-dep", directory = "../absolute_dep" },
{ name = "file-url-dep", directory = "../file_url_dep" },
{ name = "absolute-dep", directory = "[TEMP_DIR]/absolute_dep" },
{ name = "file-url-dep", directory = "[TEMP_DIR]/file_url_dep" },
{ name = "relative-dep", directory = "../relative_dep" },
]

Expand Down
Loading