jdx/mise · error · RuntimeError

patch failed:\n#{output}

Error message

patch failed:\n#{output}

What it means

apply_content pipes patch content into the external `patch` program (patch -g 0 -f -p<strip>). It raises with the patch tool's captured output when that process exits non-zero, i.e. the unified diff did not apply cleanly.

Source

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

    if @data
      content = @formula_file.read.split(/^__END__$/, 2)[1]
      raise "formula declares a DATA patch but has no __END__ section" if content.nil?
      apply_content(content.sub(/\A\n/, ""))
    elsif @url
      file = MiseDownload.fetch(@url, @sha256, "patch")
      apply_content(file.read)
    end
  end

  private

  def apply_content(content)
    ohai "Applying patch (-p#{@strip})"
    Open3.popen2e("patch", "-g", "0", "-f", "-p#{@strip}") do |stdin, out, thread|
      stdin.write(content)
      stdin.close
      output = out.read
      raise "patch failed:\n#{output}" unless thread.value.success?
      $stdout.puts output
    end
  end
end

# reference to an installed dependency, as returned by Formula["name"]
class DependencyFormula
  def initialize(name) = @name = name

  def opt_prefix = MISE_BREW_PREFIX + "opt" + @name
  def opt_bin = opt_prefix + "bin"
  def opt_lib = opt_prefix + "lib"
  def opt_include = opt_prefix + "include"
  def opt_libexec = opt_prefix + "libexec"
  def opt_share = opt_prefix + "share"
  def opt_frameworks = opt_prefix + "Frameworks"

  # resolved keg path (through the opt symlink)

View on GitHub (pinned to afd2eddd3a)

Solutions

  1. Regenerate the patch against the exact source version being built
  2. Adjust the strip level (`patch :p1` vs default) to match diff paths
  3. Check whether the change already exists upstream and drop the patch
  4. Read the raised output to see which hunk failed and fix paths/paths context

Example fix

# before
patch do
  url "https://example.com/fix-old-version.patch"
end
# after
# regenerate patch against the current source version
patch do
  url "https://example.com/fix-v2.1.0.patch"
  sha256 "<new checksum>"
end
Defensive patterns

Strategy: try-catch

Validate before calling

# check that patch context matches source: apply with --dry-run first
system("patch", "--dry-run", "-p1", "-i", patch_file) or abort "patch will not apply"

Try / catch

begin
  formula.patch!
rescue RuntimeError => e
  raise if !e.message.start_with?("patch failed")
  warn e.message # includes patch tool output identifying the failing hunk
end

Prevention

When it happens

Trigger: Diff context lines no longer match the upstream source (formula version mismatch); wrong strip level (-p0/-p1); patch already applied (reverse or already-present changes); paths in the diff don't match the working directory layout.

Common situations: Upstream bumped a source version so the patch hunks no longer apply; formula reused a patch across versions; running from the wrong directory so relative paths in the diff resolve incorrectly.

Related errors


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