Skip to content

Commit

Permalink
Allow workspace members from sibling directories
Browse files Browse the repository at this point in the history
  • Loading branch information
idlsoft committed Jun 25, 2024
1 parent 1984ada commit 6da8f64
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 7 deletions.
17 changes: 11 additions & 6 deletions crates/uv-distribution/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::{debug, trace};

use pep508_rs::VerbatimUrl;
use pypi_types::{Requirement, RequirementSource};
use uv_fs::{absolutize_path, Simplified};
use uv_fs::{absolutize_path, relative_to, Simplified};
use uv_normalize::PackageName;
use uv_warnings::warn_user;

Expand Down Expand Up @@ -178,11 +178,16 @@ impl Workspace {
marker: None,
source: RequirementSource::Directory {
install_path: member.root.clone(),
lock_path: member
.root
.strip_prefix(&self.root)
.expect("Project must be below workspace root")
.to_path_buf(),
lock_path: relative_to(&member.root, &self.root)
.map_err(|e| {
format!(
"Cannot get relative path to {} from {}: {}",
&member.root.display(),
&self.root.display(),
e
)
})
.unwrap(),
editable: true,
url,
},
Expand Down
33 changes: 32 additions & 1 deletion crates/uv/tests/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;
use std::process::{Command, Stdio};

use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
Expand Down Expand Up @@ -564,3 +564,34 @@ fn workspace_lock_idempotence_virtual_workspace() -> Result<()> {
)?;
Ok(())
}

#[test]
fn test_sibling_member() {
let context = TestContext::new("3.12");
let work_dir = context.temp_dir.join("shared-lib-ws");
copy_dir_ignore(workspaces_dir().join("shared-lib"), &work_dir)
.expect("Could not copy to temp dir");

for (app_name, numpy_version) in [("app1", "2.0.0b1"), ("app2", "1.26.4")] {
let app_dir = work_dir.join(app_name);

let mut filters = context.filters();

filters.push((app_name, "[app_name]"));
filters.push((numpy_version, "[numpy_version]"));

insta::allow_duplicates! {
uv_snapshot!(filters, run_workspace(&context)
.arg(format!("{app_name}/app.py")).stderr(Stdio::null())
.current_dir(&app_dir), @r###"
success: true
exit_code: 0
----- stdout -----
shared-lib is loaded from [TEMP_DIR]/shared-lib-ws/shared_lib/shared_lib/__init__.py
numpy [numpy_version]
----- stderr -----
"###);
}
}
}
9 changes: 9 additions & 0 deletions scripts/workspaces/shared-lib/app1/app1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from shared_lib import hello_shared
import numpy as np

def main():
hello_shared()
print(f"numpy {np.__version__}")

if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions scripts/workspaces/shared-lib/app1/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[project]
name = "app1"
version = "1.0.0"
requires-python = ">=3.12"
dependencies=[
"shared_lib",
"numpy == 2.0.0b1",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["app1"]

[tool.uv.workspace]
members = ["../shared_lib"]

[tool.uv.sources]
shared_lib = { workspace = true }
9 changes: 9 additions & 0 deletions scripts/workspaces/shared-lib/app2/app2/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from shared_lib import hello_shared
import numpy as np

def main():
hello_shared()
print(f"numpy {np.__version__}")

if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions scripts/workspaces/shared-lib/app2/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[project]
name = "app2"
version = "1.0.0"
requires-python = ">=3.12"
dependencies=[
"shared_lib",
"numpy == 1.26.4",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["app1"]

[tool.uv.workspace]
members = ["../shared_lib"]

[tool.uv.sources]
shared_lib = { workspace = true }
15 changes: 15 additions & 0 deletions scripts/workspaces/shared-lib/shared_lib/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "shared_lib"
version = "1.0.0"
requires-python = ">=3.12"
dependencies=[
"six == 1.16.0",
]

[build-system]
requires = ["hatchling==1.24.2"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["shared_lib"]

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_shared():
print(f"shared-lib is loaded from {__file__}")

0 comments on commit 6da8f64

Please sign in to comment.