Skip to content

Commit

Permalink
Skip uv compile step for URL-based dependencies in add command
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Mar 6, 2024
1 parent 2111778 commit 04d8796
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 17 deletions.
38 changes: 22 additions & 16 deletions rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,23 +468,29 @@ fn resolve_requirements_with_uv(
if let Ok(dt) = env::var("__RYE_UV_EXCLUDE_NEWER") {
cmd.arg("--exclude-newer").arg(dt);
}
let sources = ExpandedSources::from_sources(&pyproject_toml.sources()?)?;
sources.add_as_pip_args(&mut cmd);
let mut child = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let child_stdin = child.stdin.as_mut().unwrap();
writeln!(child_stdin, "{}", req)?;

let rv = child.wait_with_output()?;
if !rv.status.success() {
let log = String::from_utf8_lossy(&rv.stderr);
bail!("failed to resolve packages:\n{}", log);
}

let mut new_req: Requirement = String::from_utf8_lossy(&rv.stdout).parse()?;
// we can skip the `uv compile` step for dependencies with direct references (URLs/paths)
let mut new_req = if matches!(req.version_or_url, Some(VersionOrUrl::Url(_))) {
req.clone()
} else {
let sources = ExpandedSources::from_sources(&pyproject_toml.sources()?)?;
sources.add_as_pip_args(&mut cmd);
let mut child = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let child_stdin = child.stdin.as_mut().unwrap();
writeln!(child_stdin, "{}", req)?;

let rv = child.wait_with_output()?;
if !rv.status.success() {
let log = String::from_utf8_lossy(&rv.stderr);
bail!("failed to resolve packages:\n{}", log);
}
String::from_utf8_lossy(&rv.stdout).parse()?
};

if let Some(ref mut version_or_url) = new_req.version_or_url {
if let VersionOrUrl::VersionSpecifier(ref mut specs) = version_or_url {
*version_or_url = VersionOrUrl::VersionSpecifier(VersionSpecifiers::from_iter({
Expand Down
17 changes: 17 additions & 0 deletions rye/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub const INSTA_FILTERS: &[(&str, &str)] = &[
(r" in (\d+\.)?\d+(ms|s)\b", " in [EXECUTION_TIME]"),
(r"\\\\?([\w\d.])", "/$1"),
(r"rye.exe", "rye"),
// fixtures
(
r"(\b[A-Z\d]:)?[\\/].*?[\\/]tests[\\/]fixtures[\\/]",
"[FIXTURES]/",
),
];

fn marked_tempdir() -> TempDir {
Expand Down Expand Up @@ -102,6 +107,18 @@ pub fn get_bin() -> PathBuf {
get_cargo_bin("rye")
}

#[allow(unused)]
pub fn get_fixtures_dir() -> PathBuf {
get_cargo_manifest_dir().join("tests").join("fixtures")
}

#[allow(unused)]
pub fn get_cargo_manifest_dir() -> PathBuf {
std::env::var_os("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap()
}

pub struct Space {
#[allow(unused)]
tempdir: TempDir,
Expand Down
Binary file not shown.
74 changes: 73 additions & 1 deletion rye/tests/test_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;

use toml_edit::{value, ArrayOfTables, Table};

use crate::common::{rye_cmd_snapshot, Space};
use crate::common::{get_fixtures_dir, rye_cmd_snapshot, Space};

mod common;

Expand Down Expand Up @@ -163,3 +163,75 @@ fn test_add_flask_wrong_venv_exported() {
"###);
fs::remove_dir_all(&fake_venv).unwrap();
}

#[test]
fn test_add_from_file_path() {
let space = Space::new();
space.init("my-project");
let fake_venv = space.project_path().join("fake-venv");
fs::create_dir_all(&fake_venv).unwrap();

let wheel_path = get_fixtures_dir()
.join("wheels")
.join("twine-1.5.0-py2.py3-none-any.whl");
rye_cmd_snapshot!(space.rye_cmd().arg("add").arg("twine").arg("--path").arg(wheel_path).env("VIRTUAL_ENV", fake_venv.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
Initializing new virtualenv in [TEMP_PATH]/project/.venv
Python version: [email protected]
Added twine @ file:[FIXTURES]/wheels/twine-1.5.0-py2.py3-none-any.whl as regular dependency
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Done!
----- stderr -----
Built 1 editable in [EXECUTION_TIME]
Resolved 7 packages in [EXECUTION_TIME]
Downloaded 7 packages in [EXECUTION_TIME]
Installed 8 packages in [EXECUTION_TIME]
+ certifi==2023.11.17
+ charset-normalizer==3.3.2
+ idna==3.4
+ my-project==0.1.0 (from file:[TEMP_PATH]/project)
+ pkginfo==1.9.6
+ requests==2.31.0
+ twine==1.5.0 (from file:[FIXTURES]/wheels/twine-1.5.0-py2.py3-none-any.whl)
+ urllib3==2.1.0
"###);
fs::remove_dir_all(&fake_venv).unwrap();
}

#[test]
fn test_add_from_remote_url() {
let space = Space::new();
space.init("my-project");
let fake_venv = space.project_path().join("fake-venv");
fs::create_dir_all(&fake_venv).unwrap();

let url = "https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686";
rye_cmd_snapshot!(space.rye_cmd().arg("add").arg("pip").arg("--url").arg(url).env("VIRTUAL_ENV", fake_venv.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
Initializing new virtualenv in [TEMP_PATH]/project/.venv
Python version: [email protected]
Added pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686 as regular dependency
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Done!
----- stderr -----
Built 1 editable in [EXECUTION_TIME]
Resolved 1 package in [EXECUTION_TIME]
Downloaded 1 package in [EXECUTION_TIME]
Installed 2 packages in [EXECUTION_TIME]
+ my-project==0.1.0 (from file:[TEMP_PATH]/project)
+ pip==1.3.1 (from https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686)
"###);
fs::remove_dir_all(&fake_venv).unwrap();
}

0 comments on commit 04d8796

Please sign in to comment.