ruby/ruby · error · ArgumentError

empty parent path

Error message

empty parent path

What it means

Tmpname.create (backing Dir.mktmpdir, Dir.mktmpname and friends) raises ArgumentError 'empty parent path' when an explicit tmpdir argument was supplied but File.path(tmpdir) resolves to "". It guards against joining the temp name onto an empty string, which would silently create hidden files in the current directory.

Source

Thrown at lib/tmpdir.rb:162

    # Dedicated random number generator
    RANDOM = Object.new
    class << RANDOM # :nodoc:
      # Maximum random number
      MAX = 36**6 # < 0x100000000

      # Returns new random string upto 6 bytes
      def next
        (::Random.urandom(4).unpack1("L")%MAX).to_s(36)
      end
    end
    RANDOM.freeze
    private_constant :RANDOM

    # Generates and yields random names to create a temporary name
    def create(basename, tmpdir=nil, max_try: nil, **opts)
      if tmpdir
        origdir = tmpdir = File.path(tmpdir)
        raise ArgumentError, "empty parent path" if tmpdir.empty?
      else
        tmpdir = tmpdir()
      end
      n = nil
      prefix, suffix = basename
      prefix = (String.try_convert(prefix) or
                raise ArgumentError, "unexpected prefix: #{prefix.inspect}")
      prefix = prefix.delete(UNUSABLE_CHARS)
      suffix &&= (String.try_convert(suffix) or
                  raise ArgumentError, "unexpected suffix: #{suffix.inspect}")
      suffix &&= suffix.delete(UNUSABLE_CHARS)
      begin
        t = Time.now.strftime("%Y%m%d")
        path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\
               "#{n ? %[-#{n}] : ''}#{suffix||''}"
        path = File.join(tmpdir, path)
        yield(path, n, opts, origdir)
      rescue Errno::EEXIST

View on GitHub (pinned to 0e5b888e1c)

Solutions

  1. Normalize empty to nil so the default Dir.tmpdir is used: dir = (s = ENV['TMPDIR']).to_s.empty? ? nil : s
  2. Validate config at load time: reject or default blank tmp_dir entries
  3. Unset rather than empty the variable in CI sanitizers: ENV.delete('TMPDIR') when blank
  4. Pass no dir argument when you have nothing meaningful to pass

Example fix

# before
Dir.mktmpdir("job-", ENV["TMPDIR"])    # TMPDIR is "" -> raises: empty parent path

# after
tmp = ENV["TMPDIR"]
tmp = nil if tmp.to_s.empty?
Dir.mktmpdir("job-", tmp)
Defensive patterns

Strategy: validation

Validate before calling

tmp = ENV['TMPDIR']
tmp = nil if tmp.to_s.empty?
Dir.mktmpdir('job-', tmp)

Try / catch

begin
  Dir.mktmpdir('job-', dir)
rescue ArgumentError => e
  raise unless e.message.include?('empty parent path')
  Dir.mktmpdir('job-')  # fall back to default tmpdir
end

Prevention

When it happens

Trigger: Dir.mktmpdir("x", ""); passing ENV['TMPDIR'] when it is set-but-empty (common in CI-scrubbed environments); blank config values interpolated into a dir argument.

Common situations: Kubernetes/CI env sanitizers that set TMPDIR="" instead of unsetting it; config where tmp_dir: is an empty string default; Pathname or String inputs that reduce to empty after trimming.

Related errors


AI-assisted analysis of ruby/ruby@0e5b888e1c (2026-08-21). Data as JSON: /api/errors/e6178fbd457092b9. Report an issue: GitHub.