From 15135030e5808d527325feaaaf04caeb1b44f8b5 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 11 Sep 2024 16:47:14 -0400 Subject: [PATCH] [ruby/prism] Do not warn \r in shebang on windows https://github.com/ruby/prism/commit/e8c862ca1f --- prism/prism.c | 13 +++--- test/prism/result/warnings_test.rb | 64 ++++++++++++++++-------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/prism/prism.c b/prism/prism.c index 0dcc4afe5d..b3d843e672 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -21772,6 +21772,9 @@ pm_strnstr(const char *big, const char *little, size_t big_length) { return NULL; } +#ifdef _WIN32 +#define pm_parser_warn_shebang_carriage_return(parser, start, length) ((void) 0) +#else /** * Potentially warn the user if the shebang that has been found to include * "ruby" has a carriage return at the end, as that can cause problems on some @@ -21783,6 +21786,7 @@ pm_parser_warn_shebang_carriage_return(pm_parser_t *parser, const uint8_t *start pm_parser_warn(parser, start, start + length, PM_WARN_SHEBANG_CARRIAGE_RETURN); } } +#endif /** * Process the shebang when initializing the parser. This function assumes that @@ -21973,9 +21977,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm const char *engine; if ((engine = pm_strnstr((const char *) parser->start, "ruby", length)) != NULL) { if (newline != NULL) { - size_t length_including_newline = length + 1; - pm_parser_warn_shebang_carriage_return(parser, parser->start, length_including_newline); - + pm_parser_warn_shebang_carriage_return(parser, parser->start, length + 1); parser->encoding_comment_start = newline + 1; } @@ -22015,10 +22017,9 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm const char *engine; if ((engine = pm_strnstr((const char *) cursor, "ruby", length)) != NULL) { found_shebang = true; - if (newline != NULL) { - size_t length_including_newline = length + 1; - pm_parser_warn_shebang_carriage_return(parser, cursor, length_including_newline); + if (newline != NULL) { + pm_parser_warn_shebang_carriage_return(parser, cursor, length + 1); parser->encoding_comment_start = newline + 1; } diff --git a/test/prism/result/warnings_test.rb b/test/prism/result/warnings_test.rb index aeac7f80e6..861f6d9999 100644 --- a/test/prism/result/warnings_test.rb +++ b/test/prism/result/warnings_test.rb @@ -321,42 +321,48 @@ module Prism assert_warning("tap { redo; foo }", "statement not reached") end - def test_shebang_ending_with_carriage_return - msg = "shebang line ending with \\r may cause problems" + if RbConfig::CONFIG["host_os"].match?(/bccwin|cygwin|djgpp|mingw|mswin|wince/i) + def test_shebang_ending_with_carriage_return + refute_warning("#!ruby\r\np(123)\n", compare: false) + end + else + def test_shebang_ending_with_carriage_return + msg = "shebang line ending with \\r may cause problems" - assert_warning(<<~RUBY, msg, compare: false) - #!ruby\r - p(123) - RUBY + assert_warning(<<~RUBY, msg, compare: false) + #!ruby\r + p(123) + RUBY - assert_warning(<<~RUBY, msg, compare: false) - #!ruby \r - p(123) - RUBY + assert_warning(<<~RUBY, msg, compare: false) + #!ruby \r + p(123) + RUBY - assert_warning(<<~RUBY, msg, compare: false) - #!ruby -Eutf-8\r - p(123) - RUBY + assert_warning(<<~RUBY, msg, compare: false) + #!ruby -Eutf-8\r + p(123) + RUBY - # Used with the `-x` object, to ignore the script up until the first shebang that mentioned "ruby". - assert_warning(<<~SCRIPT, msg, compare: false) - #!/usr/bin/env bash - # Some initial shell script or other content - # that Ruby should ignore - echo "This is shell script part" - exit 0 + # Used with the `-x` object, to ignore the script up until the first shebang that mentioned "ruby". + assert_warning(<<~SCRIPT, msg, compare: false) + #!/usr/bin/env bash + # Some initial shell script or other content + # that Ruby should ignore + echo "This is shell script part" + exit 0 - #! /usr/bin/env ruby -Eutf-8\r - # Ruby script starts here - puts "Hello from Ruby!" - SCRIPT + #! /usr/bin/env ruby -Eutf-8\r + # Ruby script starts here + puts "Hello from Ruby!" + SCRIPT - refute_warning("#ruby not_a_shebang\r\n", compare: false) + refute_warning("#ruby not_a_shebang\r\n", compare: false) - # CRuby doesn't emit the warning if a malformed file only has `\r` and not `\n`. - # https://bugs.ruby-lang.org/issues/20700 - refute_warning("#!ruby\r", compare: false) + # CRuby doesn't emit the warning if a malformed file only has `\r` and not `\n`. + # https://bugs.ruby-lang.org/issues/20700 + refute_warning("#!ruby\r", compare: false) + end end def test_warnings_verbosity