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

IRB 1.2.1 (2019-12-24) (ruby) not compatible byexample #162

Closed
eldipa opened this issue Apr 22, 2021 · 7 comments · Fixed by #173
Closed

IRB 1.2.1 (2019-12-24) (ruby) not compatible byexample #162

eldipa opened this issue Apr 22, 2021 · 7 comments · Fixed by #173
Labels
bug out of sync between the code and the docs; something that should be fixed in the near future
Milestone

Comments

@eldipa
Copy link
Collaborator

eldipa commented Apr 22, 2021

Describe the bug

root@7a2b065b0459:/mnt# byexample -l shell,ruby --timeout 20 docs/languages/ruby.md 
[w] Initialization of Ruby Runner failed.
[!] Something went wrong processing the file 'docs/languages/ruby.md':
Prompt not found: the code is taking too long to finish or there is a syntax error.

Last 1000 bytes read:
▽

Rerun with -vvv to get a full stack trace.
^[[38;2Rroot@7a2b065b0459:/mnt# ;2R

IRB/pexpect breaks in a really weird way. It seems that IRB never prints the prompts which makes byexample time out and instead, it prints some weird symbols/bytes.

Expected behavior
It should work.

byexample version
byexample 10.0.1 (Python 3.8.5) - GNU GPLv3
irb 1.2.1 (2019-12-24)
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]

irb(main):005:0> Reline::VERSION
=> "0.1.2"

reline is the new lib used by irb to implement the prompt which is probably the reason of this problem.

May not be related but:
https://github.com/ruby/reline/blob/master/lib/reline.rb
ruby/reline#207
https://github.com/ruby/reline/blob/d1a7e74aa432ca1dda4e94aa1389f22c08b59c58/lib/reline/line_editor.rb#L530

@eldipa eldipa added the bug out of sync between the code and the docs; something that should be fixed in the near future label Apr 22, 2021
@fnordfish
Copy link

I'm using byexample -l ruby --options="-ruby-pretty-print" -x-shebang "ruby:%e %p -f --nocolorize %a"

@eldipa
Copy link
Collaborator Author

eldipa commented May 20, 2021

@fnordfish thanks for the tip!

I'm testing your solution but I'm still getting the same error (same version of ruby, irb and byexample than the ones in the initial description of this ticket).

However, I ran byexample successfully as follows:

byexample -l ruby -x-shebang "ruby:%e %p --nocolorize --nomultiline %a" a.md

So a lot of thanks!

Could you confirm me that that my setting works for you?

I will add it as fix in the next version (I'm going to take some time before the release because I need to figure out how to do the change without breaking older versions of irb).

As a side note I saw that you opened an issue in the IRB project (ruby/irb#241) about a spurious new line after the => marker.

You can workaround it quickly with something like:

>> somecode
=><...>expected output

Or,

>> somecode    # byexample: +norm-ws
=> expected output

Unfortunately I couldn't trigger it in my environment so I couldn't test it (I think that the issue happens only in IRB >= 1.3.0).

I will keep an eye on that issue: if there is not response from the IRB developers I will try to add a workaround in byexample.

@fnordfish
Copy link

fnordfish commented May 20, 2021

I've added the -f to make sure ~/.irbrc is ignored. For completeness, here's the shell script I've been using:

byexample -l ruby \
--options="-ruby-pretty-print" \
-x-shebang "ruby:%e %p -f --nomultiline --nocolorize %a -r bundler/setup -r ./_pages/docs/docs_base.rb -r my_lib" \
 $@ \
$(ruby -e 'puts Dir["_pages/**/*.{rb,md}"]')

(I know, I could have used find for the file patterns, but didn't want to care about potential differences between different operating systems)

Because I'm lazy, I've added those -r directly into my script to load the library code needed to test the docs.

@fnordfish
Copy link

fnordfish commented May 20, 2021

BTW: Thanks for workaround, but I'd rather pin my irb dependency to a working version (1.3.0 is fine. 1.3.1 to 1.3.5 have that weird extra new line)
Just tested my script on ruby 2.7.0 and it's working fine.

Little note on irb dependency pin using bundler:

With a Gemfile like this:

# Gemfile
# ...
group :development, :test do
  gem "irb", ">= 1.2.7", "< 1.3.1", platform: :mri
end

I have to use bundle exec in the shebang, to make sure the correct irb version is used:
-x-shebang "ruby:bundle exec %e %p ${irb_opts} %a ${irb_r}"
Again, the bundle exec is only needed, if you're installing ruby dependencies via bundler.

If you just install a specific irb version manually like gem install irb -v 1.3.0, you can use a shebang like this:
-x-shebang "ruby:%e %p _1.3.0_ ${irb_opts} %a ${irb_r}"

eldipa added a commit that referenced this issue May 23, 2021
We found that in irb >= 1.2.0 the interpreter outputs nonsense.

The use of `-f`, `--nomultiline`, `--nocolorize` and `--noreadline`
fixes the problem (suggested by @fnordfish).

Because `--nomultiline` and `--nocolorize` are not valid options for irb
< 1.2.0, we require to detect the version of irb to properly choose the
correct options.

Closes #162 and closes #172
eldipa added a commit that referenced this issue May 24, 2021
Allow the user to control if he/she wants the older behavior of irb
(and default for byexample) or the new one where a newline is added
after the => marker when the output is large.

To maintain backward compatibility, byexamples sets this flag to false.

This closes #162.
@eldipa
Copy link
Collaborator Author

eldipa commented May 24, 2021

I found that IRB >= 1.2.2 added on purpose the newline after the => marker if the output is multiline.

The feature was introduced in this commit: https://github.com/ruby/irb/blob/2eb7d9cdca63a443ce220debe1b9669ffdd926db/lib/irb/context.rb#L274-L285

The idea is to fix this from byexample restoring the behavior of IRB < 1.2.2 (see cc226a7) and allowing the user to switch to the newer behavior of IRB >= 1.2.2 if he/she wants (see e0916fa)

@eldipa
Copy link
Collaborator Author

eldipa commented May 24, 2021

@fnordfish I released byexample 10.0.4. This issue and the issue of the "unexpected newline" (ruby/irb#241) should be fixed by now.

Let me know if you find any trouble.

By the way, since 10.0.3 you can use "file patterns" to select which files you want to execute.

For example:

$ byexample -l ruby '_pages/**/*.rb'  '_pages/**/*.md'

You need to protect those patterns with single quotes so it is byexample and not the shell who will interpret them. They are not as powerful as Ruby's (this will not work: _pages/**/*.{rb,md}) but something is something.

@fnordfish
Copy link

Brilliant. Thanks! Will give at a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug out of sync between the code and the docs; something that should be fixed in the near future
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants