jdx/mise · error · RuntimeError

inreplace: missing before/after

Error message

inreplace: missing before/after

What it means

The Homebrew shim's inreplace replacement was invoked with neither before/after strings nor a block — there is no substitution to perform. It raises (plain Ruby raise, not mise) because silently doing nothing would let a build proceed with unpatched files.

Solutions

  1. Pass explicit before/after strings: `inreplace "config.mk", "-O2", "-O3"`.
  2. Or use block form: `inreplace "config.mk" do |s| s.gsub!(...) end`.
  3. Guard the computed before-value for nil before calling inreplace.

Example fix

# before
inreplace "Makefile", nil, "x"
# after
inreplace "Makefile", "CC = gcc", "CC = clang"
Defensive patterns

Strategy: validation

Validate before calling

raise "before pattern is nil" if before.nil? && !block_given?

Try / catch

begin
  inreplace path, before, after
rescue RuntimeError => e
  raise unless e.message == "inreplace: missing before/after"
  inreplace(path) { |s| s.gsub!(pattern, replacement) }
end

Prevention

When it happens

Trigger: Calling `inreplace path` with no second argument and no block, e.g. `inreplace "config.mk"` — typically a refactor dropped the pattern arguments or a nil variable was passed as `before`.

Common situations: Formula edits where the before-string is computed dynamically and turned out nil; misremembering inreplace's signature (before/after required unless using block form).

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@533346cc37 (2026-09-17). Data as JSON: /api/errors/dbb3ed6cd028ad5e. Report an issue: GitHub.

Appendix: 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 533346cc37)