{"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/afd2eddd3a50c16190efc1c7e94404b48f72af57/src/system/packages/brew/shim.rb#L752-L788","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide before/after: inreplace f, \"old\", \"new\"","Or pass a block: inreplace f { |s| s.change_make_var! \"PREFIX\", prefix }","Fix argument ordering so the search string reaches the before parameter"],"exampleFix":"# before\ninreplace \"Makefile\"\n# after\ninreplace \"Makefile\", \"/usr/local\", prefix","handlingStrategy":"validation","validationCode":"raise \"inreplace needs before/after or block\" if before.nil? && !block_given?","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass either before/after or a block to inreplace","Check the shim's supported inreplace signatures when porting formulas","Centralize file edits in helpers so signatures stay consistent"],"tags":["ruby","inreplace","formula"],"backgroundTag":"missing-required-argument","analyzedSha":"afd2eddd3a50c16190efc1c7e94404b48f72af57","analyzedAt":"2026-09-09T01:38:25.179Z","contentChangedAt":"2026-09-09T01:38:25.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}