Skip to content

Commit

Permalink
[GR-54907] Backport to 24.1: Fix Kernel#eval with non-Ruby shebang
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4343
  • Loading branch information
andrykonchin authored and ansalond committed Aug 15, 2024
2 parents 0ac30d1 + 88c997e commit cebc954
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Compatibility:
* Fix `rb_set_errinfo` and `rb_errinfo` and store an error separately from `$!` (#2890, @andrykonchin).
* Fix `rb_mutex_synchronize` to not wrap/unwrap result value (#3624, @andrykonchin).
* Add `StringIO#set_encoding_by_bom` method (#3632, @andrykonchin).
* Fix `Kernel#eval` to ignore shebang with non-Ruby interpreter (#3623, @andrykonchin).

Performance:

Expand Down
20 changes: 20 additions & 0 deletions spec/ruby/core/kernel/eval_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ class EvalSpecs
eval("").should == nil
end

context "with shebang" do
it "ignores shebang with ruby interpreter" do
pid = eval(<<~CODE.b)
#!/usr/bin/env ruby
Process.pid
CODE

pid.should == Process.pid
end

it "ignores shebang with non-ruby interpreter" do
pid = eval(<<~CODE.b)
#!/usr/bin/env puma
Process.pid
CODE

pid.should == Process.pid
end
end

# See language/magic_comment_spec.rb for more magic comments specs
describe "with a magic encoding comment" do
it "uses the magic comment encoding for the encoding of literal strings" do
Expand Down
2 changes: 1 addition & 1 deletion src/main/c/yarp/src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -21467,7 +21467,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
pm_parser_warn_shebang_carriage_return(parser, parser->start, length);
if (newline != NULL) parser->encoding_comment_start = newline + 1;
search_shebang = false;
} else {
} else if (!parser->parsing_eval) { // See https://github.com/ruby/prism/pull/2952
search_shebang = true;
}
}
Expand Down

0 comments on commit cebc954

Please sign in to comment.