{"record":{"id":"dbb3ed6cd028ad5e","repo":"jdx/mise","slug":"inreplace-missing-before-after","errorCode":null,"errorMessage":"inreplace: missing before/after","messagePattern":"inreplace: missing before/after","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/shim.rb","lineNumber":770,"sourceCode":"  def which(cmd)\n    ENV.fetch(\"PATH\", \"\").split(File::PATH_SEPARATOR).each do |dir|\n      candidate = Pathname.new(dir) + cmd.to_s\n      return candidate if candidate.executable? && candidate.file?\n    end\n    nil\n  end\n\n  def inreplace(paths, before = nil, after = nil, audit_result = true, &block)\n    Array(paths).each do |path|\n      path = Pathname.new(path.to_s)\n      content = path.read\n      replaced = content.dup\n      if block\n        ext = InreplaceText.new(replaced)\n        block.call(ext)\n        replaced = ext.text\n      else\n        raise \"inreplace: missing before/after\" if before.nil?\n        case before\n        when Regexp then replaced.gsub!(before, after.to_s)\n        else replaced = replaced.gsub(before.to_s, after.to_s)\n        end\n      end\n      if audit_result && replaced == content\n        raise \"inreplace in #{path} made no substitutions — the formula may need updating\"\n      end\n      path.write(replaced)\n    end\n  end\n\n  def cd(path, &block) = Dir.chdir(path.to_s, &block)\n\n  def mkdir(name, &block)\n    path = Pathname.new(name.to_s)\n    path.mkpath\n    block ? Dir.chdir(path.to_s, &block) : path","sourceCodeStart":752,"sourceCodeEnd":788,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/packages/brew/shim.rb#L752-L788","documentation":"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.","triggerScenarios":"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`.","commonSituations":"Formula edits where the before-string is computed dynamically and turned out nil; misremembering inreplace's signature (before/after required unless using block form).","solutions":["Pass explicit before/after strings: `inreplace \"config.mk\", \"-O2\", \"-O3\"`.","Or use block form: `inreplace \"config.mk\" do |s| s.gsub!(...) end`.","Guard the computed before-value for nil before calling inreplace."],"exampleFix":"# before\ninreplace \"Makefile\", nil, \"x\"\n# after\ninreplace \"Makefile\", \"CC = gcc\", \"CC = clang\"","handlingStrategy":"validation","validationCode":"raise \"before pattern is nil\" if before.nil? && !block_given?","typeGuard":null,"tryCatchPattern":"begin\n  inreplace path, before, after\nrescue RuntimeError => e\n  raise unless e.message == \"inreplace: missing before/after\"\n  inreplace(path) { |s| s.gsub!(pattern, replacement) }\nend","preventionTips":["Never pass computed patterns without a nil check","Use block form for dynamic substitutions","Check inreplace arity when porting Homebrew code"],"tags":["ruby","brew-shim","inreplace","api-usage"],"backgroundTag":"missing-required-argument","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}