@@ -332,6 +332,8 @@ pub struct Graph {
332
332
// This will be set when creating from a snapshot, then
333
333
// inform the final snapshot creation.
334
334
packages_to_copy_index : HashMap < NpmPackageId , u8 > ,
335
+ #[ cfg( feature = "tracing" ) ]
336
+ traces : Vec < super :: tracing:: TraceGraphSnapshot > ,
335
337
}
336
338
337
339
impl Graph {
@@ -436,6 +438,8 @@ impl Graph {
436
438
package_name_versions : Default :: default ( ) ,
437
439
resolved_node_ids : Default :: default ( ) ,
438
440
root_packages : Default :: default ( ) ,
441
+ #[ cfg( feature = "tracing" ) ]
442
+ traces : Default :: default ( ) ,
439
443
} ;
440
444
let mut created_package_ids =
441
445
HashMap :: with_capacity ( snapshot. packages . len ( ) ) ;
@@ -599,6 +603,11 @@ impl Graph {
599
603
api : & TNpmRegistryApi ,
600
604
patch_packages : & HashMap < PackageName , Vec < NpmPackageVersionInfo > > ,
601
605
) -> Result < NpmResolutionSnapshot , NpmRegistryPackageInfoLoadError > {
606
+ #[ cfg( feature = "tracing" ) ]
607
+ if !self . traces . is_empty ( ) {
608
+ super :: tracing:: output ( & self . traces ) ;
609
+ }
610
+
602
611
let packages_to_pkg_ids = self
603
612
. nodes
604
613
. keys ( )
@@ -1036,6 +1045,25 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
1036
1045
}
1037
1046
} ;
1038
1047
1048
+ #[ cfg( feature = "tracing" ) ]
1049
+ {
1050
+ self . graph . traces . push ( build_trace_graph_snapshot (
1051
+ self . graph ,
1052
+ & self . dep_entry_cache ,
1053
+ & parent_path. with_id (
1054
+ child_id,
1055
+ dep. bare_specifier . clone ( ) ,
1056
+ self
1057
+ . graph
1058
+ . resolved_node_ids
1059
+ . get ( child_id)
1060
+ . unwrap ( )
1061
+ . nv
1062
+ . clone ( ) ,
1063
+ ) ,
1064
+ ) ) ;
1065
+ }
1066
+
1039
1067
if !found_peer {
1040
1068
found_peer = !self . graph . borrow_node_mut ( child_id) . no_peers ;
1041
1069
}
@@ -1053,6 +1081,25 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
1053
1081
& parent_path,
1054
1082
) ?;
1055
1083
1084
+ #[ cfg( feature = "tracing" ) ]
1085
+ if let Some ( child_id) = maybe_new_id {
1086
+ self . graph . traces . push ( build_trace_graph_snapshot (
1087
+ self . graph ,
1088
+ & self . dep_entry_cache ,
1089
+ & parent_path. with_id (
1090
+ child_id,
1091
+ dep. bare_specifier . clone ( ) ,
1092
+ self
1093
+ . graph
1094
+ . resolved_node_ids
1095
+ . get ( child_id)
1096
+ . unwrap ( )
1097
+ . nv
1098
+ . clone ( ) ,
1099
+ ) ,
1100
+ ) ) ;
1101
+ }
1102
+
1056
1103
// For optional peer dependencies, we want to resolve them if any future
1057
1104
// same parent version resolves them. So when not resolved, store them to be
1058
1105
// potentially resolved later.
@@ -1477,6 +1524,71 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
1477
1524
}
1478
1525
}
1479
1526
1527
+ #[ cfg( feature = "tracing" ) ]
1528
+ fn build_trace_graph_snapshot (
1529
+ graph : & Graph ,
1530
+ dep_entry_cache : & DepEntryCache ,
1531
+ current_path : & GraphPath ,
1532
+ ) -> super :: tracing:: TraceGraphSnapshot {
1533
+ use super :: tracing:: * ;
1534
+
1535
+ fn build_path ( current_path : & GraphPath ) -> TraceGraphPath {
1536
+ TraceGraphPath {
1537
+ specifier : current_path. specifier . to_string ( ) ,
1538
+ node_id : current_path. node_id ( ) . 0 ,
1539
+ nv : current_path. nv . to_string ( ) ,
1540
+ previous : current_path. previous_node . as_ref ( ) . and_then ( |n| match n {
1541
+ GraphPathNodeOrRoot :: Node ( graph_path) => {
1542
+ Some ( Box :: new ( build_path ( graph_path) ) )
1543
+ }
1544
+ GraphPathNodeOrRoot :: Root ( _) => None ,
1545
+ } ) ,
1546
+ }
1547
+ }
1548
+
1549
+ TraceGraphSnapshot {
1550
+ nodes : graph
1551
+ . nodes
1552
+ . iter ( )
1553
+ . map ( |( node_id, node) | {
1554
+ let id = graph. get_npm_pkg_id ( * node_id) ;
1555
+ TraceNode {
1556
+ id : node_id. 0 ,
1557
+ resolved_id : id. as_serialized ( ) . to_string ( ) ,
1558
+ children : node
1559
+ . children
1560
+ . iter ( )
1561
+ . map ( |( k, v) | ( k. to_string ( ) , v. 0 ) )
1562
+ . collect ( ) ,
1563
+ dependencies : dep_entry_cache
1564
+ . get ( & id. nv )
1565
+ . map ( |d| {
1566
+ d. iter ( )
1567
+ . map ( |dep| TraceNodeDependency {
1568
+ kind : format ! ( "{:?}" , dep. kind) ,
1569
+ bare_specifier : dep. bare_specifier . to_string ( ) ,
1570
+ name : dep. name . to_string ( ) ,
1571
+ version_req : dep. version_req . to_string ( ) ,
1572
+ peer_dep_version_req : dep
1573
+ . peer_dep_version_req
1574
+ . as_ref ( )
1575
+ . map ( |r| r. to_string ( ) ) ,
1576
+ } )
1577
+ . collect ( )
1578
+ } )
1579
+ . unwrap_or_default ( ) ,
1580
+ }
1581
+ } )
1582
+ . collect ( ) ,
1583
+ roots : graph
1584
+ . root_packages
1585
+ . iter ( )
1586
+ . map ( |( nv, id) | ( nv. to_string ( ) , id. 0 ) )
1587
+ . collect ( ) ,
1588
+ path : build_path ( current_path) ,
1589
+ }
1590
+ }
1591
+
1480
1592
#[ cfg( test) ]
1481
1593
mod test {
1482
1594
use std:: borrow:: Cow ;
@@ -2383,6 +2495,7 @@ mod test {
2383
2495
]
2384
2496
) ;
2385
2497
}
2498
+
2386
2499
#[ tokio:: test]
2387
2500
async fn resolve_peer_dep_other_specifier_slot ( ) {
2388
2501
let api = TestNpmRegistryApi :: default ( ) ;
0 commit comments