Issue #12026 has been updated by Jeremy Evans.


Shyouhei Urabe wrote:
> Yes we discussed this at the meeting, and someone pointed out that this request has something to do with https://bugs.ruby-lang.org/issues/11588.  Everyone wants to control which warning to show and which to avoid.  Proposed ways vary from a heavyweight complex mechanism to a lightweight grep-like filtering.
> 
> Maybe ruby should provide a primitive to cook warning strings before they are displayed.  That way you can write your filter on top of it, or parse JSON-formatted warning string to operate, and so on.

One simple way to do this would be to add something like $WARNING_PROCESSOR, which would be any callable object, and if present it is called with all warnings and the result is displayed (or nothing is displayed if nil is returned).  Here's an example if you just wanted a filter:

~~~
$WARNING_PROCESSOR = proc do |warning|
  unless warning =~ %r{/gem_name(-[\d\.]+)?/lib/.+.rb:\d+: warning: instance variable \@\w+ not initialized\Z}
    warning
  end
end
~~~

This would make it simple to do warning message deduping using a hash (as requested by naruse):

~~~
seen_warnings = {}
$WARNING_PROCESSOR = proc do |warning|
  unless seen_warnings[warning]
    seen_warnings[warning] = true
    warning
  end
end
~~~

As well as allow arbitrary code to handle warnings, which should fix ko1's objection and handle your example of JSON output:

~~~
$WARNING_PROCESSOR = proc do |warning|
  if warning =~ %r{/\A(.+\.rb):(\d+): warning: (.+)\Z}
    {'file'=>$1, 'line'=>$2, 'warning'=>$3}.to_json
  else
    {'warning_line'=>warning}.to_json
  end
end
~~~

I can work on an implementation of this if the response is positive.

----------------------------------------
Feature #12026: Support warning filters
https://bugs.ruby-lang.org/issues/12026#change-57546

* Author: Jeremy Evans
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This adds a simple way to filter warnings from being displayed. You
just set a $WARNING_FILTER with a regexp, and any warnings that
match the regexp will not be displayed.

I think this is a much simpler approach to filtering warnings than
feature #11588, while allowing the same type of capability.

This is backwards compatible, in that you can set $WARNING_FILTER in
previous versions of ruby without problems.

This should not cause any performance issues, as the regexp matching
isn't done until the warning message is about to be displayed.

It's possible to use something other than a global variable to store
the warning filter, but a global variable was the easiest way to
implement it, it has a global effect, and similar flags such as
$VERBOSE are also global variables, so I think a global variable
makes sense.

---Files--------------------------------
0001-Support-warning-filters.patch (4.12 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>