jdx/mise · error · RuntimeError

inreplace: missing before/after

Error message

inreplace: missing before/after

What it means

The shim's inreplace helper (like Homebrew's) rewrites file content. When called with neither a before/after pair nor a mutating block, there is nothing to substitute, so it raises rather than silently no-oping.

Source

Thrown at src/system/packages/brew/shim.rb:770

  def which(cmd)
    ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).each do |dir|
      candidate = Pathname.new(dir) + cmd.to_s
      return candidate if candidate.executable? && candidate.file?
    end
    nil
  end

  def inreplace(paths, before = nil, after = nil, audit_result = true, &block)
    Array(paths).each do |path|
      path = Pathname.new(path.to_s)
      content = path.read
      replaced = content.dup
      if block
        ext = InreplaceText.new(replaced)
        block.call(ext)
        replaced = ext.text
      else
        raise "inreplace: missing before/after" if before.nil?
        case before
        when Regexp then replaced.gsub!(before, after.to_s)
        else replaced = replaced.gsub(before.to_s, after.to_s)
        end
      end
      if audit_result && replaced == content
        raise "inreplace in #{path} made no substitutions — the formula may need updating"
      end
      path.write(replaced)
    end
  end

  def cd(path, &block) = Dir.chdir(path.to_s, &block)

  def mkdir(name, &block)
    path = Pathname.new(name.to_s)
    path.mkpath
    block ? Dir.chdir(path.to_s, &block) : path

View on GitHub (pinned to afd2eddd3a)

Solutions

  1. Provide before/after: inreplace f, "old", "new"
  2. Or pass a block: inreplace f { |s| s.change_make_var! "PREFIX", prefix }
  3. Fix argument ordering so the search string reaches the before parameter

Example fix

# before
inreplace "Makefile"
# after
inreplace "Makefile", "/usr/local", prefix
Defensive patterns

Strategy: validation

Validate before calling

raise "inreplace needs before/after or block" if before.nil? && !block_given?

Prevention

When it happens

Trigger: Calling inreplace(path) with no args and no block; passing before: nil explicitly; refactorings that dropped the block argument; formula ports using a DSL variant not supported by the shim.

Common situations: Ported Homebrew formulas using newer inreplace signatures; accidental arg reordering so before lands in an unexpected parameter; template-generated formula code omitting the replacement spec.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of jdx/mise@afd2eddd3a (2026-09-09). Data as JSON: /api/errors/dbb3ed6cd028ad5e. Report an issue: GitHub.