Skip to content

Commit e8c862c

Browse files
committedSep 11, 2024
Do not warn \r in shebang on windows
1 parent 44b1462 commit e8c862c

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed
 

‎src/prism.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21772,6 +21772,9 @@ pm_strnstr(const char *big, const char *little, size_t big_length) {
2177221772
return NULL;
2177321773
}
2177421774

21775+
#ifdef _WIN32
21776+
#define pm_parser_warn_shebang_carriage_return(parser, start, length) ((void) 0)
21777+
#else
2177521778
/**
2177621779
* Potentially warn the user if the shebang that has been found to include
2177721780
* "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
2178321786
pm_parser_warn(parser, start, start + length, PM_WARN_SHEBANG_CARRIAGE_RETURN);
2178421787
}
2178521788
}
21789+
#endif
2178621790

2178721791
/**
2178821792
* 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
2197321977
const char *engine;
2197421978
if ((engine = pm_strnstr((const char *) parser->start, "ruby", length)) != NULL) {
2197521979
if (newline != NULL) {
21976-
size_t length_including_newline = length + 1;
21977-
pm_parser_warn_shebang_carriage_return(parser, parser->start, length_including_newline);
21978-
21980+
pm_parser_warn_shebang_carriage_return(parser, parser->start, length + 1);
2197921981
parser->encoding_comment_start = newline + 1;
2198021982
}
2198121983

@@ -22015,10 +22017,9 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
2201522017
const char *engine;
2201622018
if ((engine = pm_strnstr((const char *) cursor, "ruby", length)) != NULL) {
2201722019
found_shebang = true;
22018-
if (newline != NULL) {
22019-
size_t length_including_newline = length + 1;
22020-
pm_parser_warn_shebang_carriage_return(parser, cursor, length_including_newline);
2202122020

22021+
if (newline != NULL) {
22022+
pm_parser_warn_shebang_carriage_return(parser, cursor, length + 1);
2202222023
parser->encoding_comment_start = newline + 1;
2202322024
}
2202422025

‎test/prism/result/warnings_test.rb

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -321,42 +321,48 @@ def test_unreachable_statement
321321
assert_warning("tap { redo; foo }", "statement not reached")
322322
end
323323

324-
def test_shebang_ending_with_carriage_return
325-
msg = "shebang line ending with \\r may cause problems"
326-
327-
assert_warning(<<~RUBY, msg, compare: false)
328-
#!ruby\r
329-
p(123)
330-
RUBY
331-
332-
assert_warning(<<~RUBY, msg, compare: false)
333-
#!ruby \r
334-
p(123)
335-
RUBY
336-
337-
assert_warning(<<~RUBY, msg, compare: false)
338-
#!ruby -Eutf-8\r
339-
p(123)
340-
RUBY
341-
342-
# Used with the `-x` object, to ignore the script up until the first shebang that mentioned "ruby".
343-
assert_warning(<<~SCRIPT, msg, compare: false)
344-
#!/usr/bin/env bash
345-
# Some initial shell script or other content
346-
# that Ruby should ignore
347-
echo "This is shell script part"
348-
exit 0
349-
350-
#! /usr/bin/env ruby -Eutf-8\r
351-
# Ruby script starts here
352-
puts "Hello from Ruby!"
353-
SCRIPT
354-
355-
refute_warning("#ruby not_a_shebang\r\n", compare: false)
356-
357-
# CRuby doesn't emit the warning if a malformed file only has `\r` and not `\n`.
358-
# https://bugs.ruby-lang.org/issues/20700
359-
refute_warning("#!ruby\r", compare: false)
324+
if RbConfig::CONFIG["host_os"].match?(/bccwin|cygwin|djgpp|mingw|mswin|wince/i)
325+
def test_shebang_ending_with_carriage_return
326+
refute_warning("#!ruby\r\np(123)\n", compare: false)
327+
end
328+
else
329+
def test_shebang_ending_with_carriage_return
330+
msg = "shebang line ending with \\r may cause problems"
331+
332+
assert_warning(<<~RUBY, msg, compare: false)
333+
#!ruby\r
334+
p(123)
335+
RUBY
336+
337+
assert_warning(<<~RUBY, msg, compare: false)
338+
#!ruby \r
339+
p(123)
340+
RUBY
341+
342+
assert_warning(<<~RUBY, msg, compare: false)
343+
#!ruby -Eutf-8\r
344+
p(123)
345+
RUBY
346+
347+
# Used with the `-x` object, to ignore the script up until the first shebang that mentioned "ruby".
348+
assert_warning(<<~SCRIPT, msg, compare: false)
349+
#!/usr/bin/env bash
350+
# Some initial shell script or other content
351+
# that Ruby should ignore
352+
echo "This is shell script part"
353+
exit 0
354+
355+
#! /usr/bin/env ruby -Eutf-8\r
356+
# Ruby script starts here
357+
puts "Hello from Ruby!"
358+
SCRIPT
359+
360+
refute_warning("#ruby not_a_shebang\r\n", compare: false)
361+
362+
# CRuby doesn't emit the warning if a malformed file only has `\r` and not `\n`.
363+
# https://bugs.ruby-lang.org/issues/20700
364+
refute_warning("#!ruby\r", compare: false)
365+
end
360366
end
361367

362368
def test_warnings_verbosity

0 commit comments

Comments
 (0)