voormedia/rails-erd · error

Saving diagram failed! Output directory '#{File.dirname(file

Error message

Saving diagram failed!
Output directory '#{File.dirname(filename)}' does not exist.

What it means

The Mermaid generator's save callback (mermaid.rb:105-106) applies the same guard as Graphviz before writing: filename resolves to options.filename + '.mmd' (mermaid.rb:118-120), and unless File.directory?(File.dirname(filename)) it raises before File.write runs. Mermaid output is pure Ruby with no external binary, so this directory check is the only save-time failure mode; with the default filename 'ERD' the dirname '.' always exists, and the error fires when generator: :mermaid runs with a filename option pointing into a directory that does not exist.

Source

Thrown at lib/rails_erd/diagram/mermaid.rb:106

          to.children.each do |child|
            graph << "\t#{quote_entity_name(from.name)} #{er_relation_notation(relationship)} #{quote_entity_name(child.name)} : \"\""
          end
        else
          graph << "\t`#{from.name}` #{relation_arrow(relationship)} `#{to.name}`"

          from.children.each do |child|
            graph << "\t`#{child.name}` #{relation_arrow(relationship)} `#{to.name}`"
          end

          to.children.each do |child|
            graph << "\t`#{from.name}` #{relation_arrow(relationship)} `#{child.name}`"
          end
        end
      end

      save do
        raise "Saving diagram failed!\nOutput directory '#{File.dirname(filename)}' does not exist." unless File.directory?(File.dirname(filename))

        # Emit clustered entities if not already emitted (e.g., no relationships)
        if @clustering_enabled && !@clustered_entities_emitted
          emit_clustered_entities
          @clustered_entities_emitted = true
        end

        File.write(filename.gsub(/\s/,"_"), graph.uniq.join("\n"))
        filename
      end

      def filename
        "#{options.filename}.mmd"
      end

      def diagram_type
        er_diagram? ? "erDiagram" : "classDiagram"
      end

View on GitHub (pinned to dae6e52018)

Solutions

  1. Create the directory: mkdir -p docs
  2. Or point filename at an existing directory
  3. Or create it programmatically: require 'fileutils'; FileUtils.mkdir_p(File.dirname(path)) before generating

Example fix

# before
RailsERD::Diagram::Mermaid.create(filename: 'docs/ERD')
# => RuntimeError: Saving diagram failed! Output directory 'docs' does not exist.

# after
require 'fileutils'
FileUtils.mkdir_p('docs')
RailsERD::Diagram::Mermaid.create(filename: 'docs/ERD')
Defensive patterns

Strategy: validation

Validate before calling

path = File.join('docs', 'ERD.mmd')
require 'fileutils'
FileUtils.mkdir_p(File.dirname(path))
RailsERD::Diagram::Mermaid.create(filename: 'docs/ERD')

Try / catch

begin
  RailsERD::Diagram::Mermaid.create(filename: 'docs/ERD')
rescue RuntimeError => e
  raise unless e.message.include?('Output directory')
  require 'fileutils'
  FileUtils.mkdir_p('docs')
  retry
end

Prevention

When it happens

Trigger: rake erd generator=mermaid filename=docs/ERD, or RailsERD.options[:generator] = :mermaid with erd.filename = 'docs/erd', when docs/ is missing; any environment where the configured output directory was never created.

Common situations: CI clean checkouts where doc/ or db/diagrams/ is gitignored; switching generator from graphviz to mermaid while reusing a nested filename config; the output directory renamed after the config was written.

Related errors


AI-assisted analysis of voormedia/rails-erd@dae6e52018 (2026-08-23). Data as JSON: /api/errors/5ffe4727d205ced6. Report an issue: GitHub.