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

Allow reading --with-requirements from stdin in uv add and uv run #10447

Merged
merged 1 commit into from
Jan 9, 2025
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
5 changes: 0 additions & 5 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ pub(crate) async fn add(
RequirementsSource::SetupCfg(_) => {
bail!("Adding requirements from a `setup.cfg` is not supported in `uv add`");
}
RequirementsSource::RequirementsTxt(path) => {
if path == Path::new("-") {
bail!("Reading requirements from stdin is not supported in `uv add`");
}
}
_ => {}
}
}
Expand Down
15 changes: 14 additions & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub(crate) async fn run(
) -> anyhow::Result<ExitStatus> {
// These cases seem quite complex because (in theory) they should change the "current package".
// Let's ban them entirely for now.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment feels slightly misplaced but I guess it was here already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that's pretty separate.

let mut requirements_from_stdin: bool = false;
for source in &requirements {
match source {
RequirementsSource::PyprojectToml(_) => {
Expand All @@ -106,13 +107,22 @@ pub(crate) async fn run(
}
RequirementsSource::RequirementsTxt(path) => {
if path == Path::new("-") {
bail!("Reading requirements from stdin is not supported in `uv run`");
requirements_from_stdin = true;
}
}
_ => {}
}
}

// Fail early if stdin is used for multiple purposes.
if matches!(
command,
Some(RunCommand::PythonStdin(..) | RunCommand::PythonGuiStdin(..))
) && requirements_from_stdin
{
bail!("Cannot read both requirements file and script from stdin");
}

// Initialize any shared state.
let state = SharedState::default();

Expand Down Expand Up @@ -169,6 +179,9 @@ pub(crate) async fn run(
)?;
}
Pep723Item::Stdin(_) => {
if requirements_from_stdin {
bail!("Cannot read both requirements file and script from stdin");
}
writeln!(
printer.stderr(),
"Reading inline script metadata from `{}`",
Expand Down
13 changes: 13 additions & 0 deletions crates/uv/tests/it/edit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_types)]

use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::prelude::*;
Expand Down Expand Up @@ -4611,6 +4613,17 @@ fn add_requirements_file() -> Result<()> {
);
});

// Passing stdin should succeed
uv_snapshot!(context.filters(), context.add().arg("-r").arg("-").stdin(std::fs::File::open(requirements_txt)?), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved [N] packages in [TIME]
Audited [N] packages in [TIME]
"###);

// Passing a `setup.py` should fail.
uv_snapshot!(context.filters(), context.add().arg("-r").arg("setup.py"), @r###"
success: false
Expand Down
38 changes: 35 additions & 3 deletions crates/uv/tests/it/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,19 +2124,51 @@ fn run_requirements_txt() -> Result<()> {
+ sniffio==1.3.1
"###);

// But reject `-` as a requirements file.
// Allow `-` for stdin.
uv_snapshot!(context.filters(), context.run()
.arg("--with-requirements")
.arg("-")
.arg("--with")
.arg("iniconfig")
.arg("main.py"), @r###"
.arg("main.py")
.stdin(std::fs::File::open(&requirements_txt)?), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 6 packages in [TIME]
Audited 4 packages in [TIME]
Resolved 2 packages in [TIME]
"###);

// But not in combination with reading the script from stdin
uv_snapshot!(context.filters(), context.run()
.arg("--with-requirements")
.arg("-")
// The script to run
.arg("-")
.stdin(std::fs::File::open(&requirements_txt)?), @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: Cannot read both requirements file and script from stdin
"###);

uv_snapshot!(context.filters(), context.run()
.arg("--with-requirements")
.arg("-")
.arg("--script")
.arg("-")
.stdin(std::fs::File::open(&requirements_txt)?), @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: Reading requirements from stdin is not supported in `uv run`
error: Cannot read both requirements file and script from stdin
"###);

Ok(())
Expand Down
Loading