From d355edb0143a6f196e13d6eedc3b01f8784f48a0 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 7 Dec 2024 14:31:23 -0500 Subject: [PATCH] Normalize relative paths when --project is specified --- crates/uv-fs/src/path.rs | 13 ++- crates/uv/src/lib.rs | 2 + crates/uv/tests/it/lock.rs | 205 +++++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+), 5 deletions(-) diff --git a/crates/uv-fs/src/path.rs b/crates/uv-fs/src/path.rs index 11626f5c4afb..7ac6de59c556 100644 --- a/crates/uv-fs/src/path.rs +++ b/crates/uv-fs/src/path.rs @@ -272,13 +272,16 @@ pub fn relative_to( path: impl AsRef, base: impl AsRef, ) -> Result { + // Normalize both paths, to avoid intermediate `..` components. + let path = normalize_path(path.as_ref()); + let base = normalize_path(base.as_ref()); + // Find the longest common prefix, and also return the path stripped from that prefix let (stripped, common_prefix) = base - .as_ref() .ancestors() .find_map(|ancestor| { // Simplifying removes the UNC path prefix on windows. - dunce::simplified(path.as_ref()) + dunce::simplified(&path) .strip_prefix(dunce::simplified(ancestor)) .ok() .map(|stripped| (stripped, ancestor)) @@ -288,14 +291,14 @@ pub fn relative_to( std::io::ErrorKind::Other, format!( "Trivial strip failed: {} vs. {}", - path.as_ref().simplified_display(), - base.as_ref().simplified_display() + path.simplified_display(), + base.simplified_display() ), ) })?; // go as many levels up as required - let levels_up = base.as_ref().components().count() - common_prefix.components().count(); + let levels_up = base.components().count() - common_prefix.components().count(); let up = std::iter::repeat("..").take(levels_up).collect::(); Ok(up.join(stripped)) diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index 03fa5b823d86..025105040f6f 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -65,6 +65,8 @@ async fn run(mut cli: Cli) -> Result { .as_deref() .map(std::path::absolute) .transpose()? + .as_deref() + .map(uv_fs::normalize_path) .map(Cow::Owned) .unwrap_or_else(|| Cow::Borrowed(&*CWD)); diff --git a/crates/uv/tests/it/lock.rs b/crates/uv/tests/it/lock.rs index 67bcdc03741d..2c266c995bd9 100644 --- a/crates/uv/tests/it/lock.rs +++ b/crates/uv/tests/it/lock.rs @@ -6347,6 +6347,110 @@ fn lock_no_workspace_source() -> Result<()> { Ok(()) } +/// Lock a workspace with a member that's a peer to the root. +#[test] +fn lock_peer_member() -> Result<()> { + let context = TestContext::new("3.12"); + + context + .temp_dir + .child("project") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["child"] + + [tool.uv.workspace] + members = ["../child"] + + [tool.uv.sources] + child = { workspace = true } + "#, + )?; + + context + .temp_dir + .child("child") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "child" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = [] + + [build-system] + requires = ["setuptools>=42"] + build-backend = "setuptools.build_meta" + "#, + )?; + + uv_snapshot!(context.filters(), context.lock().current_dir(context.temp_dir.child("project")), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Using CPython 3.12.[X] interpreter at: [PYTHON-3.12] + Resolved 2 packages in [TIME] + "###); + + let lock = fs_err::read_to_string(context.temp_dir.child("project").child("uv.lock")).unwrap(); + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + lock, @r###" + version = 1 + requires-python = ">=3.12" + + [options] + exclude-newer = "2024-03-25T00:00:00Z" + + [manifest] + members = [ + "child", + "project", + ] + + [[package]] + name = "child" + version = "0.1.0" + source = { editable = "../child" } + + [[package]] + name = "project" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "child" }, + ] + + [package.metadata] + requires-dist = [{ name = "child", editable = "../child" }] + "### + ); + }); + + // Re-run with `--locked`. + uv_snapshot!(context.filters(), context.lock().arg("--locked"), @r###" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: No `pyproject.toml` found in current directory or any parent directory + "###); + + Ok(()) +} + /// Ensure that development dependencies are omitted for non-workspace members. Below, `bar` depends /// on `foo`, but `bar/uv.lock` should omit `anyio`, but should include `typing-extensions`. #[test] @@ -18886,6 +18990,107 @@ fn mismatched_name_self_editable() -> Result<()> { Ok(()) } +#[test] +fn lock_relative_project() -> Result<()> { + let context = TestContext::new("3.12"); + + context + .temp_dir + .child("project") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["typing-extensions"] + "#, + )?; + + let peer = context.temp_dir.child("peer"); + fs_err::create_dir_all(&peer)?; + + uv_snapshot!(context.filters(), context.lock().arg("--project").arg("../project").current_dir(&peer), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Using CPython 3.12.[X] interpreter at: [PYTHON-3.12] + Resolved 2 packages in [TIME] + "###); + + let lock = context.read(context.temp_dir.child("project").child("uv.lock")); + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + lock, @r###" + version = 1 + requires-python = ">=3.12" + + [options] + exclude-newer = "2024-03-25T00:00:00Z" + + [[package]] + name = "project" + version = "0.1.0" + source = { virtual = "." } + dependencies = [ + { name = "typing-extensions" }, + ] + + [package.metadata] + requires-dist = [{ name = "typing-extensions" }] + + [[package]] + name = "typing-extensions" + version = "4.10.0" + source = { registry = "https://pypi.org/simple" } + sdist = { url = "https://files.pythonhosted.org/packages/16/3a/0d26ce356c7465a19c9ea8814b960f8a36c3b0d07c323176620b7b483e44/typing_extensions-4.10.0.tar.gz", hash = "sha256:b0abd7c89e8fb96f98db18d86106ff1d90ab692004eb746cf6eda2682f91b3cb", size = 77558 } + wheels = [ + { url = "https://files.pythonhosted.org/packages/f9/de/dc04a3ea60b22624b51c703a84bbe0184abcd1d0b9bc8074b5d6b7ab90bb/typing_extensions-4.10.0-py3-none-any.whl", hash = "sha256:69b1a937c3a517342112fb4c6df7e72fc39a38e7891a5730ed4985b5214b5475", size = 33926 }, + ] + "### + ); + }); + + // Re-run with `--locked`. + uv_snapshot!(context.filters(), context.lock().arg("--locked").arg("--project").arg("../project").current_dir(&peer), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Using CPython 3.12.[X] interpreter at: [PYTHON-3.12] + Resolved 2 packages in [TIME] + "###); + + // Create a virtual environment in the project directory. + context + .command() + .arg("venv") + .current_dir(context.temp_dir.child("project")) + .assert() + .success(); + + // Install from the lockfile. + uv_snapshot!(context.filters(), context.sync().arg("--frozen").arg("--project").arg("../project").current_dir(&peer), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Prepared 1 package in [TIME] + Installed 1 package in [TIME] + + typing-extensions==4.10.0 + "###); + + Ok(()) +} + #[test] fn lock_recursive_extra() -> Result<()> { let context = TestContext::new("3.12");