Skip to content

Commit

Permalink
Implement uv tree --no-dev (astral-sh#8109)
Browse files Browse the repository at this point in the history
## Summary

Allow pruning dev-dependencies in uv tree.
This is not inherently in conflict with --invert, but this pruning is
not yet implemented there.
  • Loading branch information
bluss authored Oct 12, 2024
1 parent a3b11da commit e67d873
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
11 changes: 11 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3070,6 +3070,17 @@ pub struct TreeArgs {
#[command(flatten)]
pub tree: DisplayTreeArgs,

/// Include development dependencies.
///
/// Development dependencies are defined via `tool.uv.dev-dependencies` in a
/// `pyproject.toml`.
#[arg(long, overrides_with("no_dev"), hide = true)]
pub dev: bool,

/// Omit development dependencies.
#[arg(long, overrides_with("dev"), conflicts_with = "invert")]
pub no_dev: bool,

/// Assert that the `uv.lock` will remain unchanged.
///
/// Requires that the lockfile is up-to-date. If the lockfile is missing or
Expand Down
10 changes: 9 additions & 1 deletion crates/uv-resolver/src/lock/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::BTreeSet;
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};

use uv_configuration::DevMode;
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_pypi_types::ResolverMarkerEnvironment;

Expand All @@ -22,8 +23,10 @@ pub struct TreeDisplay<'env> {
optional_dependencies:
FxHashMap<&'env PackageId, FxHashMap<ExtraName, Vec<Cow<'env, Dependency>>>>,
dev_dependencies: FxHashMap<&'env PackageId, FxHashMap<GroupName, Vec<Cow<'env, Dependency>>>>,
/// Maximum display depth of the dependency tree
/// Maximum display depth of the dependency tree.
depth: usize,
/// Whether to include development dependencies in the display.
dev: DevMode,
/// Prune the given packages from the display of the dependency tree.
prune: Vec<PackageName>,
/// Display only the specified packages.
Expand All @@ -40,6 +43,7 @@ impl<'env> TreeDisplay<'env> {
depth: usize,
prune: Vec<PackageName>,
packages: Vec<PackageName>,
dev: DevMode,
no_dedupe: bool,
invert: bool,
) -> Self {
Expand Down Expand Up @@ -180,6 +184,7 @@ impl<'env> TreeDisplay<'env> {
optional_dependencies,
dev_dependencies,
depth,
dev,
prune,
packages,
no_dedupe,
Expand Down Expand Up @@ -231,12 +236,14 @@ impl<'env> TreeDisplay<'env> {
let dependencies: Vec<Node<'env>> = self
.dependencies
.get(node.package_id())
.filter(|_| self.dev != DevMode::Only)
.into_iter()
.flatten()
.map(|dep| Node::Dependency(dep.as_ref()))
.chain(
self.optional_dependencies
.get(node.package_id())
.filter(|_| self.dev != DevMode::Only)
.into_iter()
.flatten()
.flat_map(|(extra, deps)| {
Expand All @@ -247,6 +254,7 @@ impl<'env> TreeDisplay<'env> {
.chain(
self.dev_dependencies
.get(node.package_id())
.filter(|_| self.dev != DevMode::Exclude)
.into_iter()
.flatten()
.flat_map(|(group, deps)| {
Expand Down
4 changes: 3 additions & 1 deletion crates/uv/src/commands/project/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::Path;

use uv_cache::Cache;
use uv_client::Connectivity;
use uv_configuration::{Concurrency, TargetTriple};
use uv_configuration::{Concurrency, DevMode, TargetTriple};
use uv_pep508::PackageName;
use uv_python::{PythonDownloads, PythonPreference, PythonRequest, PythonVersion};
use uv_resolver::TreeDisplay;
Expand All @@ -21,6 +21,7 @@ use crate::settings::ResolverSettings;
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) async fn tree(
project_dir: &Path,
dev: DevMode,
locked: bool,
frozen: bool,
universal: bool,
Expand Down Expand Up @@ -89,6 +90,7 @@ pub(crate) async fn tree(
depth.into(),
prune,
package,
dev,
no_dedupe,
invert,
);
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,7 @@ async fn run_project(

commands::tree(
project_dir,
args.dev,
args.locked,
args.frozen,
args.universal,
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ impl RemoveSettings {
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub(crate) struct TreeSettings {
pub(crate) dev: DevMode,
pub(crate) locked: bool,
pub(crate) frozen: bool,
pub(crate) universal: bool,
Expand All @@ -956,6 +957,8 @@ impl TreeSettings {
let TreeArgs {
tree,
universal,
dev,
no_dev,
locked,
frozen,
build,
Expand All @@ -966,6 +969,7 @@ impl TreeSettings {
} = args;

Self {
dev: DevMode::from_args(dev, no_dev, false),
locked,
frozen,
universal,
Expand Down
14 changes: 13 additions & 1 deletion crates/uv/tests/it/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ fn dev_dependencies() -> Result<()> {
"#,
)?;

uv_snapshot!(context.filters(), context.tree().arg("--universal"), @r###"
uv_snapshot!(context.filters(), context.tree(), @r###"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -453,6 +453,18 @@ fn dev_dependencies() -> Result<()> {
"###
);

uv_snapshot!(context.filters(), context.tree().arg("--no-dev"), @r###"
success: true
exit_code: 0
----- stdout -----
project v0.1.0
└── iniconfig v2.0.0
----- stderr -----
Resolved 5 packages in [TIME]
"###
);

// `uv tree` should update the lockfile
let lock = context.read("uv.lock");
assert!(!lock.is_empty());
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,8 @@ uv tree [OPTIONS]
<p>May also be set with the <code>UV_NO_CONFIG</code> environment variable.</p>
</dd><dt><code>--no-dedupe</code></dt><dd><p>Do not de-duplicate repeated dependencies. Usually, when a package has already displayed its dependencies, further occurrences will not re-display its dependencies, and will include a (*) to indicate it has already been shown. This flag will cause those duplicates to be repeated</p>

</dd><dt><code>--no-dev</code></dt><dd><p>Omit development dependencies</p>

</dd><dt><code>--no-index</code></dt><dd><p>Ignore the registry index (e.g., PyPI), instead relying on direct URL dependencies and those provided via <code>--find-links</code></p>

</dd><dt><code>--no-progress</code></dt><dd><p>Hide all progress outputs.</p>
Expand Down

0 comments on commit e67d873

Please sign in to comment.