Skip to content

Conversation

@mame
Copy link
Member

@mame mame commented Feb 1, 2022

Also, the default error printer and Exception#full_message use the
method instead of Exception#message to get the message string.

Exception#detailed_message calls Exception#message, decorates and
returns the result. It adds some escape sequences to highlight, and the
class name of the exception to the end of the first line of the message.

See https://bugs.ruby-lang.org/issues/18564 for details.

mame added 8 commits February 2, 2022 15:50
Factor out from rb_error_write the responsibility to check if stderr is
a tty.
rb_decorate_message adds bold escape sequences to message, and the class
name of exception (like " (RuntimeError)) of "message (RuntimeError)").
Also, the default error printer and Exception#full_message use the
method instead of `Exception#message` to get the message string.

`Exception#detailed_message` calls `Exception#message`, decorates and
returns the result. It adds some escape sequences to highlight, and the
class name of the exception to the end of the first line of the message.

[Feature #18370]
.. even when the argument is not explicitly passed.
@mame mame force-pushed the detailed_message branch from e8c4426 to b63b65d Compare February 2, 2022 06:51
@mame mame requested a review from nobu February 2, 2022 06:52
@mame mame merged commit 25b0577 into ruby:master Feb 22, 2022
@mame mame deleted the detailed_message branch February 22, 2022 02:55
schneems added a commit to ruby/syntax_suggest that referenced this pull request May 18, 2022
As pointed out in #31 the current version of dead_end only works if the developer requires dead_end and then invokes `require`. This PR takes advantage of this change in Ruby 3.2 ruby/ruby#5516 to modify the `SyntaxError` directly. 

This behavior will only be fixed for Ruby 3.2+ 

Additionally we are still not able to handle the case where a program is streamed to ruby and does not exist on disk:

```
$ echo "def foo" | ruby
```

As the SyntaxError does not provide us with the contents of the script.

```
$ echo "def foo" | ruby
-:1: syntax error, unexpected end-of-input
def foo
```
schneems added a commit to ruby/syntax_suggest that referenced this pull request May 18, 2022
As pointed out in #31 the current version of dead_end only works if the developer requires dead_end and then invokes `require`. This PR takes advantage of this change in Ruby 3.2 ruby/ruby#5516 to modify the `SyntaxError` directly. 

This behavior will only be fixed for Ruby 3.2+ 

Additionally we are still not able to handle the case where a program is streamed to ruby and does not exist on disk:

```
$ echo "def foo" | ruby
```

As the SyntaxError does not provide us with the contents of the script.

```
$ echo "def foo" | ruby
-:1: syntax error, unexpected end-of-input
def foo
```
schneems added a commit to ruby/syntax_suggest that referenced this pull request May 18, 2022
As pointed out in #31 the current version of dead_end only works if the developer requires dead_end and then invokes `require`. This PR takes advantage of this change in Ruby 3.2 ruby/ruby#5516 to modify the `SyntaxError` directly. 

This behavior will only be fixed for Ruby 3.2+ 

Additionally we are still not able to handle the case where a program is streamed to ruby and does not exist on disk:

```
$ echo "def foo" | ruby
```

As the SyntaxError does not provide us with the contents of the script.

```
$ echo "def foo" | ruby
-:1: syntax error, unexpected end-of-input
def foo
```
schneems added a commit to ruby/syntax_suggest that referenced this pull request May 19, 2022
As pointed out in #31 the current version of dead_end only works if the developer requires dead_end and then invokes `require`. This PR takes advantage of this change in Ruby 3.2 ruby/ruby#5516 to modify the `SyntaxError` directly. 

This behavior will only be fixed for Ruby 3.2+ 

Additionally we are still not able to handle the case where a program is streamed to ruby and does not exist on disk:

```
$ echo "def foo" | ruby
```

As the SyntaxError does not provide us with the contents of the script.

```
$ echo "def foo" | ruby
-:1: syntax error, unexpected end-of-input
def foo
```
schneems added a commit to ruby/syntax_suggest that referenced this pull request May 23, 2022
Currently dead_end works by monkey patching require. This causes confusion and problems as other tools are not expecting this. For example zombocom/derailed_benchmarks#204 and #124. This PR utilizes the new SyntaxError#detailed_message as introduced in ruby/ruby#5516 that will be released in Ruby 3.2.

That means that developers using dead_end with Ruby 3.2+ will experience more consistent behavior.


## Limitations

As pointed out in #31 the current version of dead_end only works if the developer requires dead_end and then invokes `require`.

This behavior is still not fixed for Ruby 3.2+ 

```
$ ruby -v
ruby 3.2.0preview1 (2022-04-03 master f801386f0c) [x86_64-darwin20]
$ cat monkeypatch.rb
  SyntaxError.prepend Module.new {
    def detailed_message(highlight: nil, **)
      message = super
      message += "Monkeypatch worked\n"
      message
    end
  }

  # require_relative "bad.rb"
  # Note that i am commenting
  # out the require, but leaving
  # in the monkeypatch
⛄️ 3.2.0 🚀 /tmp
$ cat bad.rb
  def lol_i-am-a-synt^xerror
⛄️ 3.2.0 🚀 /tmp
$ ruby -r./monkeypatch.rb bad.rb
bad.rb:1: syntax error, unexpected '-', expecting ';' or '\n'
  def lol_i-am-a-synt^xerror
```

Additionally we are still not able to handle the case where a program is streamed to ruby and does not exist on disk:

```
$ echo "def foo" | ruby
```

As the SyntaxError does not provide us with the contents of the script.

```
$ echo "def foo" | ruby
-:1: syntax error, unexpected end-of-input
def foo
```
schneems added a commit to ruby/syntax_suggest that referenced this pull request May 23, 2022
Currently dead_end works by monkey patching require. This causes confusion and problems as other tools are not expecting this. For example zombocom/derailed_benchmarks#204 and #124. This PR utilizes the new SyntaxError#detailed_message as introduced in ruby/ruby#5516 that will be released in Ruby 3.2.

That means that developers using dead_end with Ruby 3.2+ will experience more consistent behavior.

## Limitations

As pointed out in #31 the current version of dead_end only works if the developer requires dead_end and then invokes `require`.

This behavior is still not fixed for Ruby 3.2+

```
$ ruby -v
ruby 3.2.0preview1 (2022-04-03 master f801386f0c) [x86_64-darwin20]
$ cat monkeypatch.rb
  SyntaxError.prepend Module.new {
    def detailed_message(highlight: nil, **)
      message = super
      message += "Monkeypatch worked\n"
      message
    end
  }

  # require_relative "bad.rb"
  # Note that i am commenting
  # out the require, but leaving
  # in the monkeypatch
⛄️ 3.2.0 🚀 /tmp
$ cat bad.rb
  def lol_i-am-a-synt^xerror
⛄️ 3.2.0 🚀 /tmp
$ ruby -r./monkeypatch.rb bad.rb
bad.rb:1: syntax error, unexpected '-', expecting ';' or '\n'
  def lol_i-am-a-synt^xerror
```

Additionally we are still not able to handle the case where a program is streamed to ruby and does not exist on disk:

```
$ echo "def foo" | ruby
```

As the SyntaxError does not provide us with the contents of the script.

```
$ echo "def foo" | ruby
-:1: syntax error, unexpected end-of-input
def foo
```
@schneems
Copy link
Contributor

schneems commented May 23, 2022

Thank you very much for your work! I am updating dead_end to use this new interface. However it looks like it does not trigger for code that is directly passed to the ruby CLI for catching a SyntaxError:

$ ruby -v
ruby 3.2.0preview1 (2022-04-03 master f801386f0c) [x86_64-darwin20]
$ cat monkeypatch.rb
  SyntaxError.prepend Module.new {
    def detailed_message(highlight: nil, **)
      message = super
      message += "Monkeypatch worked\n"
      message
    end
  }
$ cat bad.rb
  def lol_i-am-a-synt^xerror
$ ruby -r./monkeypatch.rb bad.rb
bad.rb:1: syntax error, unexpected '-', expecting ';' or '\n'
  def lol_i-am-a-synt^xerror

Expected: I expect to see "Monkeypatch worked" in the output.

Actual: I do not see it.

If I modify the code to require bad.rb from monkeypatch.rb instead, that will work. But I think it is also useful to be able to annotate files that are called directly by the CLI.

Edit: Linking my PR #5859

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants