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 uv run --script #7739

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,13 @@ pub struct RunArgs {
#[arg(long, conflicts_with = "locked")]
pub frozen: bool,

/// Run the given path as a Python script.
///
/// Using `--script` will attempt to parse the path as a PEP 723 script,
/// irrespective of its extension.
#[arg(long)]
Copy link
Contributor Author

@tfsingh tfsingh Sep 27, 2024

Choose a reason for hiding this comment

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

Not sure what the best approach to arg parsing is here — we could explicitly enumerate conflicts (such as --extra and --all-extras), but we don't even warn for these as is when running a script (implicitly) so not sure if it's worth doing. Happy to either way!

pub script: bool,

#[command(flatten)]
pub installer: ResolverInstallerArgs,

Expand Down
19 changes: 16 additions & 3 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::ffi::OsString;
use std::fmt::Write;
use std::io::stdout;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use anstream::eprintln;
Expand Down Expand Up @@ -131,8 +131,21 @@ async fn run(cli: Cli) -> Result<ExitStatus> {

// Parse the external command, if necessary.
let run_command = if let Commands::Project(command) = &*cli.command {
if let ProjectCommand::Run(uv_cli::RunArgs { command, .. }) = &**command {
Some(RunCommand::try_from(command)?)
if let ProjectCommand::Run(uv_cli::RunArgs {
command, script, ..
}) = &**command
{
if *script {
let (target, args) = command.split();
if let Some(target) = target {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was lifted from RunCommand::try_from, I thought it best to not meddle with that API by adding a script parameter

let path = PathBuf::from(&target);
Some(RunCommand::PythonScript(path, args.to_vec()))
} else {
anyhow::bail!("Script path must be supplied when using `uv run --script`");
}
} else {
Some(RunCommand::try_from(command)?)
}
} else {
None
}
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl RunSettings {
no_dev,
only_dev,
no_editable,
script: _,
command: _,
with,
with_editable,
Expand Down
34 changes: 34 additions & 0 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,3 +2127,37 @@ fn run_script_without_build_system() -> Result<()> {

Ok(())
}

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

let test_script = context.temp_dir.child("script");
test_script.write_str(indoc! { r#"
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "iniconfig",
# ]
# ///
import iniconfig
print("Hello, world!")
"#
})?;

uv_snapshot!(context.filters(), context.run().arg("--script").arg("script"), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!

----- stderr -----
Reading inline script metadata from: script
Resolved 1 package in [TIME]
Prepared 1 package in [TIME]
Installed 1 package in [TIME]
+ iniconfig==2.0.0
"###);

Ok(())
}
4 changes: 4 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ uv run [OPTIONS] <COMMAND>

<li><code>lowest-direct</code>: Resolve the lowest compatible version of any direct dependencies, and the highest compatible version of any transitive dependencies</li>
</ul>
</dd><dt><code>--script</code></dt><dd><p>Run the given path as a Python script.</p>

<p>Using <code>--script</code> will attempt to parse the path as a PEP 723 script, irrespective of its extension.</p>

</dd><dt><code>--upgrade</code>, <code>-U</code></dt><dd><p>Allow package upgrades, ignoring pinned versions in any existing output file. Implies <code>--refresh</code></p>

</dd><dt><code>--upgrade-package</code>, <code>-P</code> <i>upgrade-package</i></dt><dd><p>Allow upgrades for a specific package, ignoring pinned versions in any existing output file. Implies <code>--refresh-package</code></p>
Expand Down
Loading