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

eval handles non-standard shebang differently #2947

Closed
andrykonchin opened this issue Jul 22, 2024 · 0 comments · Fixed by #2952
Closed

eval handles non-standard shebang differently #2947

andrykonchin opened this issue Jul 22, 2024 · 0 comments · Fixed by #2952

Comments

@andrykonchin
Copy link
Member

andrykonchin commented Jul 22, 2024

When a file contains non-standard shebang (e.g. in a puma config), CRuby raises "no Ruby script found in input" SyntaxError but #eval seems ignores it all. But Prism raises the Syntax error in both cases.

test.rb:

#!/usr/bin/env puma

puts 'Hello'
Prism.parse File.read("test.rb"), scopes: [[], []]
# =>
# #<Prism::ParseResult:0x00000001018edd50
#  @comments=[#<Prism::InlineComment @location=#<Prism::Location @start_offset=0 @length=19 start_line=1>>],
#  @data_loc=nil,
#  @errors=
#   [#<Prism::ParseError @type=:script_not_found @message="no Ruby script found in input" @location=#<Prism::Location @start_offset=0 @length=0 start_line=1> @level=:load>],
#  @magic_comments=[],
#  @source=
#   #<Prism::ASCIISource:0x0000000101948778
#    @offsets=[0, 20, 21, 34],
#    @source="#!/usr/bin/env puma\n\nputs 'Hello'\n",
#    @start_line=1>,
#  @value=
#   @ ProgramNode (location: (3,0)-(3,12))
#   ├── flags: ∅
#   ├── locals: []
#   └── statements:
#       @ StatementsNode (location: (3,0)-(3,12))
#       ├── flags: ∅
#       └── body: (length: 1)
#           └── @ CallNode (location: (3,0)-(3,12))
#               ├── flags: newline, ignore_visibility
#               ├── receiver: ∅
#               ├── call_operator_loc: ∅
#               ├── name: :puts
#               ├── message_loc: (3,0)-(3,4) = "puts"
#               ├── opening_loc: ∅
#               ├── arguments:
#               │   @ ArgumentsNode (location: (3,5)-(3,12))
#               │   ├── flags: ∅
#               │   └── arguments: (length: 1)
#               │       └── @ StringNode (location: (3,5)-(3,12))
#               │           ├── flags: ∅
#               │           ├── opening_loc: (3,5)-(3,6) = "'"
#               │           ├── content_loc: (3,6)-(3,11) = "Hello"
#               │           ├── closing_loc: (3,11)-(3,12) = "'"
#               │           └── unescaped: "Hello"
#               ├── closing_loc: ∅
#               └── block: ∅,
#  @warnings=[]>
eval File.read("test.rb")
# => Hello

The standard Ruby shebang #!/usr/bin/env ruby is parsed by Prism without errors.

The issue was reported originally in oracle/truffleruby#3623

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 a pull request may close this issue.

1 participant