Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate syntax error without require #139

Merged
merged 1 commit into from
May 23, 2022
Merged

Annotate syntax error without require #139

merged 1 commit into from
May 23, 2022

Conversation

schneems
Copy link
Collaborator

@schneems schneems commented May 18, 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 schneems force-pushed the schneems/ruby-32 branch 4 times, most recently from 0e0f4c8 to 4b9ac2f Compare May 23, 2022 17:16
@schneems schneems changed the title [close #31] Annotate syntax error without require Annotate syntax error without require 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 schneems marked this pull request as ready for review May 23, 2022 17:24
@schneems schneems merged commit a4cc0ed into main May 23, 2022
@schneems schneems deleted the schneems/ruby-32 branch May 23, 2022 17:24
schneems added a commit to schneems/ruby that referenced this pull request May 23, 2022
```
$ tool/sync_default_gems.rb dead_end
```

Incorporates changes from these prs:

- [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142)
- Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
schneems added a commit to schneems/ruby that referenced this pull request May 23, 2022
```
$ tool/sync_default_gems.rb dead_end
```

Incorporates changes from these prs:

- [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142)
- Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
schneems added a commit to schneems/ruby that referenced this pull request May 24, 2022
```
$ tool/sync_default_gems.rb dead_end
```

Incorporates changes from these prs:

- [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142)
- Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
schneems added a commit to schneems/ruby that referenced this pull request May 24, 2022
```
$ tool/sync_default_gems.rb dead_end
```

Incorporates changes from these prs:

- [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142)
- Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
schneems added a commit to schneems/ruby that referenced this pull request Jun 4, 2022
```
$ tool/sync_default_gems.rb dead_end
```

Incorporates changes from these prs:

- [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142)
- Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
schneems added a commit to schneems/ruby that referenced this pull request Jul 26, 2022
```
$ tool/sync_default_gems.rb dead_end
```

Incorporates changes from these prs:

- [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142)
- Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant