@@ -6,7 +6,7 @@ use crate::core::dependency::DepKind;
6
6
use crate :: core:: resolver:: { features:: CliFeatures , ForceAllTargets , HasDevUnits } ;
7
7
use crate :: core:: { Package , PackageId , PackageIdSpec , PackageIdSpecQuery , Workspace } ;
8
8
use crate :: ops:: { self , Packages } ;
9
- use crate :: util:: { CargoResult , GlobalContext } ;
9
+ use crate :: util:: CargoResult ;
10
10
use crate :: { drop_print, drop_println} ;
11
11
use anyhow:: Context as _;
12
12
use graph:: Graph ;
@@ -43,8 +43,10 @@ pub struct TreeOptions {
43
43
pub format : String ,
44
44
/// Includes features in the tree as separate nodes.
45
45
pub graph_features : bool ,
46
- /// Maximum display depth of the dependency tree.
47
- pub max_display_depth : u32 ,
46
+ /// Display depth of the dependency tree.
47
+ /// If non-negative integer, display dependencies with that amount of max depth.
48
+ /// If `workspace`, display dependencies from current workspace only.
49
+ pub display_depth : DisplayDepth ,
48
50
/// Excludes proc-macro dependencies.
49
51
pub no_proc_macro : bool ,
50
52
}
@@ -86,6 +88,32 @@ impl FromStr for Prefix {
86
88
}
87
89
}
88
90
91
+ #[ derive( Clone , Copy ) ]
92
+ pub enum DisplayDepth {
93
+ MaxDisplayDepth ( u32 ) ,
94
+ Workspace ,
95
+ }
96
+
97
+ impl FromStr for DisplayDepth {
98
+ type Err = clap:: Error ;
99
+
100
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
101
+ match s {
102
+ "workspace" => Ok ( Self :: Workspace ) ,
103
+ s => s. parse ( ) . map ( Self :: MaxDisplayDepth ) . map_err ( |_| {
104
+ clap:: Error :: raw (
105
+ clap:: error:: ErrorKind :: ValueValidation ,
106
+ format ! (
107
+ "supported values for --depth are non-negative integers and `workspace`, \
108
+ but `{}` is unknown",
109
+ s
110
+ ) ,
111
+ )
112
+ } ) ,
113
+ }
114
+ }
115
+ }
116
+
89
117
struct Symbols {
90
118
down : & ' static str ,
91
119
tee : & ' static str ,
@@ -203,14 +231,14 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
203
231
try to use option `--target all` first, and then narrow your search scope accordingly.",
204
232
) ?;
205
233
} else {
206
- print ( ws. gctx ( ) , opts, root_indexes, & pkgs_to_prune, & graph) ?;
234
+ print ( ws, opts, root_indexes, & pkgs_to_prune, & graph) ?;
207
235
}
208
236
Ok ( ( ) )
209
237
}
210
238
211
239
/// Prints a tree for each given root.
212
240
fn print (
213
- gctx : & GlobalContext ,
241
+ ws : & Workspace < ' _ > ,
214
242
opts : & TreeOptions ,
215
243
roots : Vec < usize > ,
216
244
pkgs_to_prune : & [ PackageIdSpec ] ,
@@ -219,7 +247,7 @@ fn print(
219
247
let format = Pattern :: new ( & opts. format )
220
248
. with_context ( || format ! ( "tree format `{}` not valid" , opts. format) ) ?;
221
249
222
- let symbols = if gctx. shell ( ) . out_unicode ( ) {
250
+ let symbols = if ws . gctx ( ) . shell ( ) . out_unicode ( ) {
223
251
& UTF8_SYMBOLS
224
252
} else {
225
253
& ASCII_SYMBOLS
@@ -231,7 +259,7 @@ fn print(
231
259
232
260
for ( i, root_index) in roots. into_iter ( ) . enumerate ( ) {
233
261
if i != 0 {
234
- drop_println ! ( gctx) ;
262
+ drop_println ! ( ws . gctx( ) ) ;
235
263
}
236
264
237
265
// A stack of bools used to determine where | symbols should appear
@@ -242,15 +270,15 @@ fn print(
242
270
let mut print_stack = vec ! [ ] ;
243
271
244
272
print_node (
245
- gctx ,
273
+ ws ,
246
274
graph,
247
275
root_index,
248
276
& format,
249
277
symbols,
250
278
pkgs_to_prune,
251
279
opts. prefix ,
252
280
opts. no_dedupe ,
253
- opts. max_display_depth ,
281
+ opts. display_depth ,
254
282
& mut visited_deps,
255
283
& mut levels_continue,
256
284
& mut print_stack,
@@ -262,36 +290,36 @@ fn print(
262
290
263
291
/// Prints a package and all of its dependencies.
264
292
fn print_node < ' a > (
265
- gctx : & GlobalContext ,
293
+ ws : & Workspace < ' _ > ,
266
294
graph : & ' a Graph < ' _ > ,
267
295
node_index : usize ,
268
296
format : & Pattern ,
269
297
symbols : & Symbols ,
270
298
pkgs_to_prune : & [ PackageIdSpec ] ,
271
299
prefix : Prefix ,
272
300
no_dedupe : bool ,
273
- max_display_depth : u32 ,
301
+ display_depth : DisplayDepth ,
274
302
visited_deps : & mut HashSet < usize > ,
275
303
levels_continue : & mut Vec < bool > ,
276
304
print_stack : & mut Vec < usize > ,
277
305
) {
278
306
let new = no_dedupe || visited_deps. insert ( node_index) ;
279
307
280
308
match prefix {
281
- Prefix :: Depth => drop_print ! ( gctx, "{}" , levels_continue. len( ) ) ,
309
+ Prefix :: Depth => drop_print ! ( ws . gctx( ) , "{}" , levels_continue. len( ) ) ,
282
310
Prefix :: Indent => {
283
311
if let Some ( ( last_continues, rest) ) = levels_continue. split_last ( ) {
284
312
for continues in rest {
285
313
let c = if * continues { symbols. down } else { " " } ;
286
- drop_print ! ( gctx, "{} " , c) ;
314
+ drop_print ! ( ws . gctx( ) , "{} " , c) ;
287
315
}
288
316
289
317
let c = if * last_continues {
290
318
symbols. tee
291
319
} else {
292
320
symbols. ell
293
321
} ;
294
- drop_print ! ( gctx, "{0}{1}{1} " , c, symbols. right) ;
322
+ drop_print ! ( ws . gctx( ) , "{0}{1}{1} " , c, symbols. right) ;
295
323
}
296
324
}
297
325
Prefix :: None => { }
@@ -307,7 +335,7 @@ fn print_node<'a>(
307
335
} else {
308
336
" (*)"
309
337
} ;
310
- drop_println ! ( gctx, "{}{}" , format. display( graph, node_index) , star) ;
338
+ drop_println ! ( ws . gctx( ) , "{}{}" , format. display( graph, node_index) , star) ;
311
339
312
340
if !new || in_cycle {
313
341
return ;
@@ -321,15 +349,15 @@ fn print_node<'a>(
321
349
EdgeKind :: Feature ,
322
350
] {
323
351
print_dependencies (
324
- gctx ,
352
+ ws ,
325
353
graph,
326
354
node_index,
327
355
format,
328
356
symbols,
329
357
pkgs_to_prune,
330
358
prefix,
331
359
no_dedupe,
332
- max_display_depth ,
360
+ display_depth ,
333
361
visited_deps,
334
362
levels_continue,
335
363
print_stack,
@@ -341,15 +369,15 @@ fn print_node<'a>(
341
369
342
370
/// Prints all the dependencies of a package for the given dependency kind.
343
371
fn print_dependencies < ' a > (
344
- gctx : & GlobalContext ,
372
+ ws : & Workspace < ' _ > ,
345
373
graph : & ' a Graph < ' _ > ,
346
374
node_index : usize ,
347
375
format : & Pattern ,
348
376
symbols : & Symbols ,
349
377
pkgs_to_prune : & [ PackageIdSpec ] ,
350
378
prefix : Prefix ,
351
379
no_dedupe : bool ,
352
- max_display_depth : u32 ,
380
+ display_depth : DisplayDepth ,
353
381
visited_deps : & mut HashSet < usize > ,
354
382
levels_continue : & mut Vec < bool > ,
355
383
print_stack : & mut Vec < usize > ,
@@ -371,13 +399,18 @@ fn print_dependencies<'a>(
371
399
if let Some ( name) = name {
372
400
for continues in & * * levels_continue {
373
401
let c = if * continues { symbols. down } else { " " } ;
374
- drop_print ! ( gctx, "{} " , c) ;
402
+ drop_print ! ( ws . gctx( ) , "{} " , c) ;
375
403
}
376
404
377
- drop_println ! ( gctx, "{}" , name) ;
405
+ drop_println ! ( ws . gctx( ) , "{}" , name) ;
378
406
}
379
407
}
380
408
409
+ let ( max_display_depth, filter_non_workspace_member) = match display_depth {
410
+ DisplayDepth :: MaxDisplayDepth ( max) => ( max, false ) ,
411
+ DisplayDepth :: Workspace => ( u32:: MAX , true ) ,
412
+ } ;
413
+
381
414
// Current level exceeds maximum display depth. Skip.
382
415
if levels_continue. len ( ) + 1 > max_display_depth as usize {
383
416
return ;
@@ -389,6 +422,9 @@ fn print_dependencies<'a>(
389
422
// Filter out packages to prune.
390
423
match graph. node ( * * dep) {
391
424
Node :: Package { package_id, .. } => {
425
+ if filter_non_workspace_member && !ws. is_member_id ( * package_id) {
426
+ return false ;
427
+ }
392
428
!pkgs_to_prune. iter ( ) . any ( |spec| spec. matches ( * package_id) )
393
429
}
394
430
_ => true ,
@@ -399,15 +435,15 @@ fn print_dependencies<'a>(
399
435
while let Some ( dependency) = it. next ( ) {
400
436
levels_continue. push ( it. peek ( ) . is_some ( ) ) ;
401
437
print_node (
402
- gctx ,
438
+ ws ,
403
439
graph,
404
440
* dependency,
405
441
format,
406
442
symbols,
407
443
pkgs_to_prune,
408
444
prefix,
409
445
no_dedupe,
410
- max_display_depth ,
446
+ display_depth ,
411
447
visited_deps,
412
448
levels_continue,
413
449
print_stack,
0 commit comments