Skip to content

fix(capture): strip a CRLF terminator from command-substitution output (niubash #120) - #115

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/niubash-120-crlf-capture
Sep 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/niubash-120-crlf-capture

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Fixes unixwin/niubash#120.

Problem

$( ) captures a trailing \r from every Windows-native tool:

$ niu -c 'o=$(printf "a\n" | awk "{print}"); [ "$o" = a ] && echo EQ || echo NE'
NE
$ niu -c 'n=$(echo "{\"n\":2}" | jq -r .n); [ "$n" = 2 ] && echo EQ || echo NE'
NE

GNU (subst.c command_substitute) deletes every trailing newline and keeps any
other trailing byte, so on Unix o=$(printf 'a\r\n') is a\r. That holds when
the tools in the pipe are GNU tools. Here they are Windows-native, and their CRT
text mode turns every \n into \r\n; a native shell reads those pipes without
the MSYS text-mode translation that maps \r\n back to \n, so the \r reaches
$o and breaks [ "$o" = x ], case labels, array keys and concatenated paths.

A 47-probe sweep of the shipped tools: CRLF from gawk, GoAWK's awk,
ugrep, python, jq, xz, curl, 7z (all C/C++ builds); LF from
winuxcmd's own builtins, rg, fd, bat, node, git, yq, age, tar.
The earlier attribution ("GoAWK outputs CRLF, GNU awk outputs LF") is wrong —
swapping in GNU awk changes nothing.

Change

Treat a \r that immediately precedes a stripped \n as part of the
terminator; keep a lone trailing \r (GNU fidelity) and leave interior CRLF
alone. This is the policy the lexer already applies to CRLF script lines
(#106, 3ce1872); the capture side was the part missing it.

  • SubstitutionOutput::readback — the byte-level choke point every substitution
    that runs through the real executor passes through, so it covers $(cmd),
    pipelines and compound bodies.
  • CaptureTerminator / trim_capture_terminator_bytes carry the rule once for
    text, once for bytes.
  • The 18 specialized capture sites that stripped only '\n' and the type -t
    shortcut, which stripped every trailing '\r'.
  • read -t dropped the \r before the newline of a timed line, unlike
    non-timed read.

read itself is deliberately left alone: trim_read_input also drops a lone
trailing \r that GNU keeps, but that pop is load-bearing (the pty/pipe readers
hand over a line without its delimiter, so a CRLF terminator arrives as a bare
trailing \r). The comment in trim_read_input records what a proper fix would
require.

Verification

Behavioural, against released niubash 1.1.4 (rubash 8b81c750) on this machine,
same script both sides — 16 single-line captures:

baseline 8b81c75 this branch
pass 3 15
fail 12 0

Covers gawk, GoAWK awk, ugrep -c, python, jq, trailing blank lines,
$(<file) and cat on a CRLF file, gawk \| head -1, while read on CRLF, the
n=$(… \| jq -r .n); [ "$n" = 2 ] agent pattern, and the GNU-fidelity cases
(o=$(printf 'a\r') keeps its CR; interior CRLF survives).

New unit test readback_strips_crlf_terminators_only covers terminator /
repeated terminator / interior CRLF / lone CR.

cargo test --lib: 25 passed in the touched module.

cargo test --test cli_tests on this machine: 344 passed / 73 failed on a
baseline checkout of 8b81c750, 347 passed / 70 failed on this branch — no
test regresses, and three flip to green:

  • examples::gnu_dirstack_function_definitions_parse_comments
  • examples::script_array_to_string_example_joins_arrays
  • examples::script_kshenv_example_parses_multiline_awk_quote

The remaining 70 failures are identical to the baseline set and unrelated
(arithmetic diagnostic wording, other examples::*, process_substitution::*).

GNU bash deletes every trailing newline from a substitution's output and keeps
any other trailing byte, so on Unix `o=$(printf 'a\r\n')` is "a\r". That holds
when the tools in the pipe are GNU tools. niubash runs Windows-native tools whose
CRT text mode turns every '\n' into '\r\n' (measured with a 47-probe sweep: gawk,
GoAWK's awk, ugrep, python, jq, xz, curl and 7z emit CRLF; winuxcmd's own
builtins, rg, fd, bat, node, git, yq and age emit LF), and a native shell reads
those pipes without the MSYS text-mode translation that maps "\r\n" back to "\n".
The stray '\r' therefore survives into `$o`, where one invisible trailing byte
breaks `[ "$o" = x ]`, `case` labels, array keys and concatenated paths
(niubash #120). `n=$(echo '{"n":2}' | jq -r .n)` compared unequal to "2" for
exactly this reason.

Treat a '\r' that immediately precedes a stripped '\n' as part of the
terminator; keep a lone trailing '\r' so the GNU-fidelity case stays intact, and
leave interior CRLF alone. This is the policy the lexer already applies to CRLF
script lines (niubash #106, 3ce1872); the capture side was the part missing it.

Sites fixed:
- `SubstitutionOutput::readback` (substitution_metadata.rs) is the byte-level
  choke point for every substitution that goes through the real executor, so it
  carries the rule for the general path (`$(cmd)`, pipelines, compound bodies).
- CaptureTerminator/trim_capture_terminator_bytes (command_subst_helpers.rs)
  hold the rule once for text and once for bytes.
- The 18 specialized capture sites that stripped only '\n'
  (command_substitution*.rs, embedded_mutations.rs) and the `type -t` shortcut,
  which stripped every trailing '\r'.
- `read -t` dropped the '\r' before the newline of a timed line, unlike
  non-timed `read`.

`read` itself is deliberately unchanged: trim_read_input also drops a lone
trailing '\r', which GNU keeps, but that pop is load-bearing because the
pty/pipe readers hand over a line without its delimiter. Reversing it only for
the reading paths that report the delimiter is a separate change; the comment
in trim_read_input records the constraint.

Interior CRLF stays untouched, so multi-line captures only differ from before in
the terminator.

Verified against niubash 1.1.4 (rubash 8b81c75) on 16 single-line captures:
3 pass before, 15 pass after (the two excluded cases are `read`, unchanged).
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.

[bug] 命令替换只剥 \n 不剥 \r:捕获值带尾随 CR,字符串比较静默失败(与捆绑 awk 的 CRLF 叠加)

1 participant