dagrejs/dagre

TypeError: reduce of empty array with no initial value

RobinClowers opened this issue · 3 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)

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 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.

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