iOfficeAI/OfficeCLI · error · ArgumentException

diagram has no nodes — the mermaid source has no node/edge s

Error message

diagram has no nodes — the mermaid source has no node/edge statements (e.g. 'flowchart TD; A[Start] --> B[End]').

What it means

Thrown by FlowchartLayout.Layout (FlowchartLayout.cs:55) when the parsed DiagramGraph has zero nodes. This happens when the mermaid source has a flowchart header (or no header, defaulting to flowchart) but no parseable node/edge statements — e.g. a bare 'flowchart TD' with nothing after it, or a line that failed to parse into any node. The guard exists because otherwise the bounding-box Min/Max call later throws a bare 'Sequence contains no elements' that surfaces as an opaque internal_error.

Source

Thrown at src/officecli/Core/Diagram/FlowchartLayout.cs:55

    {
        public string From = "", To = "", Label = "";
        public bool Rev, Self;
        public List<string> Wp = new();     // waypoint dummy ids, source→target order
        // routing scratch
        public string SSide = "", TSide = "";
        public Pt SRef, TRef;
        public double LabelDy;
    }

    public static LaidOutGraph Layout(DiagramGraph g)
    {
        // A header with no node/edge statements (e.g. a bare "flowchart TD", or
        // input whose only line failed to parse into a node) leaves zero nodes.
        // Guard here with a clear message — otherwise the bounding-box Min/Max
        // below throws a bare "Sequence contains no elements" that surfaces as
        // internal_error. Mirrors SequenceLayout's empty-participants guard.
        if (g.Nodes.Count == 0)
            throw new ArgumentException(
                "diagram has no nodes — the mermaid source has no node/edge statements "
                + "(e.g. 'flowchart TD; A[Start] --> B[End]').");

        bool td = g.Direction == FlowDirection.TopDown;
        var nodes = new Dictionary<string, WNode>();
        var real = new List<string>();
        foreach (var dn in g.Nodes)
        {
            var n = new WNode { Id = dn.Id, Label = dn.Label, Shape = dn.Shape };
            SizeNode(n);
            nodes[dn.Id] = n;
            real.Add(dn.Id);
        }

        var edges = new List<WEdge>();
        foreach (var de in g.Edges)
        {
            if (!nodes.ContainsKey(de.From) || !nodes.ContainsKey(de.To)) continue;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Add at least one node or edge statement, e.g. 'flowchart TD; A[Start] --> B[End]'.
  2. Remove leading %% comment-only lines that leave no real statements.
  3. Verify the node syntax (A[Label], A-->B) matches what MermaidParser accepts.

Example fix

// before
flowchart TD
// after
flowchart TD
  A[Start] --> B[End]
Defensive patterns

Strategy: validation

Validate before calling

static void EnsureDiagramHasNodes(string mermaid)
{
    // crude pre-check: at least one line looks like a node or edge statement
    var hasStatement = mermaid.Split('\n')
        .Select(l => l.Trim())
        .Any(l => l.Length > 0 && !l.StartsWith("%%") && !Regex.IsMatch(l, @"^(flowchart|graph|sequenceDiagram)", RegexOptions.IgnoreCase)
            && (l.Contains("-->") || l.Contains("---") || Regex.IsMatch(l, @"^\w+[\[\(]")));
    if (!hasStatement) throw new ArgumentException("diagram has no node/edge statements");
}

Type guard

static bool DiagramLikelyHasNodes(string mermaid) =>
    mermaid.Split('\n').Select(l => l.Trim())
        .Any(l => l.Length > 0 && !l.StartsWith("%%") && (l.Contains("-->") || l.Contains("---") || Regex.IsMatch(l, @"^\w+[\[\(]")));

Try / catch

try { FlowchartLayout.Layout(graph); }
catch (ArgumentException ex) when (ex.Message.Contains("diagram has no nodes"))
{ /* prompt user to add node/edge statements */ }

Prevention

When it happens

Trigger: Compiling a source that is just 'flowchart TD' with no edges; a source whose only statement line is a comment (%%) or unparseable text; a sequence diagram that parsed zero participants would hit the analogous guard in SequenceLayout.

Common situations: Empty/incomplete mermaid pasted from a template; a flowchart whose body is all comments; node statements that use a syntax MermaidParser does not recognize so nothing is produced.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/135edfa9d22dfa77. Report an issue: GitHub.