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

Support relative paths in uv add --script #7301

Merged
merged 1 commit into from
Sep 11, 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
22 changes: 13 additions & 9 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,19 @@ pub(crate) async fn add(
Target::Script(_, _) | Target::Project(_, _) if raw_sources => {
(pep508_rs::Requirement::from(requirement), None)
}
Target::Script(ref script, _) => resolve_requirement(
requirement,
false,
editable,
rev.clone(),
tag.clone(),
branch.clone(),
&script.path,
)?,
Target::Script(ref script, _) => {
let script_path = std::path::absolute(&script.path)?;
let script_dir = script_path.parent().expect("script path has no parent");
resolve_requirement(
requirement,
false,
editable,
rev.clone(),
tag.clone(),
branch.clone(),
script_dir,
)?
}
Target::Project(ref project, _) => {
let workspace = project
.workspace()
Expand Down
3 changes: 2 additions & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ pub(crate) async fn run(
.and_then(|tool| tool.uv.as_ref())
.and_then(|uv| uv.sources.as_ref())
.unwrap_or(&empty);
let script_dir = script.path.parent().expect("script path has no parent");
let script_path = std::path::absolute(script.path)?;
let script_dir = script_path.parent().expect("script path has no parent");

let requirements = dependencies
.into_iter()
Expand Down
54 changes: 54 additions & 0 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,60 @@ fn add_script() -> Result<()> {
Ok(())
}

#[test]
fn add_script_relative_path() -> Result<()> {
let context = TestContext::new("3.12");

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

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

let script = context.temp_dir.child("script.py");
script.write_str(indoc! {r#"
print("Hello, world!")
"#})?;

uv_snapshot!(context.filters(), context.add().arg("./project").arg("--editable").arg("--script").arg("script.py"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Updated `script.py`
"###);

let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?;

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
script_content, @r###"
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "project",
# ]
#
# [tool.uv.sources]
# project = { path = "project", editable = true }
# ///
print("Hello, world!")
"###
);
});
Ok(())
}

/// Add to a script without an existing metadata table.
#[test]
fn add_script_without_metadata_table() -> Result<()> {
Expand Down
Loading