Skip to content

Commit

Permalink
Normalize relative paths when --project is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 7, 2024
1 parent 696b64f commit d355edb
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 5 deletions.
13 changes: 8 additions & 5 deletions crates/uv-fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,16 @@ pub fn relative_to(
path: impl AsRef<Path>,
base: impl AsRef<Path>,
) -> Result<PathBuf, std::io::Error> {
// 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))
Expand All @@ -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::<PathBuf>();

Ok(up.join(stripped))
Expand Down
2 changes: 2 additions & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
.as_deref()
.map(std::path::absolute)
.transpose()?
.as_deref()
.map(uv_fs::normalize_path)
.map(Cow::Owned)
.unwrap_or_else(|| Cow::Borrowed(&*CWD));

Expand Down
205 changes: 205 additions & 0 deletions crates/uv/tests/it/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit d355edb

Please sign in to comment.