Skip to content

Commit cebc954

Browse files
andrykonchinansalond
authored andcommitted
[GR-54907] Backport to 24.1: Fix Kernel#eval with non-Ruby shebang
PullRequest: truffleruby/4343
2 parents 0ac30d1 + 88c997e commit cebc954

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Compatibility:
4545
* Fix `rb_set_errinfo` and `rb_errinfo` and store an error separately from `$!` (#2890, @andrykonchin).
4646
* Fix `rb_mutex_synchronize` to not wrap/unwrap result value (#3624, @andrykonchin).
4747
* Add `StringIO#set_encoding_by_bom` method (#3632, @andrykonchin).
48+
* Fix `Kernel#eval` to ignore shebang with non-Ruby interpreter (#3623, @andrykonchin).
4849

4950
Performance:
5051

spec/ruby/core/kernel/eval_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ class EvalSpecs
274274
eval("").should == nil
275275
end
276276

277+
context "with shebang" do
278+
it "ignores shebang with ruby interpreter" do
279+
pid = eval(<<~CODE.b)
280+
#!/usr/bin/env ruby
281+
Process.pid
282+
CODE
283+
284+
pid.should == Process.pid
285+
end
286+
287+
it "ignores shebang with non-ruby interpreter" do
288+
pid = eval(<<~CODE.b)
289+
#!/usr/bin/env puma
290+
Process.pid
291+
CODE
292+
293+
pid.should == Process.pid
294+
end
295+
end
296+
277297
# See language/magic_comment_spec.rb for more magic comments specs
278298
describe "with a magic encoding comment" do
279299
it "uses the magic comment encoding for the encoding of literal strings" do

src/main/c/yarp/src/prism.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21467,7 +21467,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
2146721467
pm_parser_warn_shebang_carriage_return(parser, parser->start, length);
2146821468
if (newline != NULL) parser->encoding_comment_start = newline + 1;
2146921469
search_shebang = false;
21470-
} else {
21470+
} else if (!parser->parsing_eval) { // See https://github.com/ruby/prism/pull/2952
2147121471
search_shebang = true;
2147221472
}
2147321473
}

0 commit comments

Comments
 (0)