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

Before rendering, the Graphviz save callback (graphviz.rb:215-216) verifies the directory part of the output path exists: filename resolves to options.filename + '.' + options.filetype (graphviz.rb:297-299, default 'ERD.pdf'), and unless File.directory?(File.dirname(filename)) it raises immediately. With the default filename 'ERD' the dirname is '.' and never fails; the error appears as soon as the filename option points into a directory that does not exist, because neither rails-erd nor ruby-graphviz creates intermediate directories.

Source

Thrown at lib/rails_erd/diagram/graphviz.rb:216

        GRAPH_ATTRIBUTES.each { |attribute, value| graph[attribute] = value }
        NODE_ATTRIBUTES.each  { |attribute, value| graph.node[attribute] = value }
        EDGE_ATTRIBUTES.each  { |attribute, value| graph.edge[attribute] = value }

        # Switch rank direction if we're creating a vertically oriented graph.
        graph[:rankdir] = (options.orientation == "vertical") ? :LR : :TB

        # Title of the graph itself.
        graph[:label] = "#{title}\\n\\n" if title

        # Style of splines
        graph[:splines] = options.splines unless options.splines.nil?

        # Setup notation options.
        extend self.class.const_get(options.notation.to_s.capitalize.to_sym)
      end

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

        begin
          # GraphViz doesn't like spaces in the filename
          graph.output(filetype => filename.gsub(/\s/,"_"))
          filename
        rescue RuntimeError => e
          raise "Saving diagram failed!\nGraphviz produced errors. Verify it " +
                  "has support for filetype=#{options.filetype}, or use " +
                  "filetype=dot.\nOriginal error: #{e.message.split("\n").last}"
        rescue StandardError => e
          raise "Saving diagram failed!\nVerify that Graphviz is installed " +
                  "and in your path, or use filetype=dot."
        end
      end

      each_entity do |entity, attributes|
        if options[:cluster] && entity.namespace
          cluster_name = "cluster_#{entity.namespace}"

View on GitHub (pinned to dae6e52018)

Solutions

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

Example fix

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

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

Strategy: validation

Validate before calling

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

Try / catch

begin
  RailsERD::Diagram::Graphviz.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: RailsERD::Diagram::Graphviz.create(filename: 'docs/ERD') when docs/ does not exist; erd config erd.filename = 'doc/diagrams/erd' with a missing path segment; rake erd filename=tmp/erd after tmp/ was cleaned. Any filetype (pdf/png/dot) triggers it -- the check runs before graph.output is ever called.

Common situations: Writing diagrams into doc/ or db/visual/ folders that are gitignored and absent on CI clean checkouts; renaming the output directory after the erd config was written; fresh clones on machines that never had the directory.

Related errors


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