Skip to content

Add support for --with-editable to uv tool ru #6744

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

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,14 @@ pub struct ToolRunArgs {
#[arg(long)]
pub with: Vec<String>,

/// Run with the given packages installed as editables
///
/// When used in a project, these dependencies will be layered on top of
/// the uv tool's environment in a separate, ephemeral environment. These
/// dependencies are allowed to conflict with those specified.
#[arg(long)]
pub with_editable: Vec<String>,

/// Run with all packages listed in the given `requirements.txt` files.
#[arg(long, value_parser = parse_maybe_file_path)]
pub with_requirements: Vec<Maybe<PathBuf>>,
Expand Down
5 changes: 5 additions & 0 deletions crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ pub(crate) async fn run(
eprint!("{report:?}");
return Ok(ExitStatus::Failure);
}
Err(ProjectError::Operation(operations::Error::Named(err))) => {
let err = miette::Report::msg(format!("{err}")).context("Invalid `--with` requirement");
eprint!("{err:?}");
return Ok(ExitStatus::Failure);
}
Err(err) => return Err(err.into()),
};

Expand Down
5 changes: 5 additions & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
.with
.into_iter()
.map(RequirementsSource::from_package)
.chain(
args.with_editable
.into_iter()
.map(RequirementsSource::Editable),
)
.chain(
args.with_requirements
.into_iter()
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ pub(crate) struct ToolRunSettings {
pub(crate) command: Option<ExternalCommand>,
pub(crate) from: Option<String>,
pub(crate) with: Vec<String>,
pub(crate) with_editable: Vec<String>,
pub(crate) with_requirements: Vec<PathBuf>,
pub(crate) isolated: bool,
pub(crate) show_resolution: bool,
Expand All @@ -285,6 +286,7 @@ impl ToolRunSettings {
command,
from,
with,
with_editable,
with_requirements,
isolated,
show_resolution,
Expand All @@ -308,6 +310,7 @@ impl ToolRunSettings {
command,
from,
with,
with_editable,
with_requirements: with_requirements
.into_iter()
.filter_map(Maybe::into_option)
Expand Down
131 changes: 130 additions & 1 deletion crates/uv/tests/tool_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use common::{uv_snapshot, TestContext};
use common::{copy_dir_all, uv_snapshot, TestContext};
use indoc::indoc;

mod common;
Expand Down Expand Up @@ -823,6 +823,135 @@ fn tool_run_without_output() {
"###);
}

#[test]
fn tool_run_with_editable() -> anyhow::Result<()> {
let context = TestContext::new("3.12").with_filtered_counts();
let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin");

let anyio_local = context.temp_dir.child("src").child("anyio_local");
copy_dir_all(
context.workspace_root.join("scripts/packages/anyio_local"),
&anyio_local,
)?;

let black_editable = context.temp_dir.child("src").child("black_editable");
copy_dir_all(
context
.workspace_root
.join("scripts/packages/black_editable"),
&black_editable,
)?;

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! { r#"
[project]
name = "foo"
version = "1.0.0"
requires-python = ">=3.8"
dependencies = ["anyio", "sniffio==1.3.1"]
"#
})?;

let test_script = context.temp_dir.child("main.py");
test_script.write_str(indoc! { r"
import sniffio
"
})?;

uv_snapshot!(context.filters(), context.tool_run()
.arg("--with-editable")
.arg("./src/black_editable")
.arg("--with")
.arg("iniconfig")
.arg("flask")
.arg("--version")
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
Python 3.12.[X]
Flask 3.0.2
Werkzeug 3.0.1

----- stderr -----
Resolved [N] packages in [TIME]
Prepared [N] packages in [TIME]
Installed [N] packages in [TIME]
+ black==0.1.0 (from file://[TEMP_DIR]/src/black_editable)
+ blinker==1.7.0
+ click==8.1.7
+ flask==3.0.2
+ iniconfig==2.0.0
+ itsdangerous==2.1.2
+ jinja2==3.1.3
+ markupsafe==2.1.5
+ werkzeug==3.0.1
"###);

// Requesting an editable requirement should install it in a layer, even if it satisfied
uv_snapshot!(context.filters(), context.tool_run().arg("--with-editable").arg("./src/anyio_local").arg("flask").arg("--version"), @r###"
success: true
exit_code: 0
----- stdout -----
Python 3.12.[X]
Flask 3.0.2
Werkzeug 3.0.1

----- stderr -----
Resolved [N] packages in [TIME]
Prepared [N] packages in [TIME]
Installed [N] packages in [TIME]
+ anyio==4.3.0+foo (from file://[TEMP_DIR]/src/anyio_local)
+ blinker==1.7.0
+ click==8.1.7
+ flask==3.0.2
+ itsdangerous==2.1.2
+ jinja2==3.1.3
+ markupsafe==2.1.5
+ werkzeug==3.0.1
"###);

// Requesting the project itself should use a new environment.
uv_snapshot!(context.filters(), context.tool_run().arg("--with-editable").arg(".").arg("flask").arg("--version"), @r###"
success: true
exit_code: 0
----- stdout -----
Python 3.12.[X]
Flask 3.0.2
Werkzeug 3.0.1

----- stderr -----
Resolved [N] packages in [TIME]
Prepared [N] packages in [TIME]
Installed [N] packages in [TIME]
+ anyio==4.3.0
+ blinker==1.7.0
+ click==8.1.7
+ flask==3.0.2
+ foo==1.0.0 (from file://[TEMP_DIR]/)
+ idna==3.6
+ itsdangerous==2.1.2
+ jinja2==3.1.3
+ markupsafe==2.1.5
+ sniffio==1.3.1
+ werkzeug==3.0.1
"###);

// If invalid, we should reference `--with`.
uv_snapshot!(context.filters(), context.tool_run().arg("--with").arg("./foo").arg("flask").arg("--version"), @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: Distribution not found at: file://[TEMP_DIR]/foo
"###);
Copy link
Member

Choose a reason for hiding this comment

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

Just a quick comment, looks like this doesn't reference --with like we'd hope :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review :)
I'll try working on it for a few more days 👍🏽

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've implemented the feature similarly to the pull request, but I'm running into an issue where an anyhow::Error is being returned, which is preventing me from adding context to the error. I need to investigate this further, but if you have any insights or know of a possible cause, I would greatly appreciate your input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zanieb
Could you please check it once?

Copy link
Member

Choose a reason for hiding this comment

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

73085ae has the relevant fix


Ok(())
}

#[test]
fn warn_no_executables_found() {
let context = TestContext::new("3.12").with_filtered_exe_suffix();
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,10 @@ uv tool run [OPTIONS] [COMMAND]

</dd><dt><code>--with</code> <i>with</i></dt><dd><p>Run with the given packages installed</p>

</dd><dt><code>--with-editable</code> <i>with-editable</i></dt><dd><p>Run with the given packages installed as editables</p>

<p>When used in a project, these dependencies will be layered on top of the project environment in a separate, ephemeral environment. These dependencies are allowed to conflict with those specified by the project.</p>

</dd><dt><code>--with-requirements</code> <i>with-requirements</i></dt><dd><p>Run with all packages listed in the given <code>requirements.txt</code> files</p>

</dd></dl>
Expand Down
Loading