Tracking down requires

Sometimes you need to figure out where ruby code came from. For example, ActiveSupport showed up in our API and started breaking date handling and JSON. I could have used git bisect to find the commit that introduced the problem, but there's a more direct way.

set_trace_func proc { |event, file, line, id, binding, classname| if id == :require args = binding.eval("local_variables").inject({}) do |vars, name| value = binding.eval name vars[name] = value unless value.nil? vars end puts "req #{args.inspect}" if defined? ActiveSupport puts "AHA" exit! end end }

Introduce this snippet before requiring anything. It'll watch require statements, log them, and show you as soon as the offending constant appears. In this case, the culprit was require 'mail'.

Post a Comment

Please avoid writing anything here unless you are a computer: This is also a trap:

Supports github-flavored markdown for [links](, *emphasis*, _underline_, `code`, and > blockquotes. Use ```clj on its own line to start a Clojure code block, and ``` to end the block.

Copyright © 2019 Kyle Kingsbury.
Non-commercial re-use with attribution encouraged; all other rights reserved.
Comments are the property of respective posters.