fix(flowchart): render nested subgraph with numeric id without dagre rank crash#7771
fix(flowchart): render nested subgraph with numeric id without dagre rank crash#7771devareddy05 wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: 73a1b1f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for mermaid-js ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
@mermaid-js/examples
mermaid
@mermaid-js/layout-elk
@mermaid-js/layout-tidy-tree
@mermaid-js/mermaid-zenuml
@mermaid-js/parser
@mermaid-js/tiny
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7771 +/- ##
==========================================
- Coverage 3.26% 3.26% -0.01%
==========================================
Files 599 600 +1
Lines 60839 60876 +37
Branches 917 917
==========================================
Hits 1986 1986
- Misses 58853 58890 +37
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
The new code path is exercised by the regression test added in both spec files for the same diagram structure described in #7609. Running
Every line of the added |
|
Hi @devareddy05, [sisyphos-bot] What's working well 🎉 Root cause and fix are correct — graphlib backs its node store with a plain JS object, so integer-like keys ("1") are enumerated before other string keys in V8's property 🎉 The fix is applied to both copies — the deprecated dagre-wrapper path and the active rendering-util path both get the sort. Good consistency. 🎉 Test coverage for the regression — both spec files get a direct regression test that reconstructs the exact topology from issue #7609 (outer > 1 > sub > internal with a Things to address 🟡 [important] No Cypress E2E test for the crash scenario. The changes touch rendering-util/layout-algorithms/dagre/mermaid-graphlib.js, which is shared code that affects every diagram using dagre layout. Per project conventions, shared It would be great to add a test case to cypress/integration/rendering/flowchart/flowchart.spec.js with the exact diagram from issue #7609: graph LR 🟡 [important] The rendering-util version goes well beyond the stated PR scope without any description. The PR title and body describe only the numeric ID ordering fix (#7609). But rendering-util/layout-algorithms/dagre/mermaid-graphlib.js also adds:
These are meaningful correctness improvements but they're invisible in the PR description. A couple of sentences covering each would help reviewers and make future bisects much Minor observations 🟢 [nit] nodeDepth is recomputed on every sort comparison, so it's called O(n log n) times with each call walking O(d) up the parent chain. For typical diagrams with shallow const depthCache = new Map(); Not a blocker — just a readability and micro-performance nicety. 🟢 [nit] isNodeInExtractableCluster and findSafeAnchorNode are new private helpers without isolated unit tests. The GLB-DIR tests exercise them indirectly, but explicit tests Security No XSS or injection issues. The new code operates exclusively on abstract graphlib graph structures — no DOM sinks, no string interpolation into SVG attributes, no |
|
Thanks for the review @pbrolin47. On the two points raised by sisyphos-bot: Scope is narrower than the bot suggests. The 13-line change in each No Cypress E2E test for the crash scenario. Fair point, added in On the nits: happy to add a Map-based memo to |
📑 Summary
Fixes
TypeError: Cannot set properties of undefined (setting 'rank')when rendering a flowchart whose nested subgraph uses a numeric id, e.g.subgraph 1 ["inner"].Resolves #7609
Root Cause
dagre-d3-es's graphlib stores nodes in a plain JS object, sograph.nodes()enumerates integer-like keys ("1") before others ("outer").mermaid-graphlib.js'sextractoriterated that list directly, so the inner cluster1was extracted before its parentouter— flattening clustersubinto the cluster-1subgraph as a regular leaf withinternalstill parented under it. The nested recursion then bailed out early because1looked like a leaf incg_outer, leaving a compound node referenced by an edge. dagre's longest-path DFS hit a node with no label and crashed onlabel.rank = …. Non-numeric ids preserve insertion order, so the issue only surfaces with numeric subgraph ids.Changes
extractor's iteration by depth so outermost clusters are processed before inner ones, making cluster extraction independent ofgraph.nodes()ordering.rendering-util/layout-algorithms/dagre/mermaid-graphlib.js(flowchart-v2) anddagre-wrapper/mermaid-graphlib.js(legacy, still used by class and block diagrams).Testing
mermaid-graphlib.spec.jsexercising the issue diagram structure; verifies that the inner cluster becomes a properclusterNodewith its own subgraph afteradjustClustersAndEdges.packages/mermaidtest suite passes (1202 tests).📋 Tasks
fix-7609-numeric-subgraph-id.md, patch).