Skip to content

Commit

Permalink
Preserve given for tool.uv.sources paths (astral-sh#3412)
Browse files Browse the repository at this point in the history
We now correctly emit relative paths in `uv pip compile` with
`tool.uv.sources` path inputs.

`tool.uv.sources` is mainly intended to be used with the uv lock file
over requirements.txt, but it's good to have basic `uv pip` support
working.

Fixes astral-sh#3366
  • Loading branch information
konstin authored May 7, 2024
1 parent 18516b4 commit 24f38d7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/uv-requirements/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ fn path_source(
project_dir: &Path,
editable: Option<bool>,
) -> Result<RequirementSource, LoweringError> {
let url = VerbatimUrl::parse_path(&path, project_dir);
let url = VerbatimUrl::parse_path(&path, project_dir).with_given(path.clone());
let path_buf = PathBuf::from(&path);
let path_buf = path_buf
.absolutize_from(project_dir)
Expand Down
83 changes: 83 additions & 0 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8652,3 +8652,86 @@ fn git_source_missing_tag() -> Result<()> {

Ok(())
}

#[test]
fn tool_uv_sources() -> Result<()> {
let context = TestContext::new("3.12");
// Use a subdir to test path normalization.
let require_path = "some_dir/pyproject.toml";
let pyproject_toml = context.temp_dir.child(require_path);
pyproject_toml.write_str(indoc! {r#"
[project]
name = "foo"
version = "0.0.0"
dependencies = [
"tqdm>4,<=5",
"packaging @ git+https://github.com/pypa/packaging@32deafe8668a2130a3366b98154914d188f3718e",
"poetry_editable",
"urllib3 @ https://files.pythonhosted.org/packages/a2/73/a68704750a7679d0b6d3ad7aa8d4da8e14e151ae82e6fee774e6e0d05ec8/urllib3-2.2.1-py3-none-any.whl",
# Windows consistency
"colorama>0.4,<5",
]
[project.optional-dependencies]
utils = [
"boltons==24.0.0"
]
dont_install_me = [
"broken @ https://example.org/does/not/exist"
]
[tool.uv.sources]
tqdm = { url = "https://files.pythonhosted.org/packages/a5/d6/502a859bac4ad5e274255576cd3e15ca273cdb91731bc39fb840dd422ee9/tqdm-4.66.0-py3-none-any.whl" }
boltons = { git = "https://github.com/mahmoud/boltons", rev = "57fbaa9b673ed85b32458b31baeeae230520e4a0" }
poetry_editable = { path = "../poetry_editable", editable = true }
"#})?;

let project_root = fs_err::canonicalize(std::env::current_dir()?.join("../.."))?;
fs_err::create_dir_all(context.temp_dir.join("poetry_editable/poetry_editable"))?;
fs_err::copy(
project_root.join("scripts/packages/poetry_editable/pyproject.toml"),
context.temp_dir.join("poetry_editable/pyproject.toml"),
)?;
fs_err::copy(
project_root.join("scripts/packages/poetry_editable/poetry_editable/__init__.py"),
context
.temp_dir
.join("poetry_editable/poetry_editable/__init__.py"),
)?;

let mut filters = context.filters();
// Remove windows-only tqdm -> colorama dependency
filters.push((" # via tqdm\n", ""));

// Install the editable packages.
uv_snapshot!(filters, windows_filters=false, context.compile()
.arg("--preview")
.arg(require_path)
.arg("--extra")
.arg("utils"), @r###"
success: true
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z --preview some_dir/pyproject.toml --extra utils
-e ../poetry_editable
anyio==4.3.0
# via poetry-editable
boltons @ git+https://github.com/mahmoud/boltons@57fbaa9b673ed85b32458b31baeeae230520e4a0
colorama==0.4.6
idna==3.6
# via anyio
packaging @ git+https://github.com/pypa/packaging@32deafe8668a2130a3366b98154914d188f3718e
sniffio==1.3.1
# via anyio
tqdm @ https://files.pythonhosted.org/packages/a5/d6/502a859bac4ad5e274255576cd3e15ca273cdb91731bc39fb840dd422ee9/tqdm-4.66.0-py3-none-any.whl
urllib3 @ https://files.pythonhosted.org/packages/a2/73/a68704750a7679d0b6d3ad7aa8d4da8e14e151ae82e6fee774e6e0d05ec8/urllib3-2.2.1-py3-none-any.whl
----- stderr -----
Built 1 editable in [TIME]
Resolved 9 packages in [TIME]
"###
);

Ok(())
}

0 comments on commit 24f38d7

Please sign in to comment.