Skip to content

Fix how Prism parses encoding from shebang args (#!)#11445

Merged
kddnewton merged 1 commit intoruby:masterfrom
Shopify:amomchilov/fix-shebang-parsing
Aug 28, 2024
Merged

Fix how Prism parses encoding from shebang args (#!)#11445
kddnewton merged 1 commit intoruby:masterfrom
Shopify:amomchilov/fix-shebang-parsing

Conversation

@amomchilov
Copy link
Copy Markdown
Contributor

@amomchilov amomchilov commented Aug 23, 2024

Fix the first of the two assertion failures here:

TestRubyOptions#test_shebang [/Users/alex/src/github.com/Ruby/ruby/test/ruby/test_rubyoptions.rb:514]:
pid 49518 exit 0.

1. [1/2] Assertion for "stdout"
   | <["\"\""]> expected but was
   | <["\"\\u3042\""]>.

2. [2/2] Assertion for "stderr"
   | Expected /shebang line ending with \\r/ to match "".

Finished tests in 0.030572s, 32.7097 tests/s, 359.8064 assertions/s.
1 tests, 11 assertions, 1 failures, 0 errors, 0 skips

ruby -v: ruby 3.4.0dev (2024-08-22T14:01:55Z master 56a34b5af5) +PRISM [arm64-darwin23]
make: *** [yes-test-all] Error 1

(The latter was fixed in ruby/prism#3007)

Fixes ruby/prism#2996

Comment thread ruby.c
pm_options_command_line_set(options, prism_script_command_line(opt));
if (opt->ext.enc.name != 0) {
pm_options_encoding_set(options, StringValueCStr(opt->ext.enc.name));
opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
Copy link
Copy Markdown
Contributor Author

@amomchilov amomchilov Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong place to set this, but just a proof-of-concept

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okok so this is a weird one.

When not using Prism, opt->ext.enc.name is set by proc_K_option(), but not index:

ruby/ruby.c

Lines 1322 to 1324 in efa4ec0

opt->src.enc.name = rb_str_new2(enc_name);
if (!opt->ext.enc.name)
opt->ext.enc.name = opt->src.enc.name;

Then, later on in load_file_internal(), the index is updated from the name, but only if no_ext_enc is true:

ruby/ruby.c

Lines 2703 to 2705 in efa4ec0

if (no_ext_enc && opt->ext.enc.name) {
opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
}

I tried modifying proc_K_option() to set both of these, but that doesn't work because it can't access the no_ext_enc variable to only set it conditionally.

This split source of truth (and that they aren't always perfectly in sync) is pretty confusing!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied the no_src_enc/no_ext_enc/no_int_enc logic exactly as-is from load_file_internal(). Would be nice to eventually clean up this split source of truth.

@amomchilov amomchilov force-pushed the amomchilov/fix-shebang-parsing branch from ac2cb37 to 1f640eb Compare August 23, 2024 23:48
@launchable-app

This comment has been minimized.

@amomchilov amomchilov force-pushed the amomchilov/fix-shebang-parsing branch 3 times, most recently from 494393d to 1c025f0 Compare August 27, 2024 20:19
@amomchilov amomchilov force-pushed the amomchilov/fix-shebang-parsing branch from 1c025f0 to d722e7f Compare August 27, 2024 20:54
@amomchilov amomchilov changed the title Fix prism #! parsing Fix how Prism parses encoding from #! Aug 27, 2024
@amomchilov amomchilov changed the title Fix how Prism parses encoding from #! Fix how Prism parses encoding from shebang args (#!) Aug 27, 2024
@amomchilov amomchilov marked this pull request as ready for review August 27, 2024 21:37
@kddnewton kddnewton merged commit f93c27d into ruby:master Aug 28, 2024
@kddnewton kddnewton deleted the amomchilov/fix-shebang-parsing branch August 28, 2024 12:47
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 this pull request may close these issues.

CRuby test failure: not setting external encoding on shebang flag

2 participants