Skip to content

Commit 3f98041

Browse files
authored
fix: Audit 07/03 (#2040)
* Fix dupe nodes. * Fix tests.
1 parent 872a316 commit 3f98041

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

.yarn/versions/ffd25b17.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
releases:
2+
"@moonrepo/cli": patch
3+
"@moonrepo/core-linux-arm64-gnu": patch
4+
"@moonrepo/core-linux-arm64-musl": patch
5+
"@moonrepo/core-linux-x64-gnu": patch
6+
"@moonrepo/core-linux-x64-musl": patch
7+
"@moonrepo/core-macos-arm64": patch
8+
"@moonrepo/core-macos-x64": patch
9+
"@moonrepo/core-windows-x64-msvc": patch

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
#### 🐞 Fixes
6+
7+
- Fixed the duplicate nodes in the action graph.
8+
39
## 1.38.2
410

511
#### 🚀 Updates

crates/action-graph/src/action_graph_builder.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ use petgraph::prelude::*;
2222
use rustc_hash::{FxHashMap, FxHashSet};
2323
use std::mem;
2424
use std::sync::Arc;
25-
use tokio::sync::RwLock;
2625
use tracing::{debug, instrument, trace};
2726

2827
macro_rules! insert_node_or_exit {
2928
($builder:ident, $node:expr) => {{
3029
let node = $node;
3130

32-
match $builder.get_index_from_node(&node).await {
31+
match $builder.get_index_from_node(&node) {
3332
Some(index) => {
3433
return Ok(Some(index));
3534
}
36-
None => $builder.insert_node(node).await,
35+
None => $builder.insert_node(node),
3736
}
3837
}};
3938
}
@@ -79,7 +78,7 @@ pub struct ActionGraphBuilder<'query> {
7978
all_query: Option<Criteria<'query>>,
8079
app_context: Arc<AppContext>,
8180
graph: DiGraph<ActionNode, ()>,
82-
nodes: RwLock<FxHashMap<ActionNode, NodeIndex>>,
81+
nodes: FxHashMap<ActionNode, NodeIndex>,
8382
options: ActionGraphBuilderOptions,
8483
platform_manager: Option<PlatformManager>,
8584
workspace_graph: Arc<WorkspaceGraph>,
@@ -107,7 +106,7 @@ impl<'query> ActionGraphBuilder<'query> {
107106
all_query: None,
108107
app_context,
109108
graph: DiGraph::new(),
110-
nodes: RwLock::new(FxHashMap::default()),
109+
nodes: FxHashMap::default(),
111110
options,
112111
passthrough_targets: FxHashSet::default(),
113112
platform_manager: None,
@@ -296,7 +295,7 @@ impl<'query> ActionGraphBuilder<'query> {
296295
} else {
297296
None
298297
}
299-
.unwrap_or_else(|| runtime.to_owned());
298+
.unwrap_or_else(|| self.get_compat_runtime(runtime));
300299

301300
let platform = platform_manager.get_by_toolchain(&new_runtime.toolchain)?;
302301
let packages_root = platform.find_dependency_workspace_root(project.source.as_str())?;
@@ -834,7 +833,7 @@ impl<'query> ActionGraphBuilder<'query> {
834833
});
835834

836835
// Check if the node exists to avoid all the overhead below
837-
if let Some(index) = self.get_index_from_node(&node).await {
836+
if let Some(index) = self.get_index_from_node(&node) {
838837
return Ok(Some(index));
839838
}
840839

@@ -852,7 +851,7 @@ impl<'query> ActionGraphBuilder<'query> {
852851
}
853852

854853
// Insert and then link edges
855-
let index = self.insert_node(node).await;
854+
let index = self.insert_node(node);
856855

857856
if !task.deps.is_empty() {
858857
child_reqs.skip_affected = true;
@@ -921,7 +920,7 @@ impl<'query> ActionGraphBuilder<'query> {
921920
let index = insert_node_or_exit!(
922921
self,
923922
ActionNode::setup_toolchain_legacy(SetupToolchainLegacyNode {
924-
runtime: runtime.to_owned(),
923+
runtime: self.get_compat_runtime(runtime),
925924
})
926925
);
927926

@@ -1038,8 +1037,16 @@ impl<'query> ActionGraphBuilder<'query> {
10381037

10391038
// PRIVATE
10401039

1041-
async fn get_index_from_node(&self, node: &ActionNode) -> Option<NodeIndex> {
1042-
self.nodes.read().await.get(node).cloned()
1040+
fn get_compat_runtime(&self, runtime: &Runtime) -> Runtime {
1041+
let mut next = runtime.to_owned();
1042+
// Disable this, so that the index for both override and not-override
1043+
// is the same, as we only care about the toolchain ID + version
1044+
next.overridden = false;
1045+
next
1046+
}
1047+
1048+
fn get_index_from_node(&self, node: &ActionNode) -> Option<NodeIndex> {
1049+
self.nodes.get(node).cloned()
10431050
}
10441051

10451052
fn link_first_requirement(&mut self, index: NodeIndex, edges: Vec<Option<NodeIndex>>) {
@@ -1070,13 +1077,11 @@ impl<'query> ActionGraphBuilder<'query> {
10701077
}
10711078
}
10721079

1073-
async fn insert_node(&mut self, node: ActionNode) -> NodeIndex {
1074-
let mut nodes = self.nodes.write().await;
1075-
1080+
fn insert_node(&mut self, node: ActionNode) -> NodeIndex {
10761081
let label = node.label();
10771082
let index = self.graph.add_node(node.clone());
10781083

1079-
nodes.insert(node, index);
1084+
self.nodes.insert(node, index);
10801085

10811086
debug!(
10821087
index = index.index(),

crates/action-graph/tests/action_graph_builder_test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2455,18 +2455,25 @@ mod action_graph_builder {
24552455
Id::raw("node"),
24562456
create_runtime_with_version(Version::new(1, 2, 3)),
24572457
);
2458-
let node2 = Runtime::new_override(
2458+
let mut node2 = Runtime::new_override(
24592459
Id::raw("node"),
24602460
create_runtime_with_version(Version::new(4, 5, 6)),
24612461
);
24622462
let node3 = Runtime::new(Id::raw("node"), RuntimeReq::Global);
2463+
let node4 = node1.clone();
2464+
let node5 = node2.clone();
24632465

24642466
builder.setup_toolchain_legacy(&node1).await.unwrap();
24652467
builder.setup_toolchain_legacy(&node2).await.unwrap();
24662468
builder.setup_toolchain_legacy(&node3).await.unwrap();
2469+
builder.setup_toolchain_legacy(&node4).await.unwrap();
2470+
builder.setup_toolchain_legacy(&node5).await.unwrap();
24672471

24682472
let (_, graph) = builder.build();
24692473

2474+
// Remove override since we discard it for equality
2475+
node2.overridden = false;
2476+
24702477
assert_snapshot!(graph.to_dot());
24712478
assert_eq!(
24722479
topo(graph),
@@ -2691,10 +2698,14 @@ mod action_graph_builder {
26912698
create_unresolved_version(Version::new(4, 5, 6)),
26922699
);
26932700
let node3 = ToolchainSpec::new_global(Id::raw("tc-tier3"));
2701+
let node4 = node1.clone();
2702+
let node5 = node2.clone();
26942703

26952704
builder.setup_toolchain(&node1).await.unwrap();
26962705
builder.setup_toolchain(&node2).await.unwrap();
26972706
builder.setup_toolchain(&node3).await.unwrap();
2707+
builder.setup_toolchain(&node4).await.unwrap();
2708+
builder.setup_toolchain(&node5).await.unwrap();
26982709

26992710
let (_, graph) = builder.build();
27002711

crates/toolchain/src/spec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::hash::Hash;
77
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize)]
88
pub struct ToolchainSpec {
99
pub id: Id,
10-
pub overridden: bool,
10+
// pub overridden: bool,
1111
pub req: Option<UnresolvedVersionSpec>,
1212
}
1313

@@ -16,23 +16,23 @@ impl ToolchainSpec {
1616
Self {
1717
id,
1818
req: Some(req),
19-
overridden: false,
19+
// overridden: false,
2020
}
2121
}
2222

2323
pub fn new_global(id: Id) -> Self {
2424
Self {
2525
id,
2626
req: None,
27-
overridden: false,
27+
// overridden: false,
2828
}
2929
}
3030

3131
pub fn new_override(id: Id, req: UnresolvedVersionSpec) -> Self {
3232
Self {
3333
id,
3434
req: Some(req),
35-
overridden: true,
35+
// overridden: true,
3636
}
3737
}
3838

0 commit comments

Comments
 (0)