{"record":{"id":"630d5fb4cccab332","repo":"jdx/mise","slug":"patch-failed-n-output","errorCode":null,"errorMessage":"patch failed:\\n#{output}","messagePattern":"patch failed:\\\\n#(.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/shim.rb","lineNumber":477,"sourceCode":"    if @data\n      content = @formula_file.read.split(/^__END__$/, 2)[1]\n      raise \"formula declares a DATA patch but has no __END__ section\" if content.nil?\n      apply_content(content.sub(/\\A\\n/, \"\"))\n    elsif @url\n      file = MiseDownload.fetch(@url, @sha256, \"patch\")\n      apply_content(file.read)\n    end\n  end\n\n  private\n\n  def apply_content(content)\n    ohai \"Applying patch (-p#{@strip})\"\n    Open3.popen2e(\"patch\", \"-g\", \"0\", \"-f\", \"-p#{@strip}\") do |stdin, out, thread|\n      stdin.write(content)\n      stdin.close\n      output = out.read\n      raise \"patch failed:\\n#{output}\" unless thread.value.success?\n      $stdout.puts output\n    end\n  end\nend\n\n# reference to an installed dependency, as returned by Formula[\"name\"]\nclass DependencyFormula\n  def initialize(name) = @name = name\n\n  def opt_prefix = MISE_BREW_PREFIX + \"opt\" + @name\n  def opt_bin = opt_prefix + \"bin\"\n  def opt_lib = opt_prefix + \"lib\"\n  def opt_include = opt_prefix + \"include\"\n  def opt_libexec = opt_prefix + \"libexec\"\n  def opt_share = opt_prefix + \"share\"\n  def opt_frameworks = opt_prefix + \"Frameworks\"\n\n  # resolved keg path (through the opt symlink)","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/jdx/mise/blob/afd2eddd3a50c16190efc1c7e94404b48f72af57/src/system/packages/brew/shim.rb#L459-L495","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Regenerate the patch against the exact source version being built","Adjust the strip level (`patch :p1` vs default) to match diff paths","Check whether the change already exists upstream and drop the patch","Read the raised output to see which hunk failed and fix paths/paths context"],"exampleFix":"# before\npatch do\n  url \"https://example.com/fix-old-version.patch\"\nend\n# after\n# regenerate patch against the current source version\npatch do\n  url \"https://example.com/fix-v2.1.0.patch\"\n  sha256 \"<new checksum>\"\nend","handlingStrategy":"try-catch","validationCode":"# check that patch context matches source: apply with --dry-run first\nsystem(\"patch\", \"--dry-run\", \"-p1\", \"-i\", patch_file) or abort \"patch will not apply\"","typeGuard":null,"tryCatchPattern":"begin\n  formula.patch!\nrescue RuntimeError => e\n  raise if !e.message.start_with?(\"patch failed\")\n  warn e.message # includes patch tool output identifying the failing hunk\nend","preventionTips":["Regenerate patches on every upstream version bump","Use --dry-run externally to pre-validate patches","Keep strip levels consistent with diff path prefixes","Check whether the patch is already applied before applying"],"tags":["patch","subprocess","ruby"],"backgroundTag":"patch-command-failed","analyzedSha":"afd2eddd3a50c16190efc1c7e94404b48f72af57","analyzedAt":"2026-09-09T01:38:25.179Z","contentChangedAt":"2026-09-09T01:38:25.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}