puppetlabs/puppet · error · ArgumentError

Working directory %{cwd} does not exist!

Error message

Working directory %{cwd} does not exist!

What it means

Puppet::Util.execute accepts a :cwd option to run the child process in a specific directory; before spawning it verifies the directory with Puppet::FileSystem.directory?. If :cwd is given but does not exist (or is not a directory), an ArgumentError is raised immediately and the command never runs.

Source

Thrown at lib/puppet/util/execution.rb:188

    end
    if options[:gid]
      user_log_s << " gid=#{options[:gid]}"
    end
    if user_log_s != ''
      user_log_s.prepend(' with')
    end

    if respond_to? :debug
      debug "Executing#{user_log_s}: '#{command_str}'"
    else
      Puppet.debug { "Executing#{user_log_s}: '#{command_str}'" }
    end

    null_file = Puppet::Util::Platform.windows? ? 'NUL' : '/dev/null'

    cwd = options[:cwd]
    if cwd && !Puppet::FileSystem.directory?(cwd)
      raise ArgumentError, _("Working directory %{cwd} does not exist!") % { cwd: cwd }
    end

    begin
      stdin = Puppet::FileSystem.open(options[:stdinfile] || null_file, nil, 'r')
      # On Windows, continue to use the file-based approach to avoid breaking people's existing
      # manifests. If they use a script that doesn't background cleanly, such as
      # `start /b ping 127.0.0.1`, we couldn't handle it with pipes as there's no non-blocking
      # read available.
      if options[:squelch]
        stdout = Puppet::FileSystem.open(null_file, nil, 'w')
      elsif Puppet.features.posix?
        reader, stdout = IO.pipe
      else
        stdout = Puppet::FileSystem::Uniquefile.new('puppet')
      end
      stderr = options[:combine] ? stdout : Puppet::FileSystem.open(null_file, nil, 'w')

      exec_args = [command, options, stdin, stdout, stderr]

View on GitHub (pinned to e227c27540)

Solutions

  1. Check that the directory exists before the call, and remember File.directory? follows symlinks (a dangling symlink fails)
  2. Create the directory first (mkdir -p) or use a path guaranteed to exist such as '/'
  3. Drop the :cwd option entirely if the child process does not actually need it

Example fix

# before
Puppet::Util.execute(['/usr/bin/git', 'pull'], cwd: '/srv/app/checkout')
# => ArgumentError: Working directory /srv/app/checkout does not exist!

# after
require 'fileutils'
FileUtils.mkdir_p('/srv/app/checkout')
Puppet::Util.execute(['/usr/bin/git', 'pull'], cwd: '/srv/app/checkout')
Defensive patterns

Strategy: validation

Validate before calling

cwd = '/srv/app/current'
raise ArgumentError, "cwd #{cwd} missing" unless cwd.nil? || File.directory?(cwd)
Puppet::Util.execute(cmd, cwd: cwd)

Prevention

When it happens

Trigger: Puppet::Util.execute(['git', 'pull'], cwd: '/srv/app/current') where the directory was removed (for example a dangling symlink after a deploy) or has not been created yet at that point in the run.

Common situations: Exec-style code referencing release directories that get rotated or removed during deploys, cwd pointing at a mount that is not mounted yet, and typo'd absolute paths in scripts.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/051be2161eb62fbf. Report an issue: GitHub.