From 29b348b46559523e312e7deaf7a7db24881e0da0 Mon Sep 17 00:00:00 2001 From: Mykhailo Chalyi Date: Thu, 26 Mar 2026 17:13:24 +0000 Subject: [PATCH] fix(awk): treat # inside regex literals as literal, not comment The awk normalizer now recognizes regex literals (/pattern/) in both pattern and action contexts. Previously, regex was only recognized inside braces, so /#/ in a pattern position caused # to be treated as a comment start. Closes #835 --- crates/bashkit/src/builtins/awk.rs | 4 ++-- .../bashkit/tests/spec_cases/awk/awk.test.sh | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/bashkit/src/builtins/awk.rs b/crates/bashkit/src/builtins/awk.rs index 8e025595..f3b08678 100644 --- a/crates/bashkit/src/builtins/awk.rs +++ b/crates/bashkit/src/builtins/awk.rs @@ -402,8 +402,8 @@ fn normalize_awk_newlines(input: &str) -> String { i += 1; } } - '/' if brace_depth > 0 => { - // Potential regex literal — pass through unchanged + '/' => { + // Regex literal — pass through unchanged (both pattern and expression context) result.push('/'); i += 1; while i < chars.len() && chars[i] != '/' { diff --git a/crates/bashkit/tests/spec_cases/awk/awk.test.sh b/crates/bashkit/tests/spec_cases/awk/awk.test.sh index f5a9bca8..b71fe95d 100644 --- a/crates/bashkit/tests/spec_cases/awk/awk.test.sh +++ b/crates/bashkit/tests/spec_cases/awk/awk.test.sh @@ -703,3 +703,24 @@ awk 'BEGIN{print "caf\u00E9"}' ### expect café ### end + +### awk_regex_hash_pattern +# Issue #835: # inside regex pattern should not be treated as comment +echo '#test' | awk '/#/ { print "matched" }' +### expect +matched +### end + +### awk_regex_hash_caret +# Issue #835: /^#/ pattern +printf '# heading\nnormal\n' | awk '/^#/ { print "comment:", $0 }' +### expect +comment: # heading +### end + +### awk_regex_hash_no_match +# Regex without # still works +echo 'abc' | awk '/abc/ { print "yes" }' +### expect +yes +### end