dagre icon indicating copy to clipboard operation
dagre copied to clipboard

TypeError: reduce of empty array with no initial value

Open RobinClowers opened this issue 2 years ago • 2 comments

Thanks for your work on dagre! Glad to see someone picking it back up. I just tried to upgrade from 0.8.5 to 1.0.4, and one of my graphs throws this error when calling dagre.layout. The stack trace points to this line.

TypeError: reduce of empty array with no initial value
  at findType1Conflicts(materialize-console/./node_modules/@dagrejs/dagre/lib/position/bk.js:78:12)
  at module.exports.positionX(materialize-console/./node_modules/@dagrejs/dagre/lib/position/bk.js:351:5)
  at module.exports(materialize-console/./node_modules/@dagrejs/dagre/lib/position/index.js:12:18)
  at fn(materialize-console/./node_modules/@dagrejs/dagre/lib/layout.js:47:44)
  at module.exports.notime(materialize-console/./node_modules/@dagrejs/dagre/lib/util.js:250:10)
  at runLayout(materialize-console/./node_modules/@dagrejs/dagre/lib/layout.js:47:3)
  at fn(materialize-console/./node_modules/@dagrejs/dagre/lib/layout.js:24:38)
  at module.exports.notime(materialize-console/./node_modules/@dagrejs/dagre/lib/util.js:250:10)
  at fn(materialize-console/./node_modules/@dagrejs/dagre/lib/layout.js:24:5)
  at module.exports.notime(materialize-console/./node_modules/@dagrejs/dagre/lib/util.js:250:10)
  at module.exports(materialize-console/./node_modules/@dagrejs/dagre/lib/layout.js:21:3)
  at graph(materialize-console/./src/components/WorkflowGraph/WorkflowGraph.tsx:101:5)

RobinClowers avatar Dec 15 '23 22:12 RobinClowers

I believe the issues occurs when neither nodes or edges are added. I've solved the issue by adding this check before calling the layout function:

if (nodes.length > 0 || edges.length > 0) {
    dagre.layout(dagreGraph)
  }

KoehlerAlexander avatar Jan 23 '24 15:01 KoehlerAlexander

@KoehlerAlexander thanks for the suggestion, that did solve the issue! It seems like the library should guard against this, but at least this unblocks my upgrade.

RobinClowers avatar Feb 16 '24 17:02 RobinClowers

I check if the layer is empty before reducing now. This should fix the issue.

rustedgrail avatar Mar 13 '24 14:03 rustedgrail