Skip to content

declare -f re-serializes from AST instead of replaying definition text: leaks source-context redirect into } (rewrites 2> as 2>>), rewrites nested quotes in $() as \", drops -n in compound [[ ]] #202

Closed

Description

@caomengxuan666

Corpus: target-tools P1 (2026-09-26 session), real tool integrations starship.bash / atuin.bash / direnv-stdlib.sh sourced via . with >file 2>file redirects, then dumped with declare -f.

GNU spec: a function's definition ORIGINAL TEXT is preserved at bind time (third_party/bash/execute_cmd.c:6333 bind_function_def()) and declare -f replays it (printing path in builtins/declare.def). rubash re-serializes from its AST, producing 5 divergent symptom classes:

  1. Definition-context redirect leaks into the closing brace AND mutates the operator:
    . ./inner.sh > /dev/null 2> /dev/null then declare -f pf prints
    } 2>> /dev/null (GNU: }). Minimal probe: target-tools/scripts/probe-starship.sh, artifacts target-tools/results/P-starship/{gnu,rub}.out. Every function defined while a .-redirect is active is affected (starship/atuin show it on ALL functions). Dumped+re-sourced functions accumulate bogus append-redirects.
  2. Nested double quotes inside $(...) inside "..." are re-serialized as \":
    source PS1="$(printf 'p %s' "")" → declare -f prints PS1="$(printf 'p %s' \"\")". Re-eval of the dump changes semantics (literal quotes enter the value). Execution itself is correct — display-layer only.
  3. -n dropped in compound [[ ]]: [[ -n $1 && ${2-x} ]] prints as [[ -n $1 && ${2-x} ]] → GNU prints [[ -n $1 && -n ${2-x} ]]" (GNU makes the implicit unary explicit; rubash drops the explicit one). Probe: target-tools/results/P2/.
  4. Cosmetic normalization byte-diffs: ((NUM_JOBS++))→(( NUM_JOBS++ )), (+=(--x) spacing, X=""→X='', literal newline in pattern → $'\n', literal CR → $'\r', multi-line comsub gets ; injected at line ends.
  5. Real-corpus diffs: target-tools/results/C-starship/diff.txt (71), C-atuin/diff.txt (393), C-direnv-stdlib/diff.txt (701); fzf key-bindings/completion are clean.

Classification: rubash-caused. Not counted as environment noise.

Activity

  1. added
    bug
    on Sep 27, 2026
  2. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

    已修复并合入 master(commit fb94d2e1,lane wt/declare)。读 C 源后修正了原假设:GNU 不是逐字回放定义文本,而是经 declare.def:516 → print_cmd.c 的 named_function_string 再序列化。五个子修复:(a) 函数定义节点的 stderr 重定向注入被跳过(execute_cmd.c:5322 规则——此前被调函数的 stderr 每次调用都被引到 source 时的 /dev/null);(b) 赋值词打印源文本 RHS(X="" 不再变 X='',嵌套 comsub 引号不再变 ");(c) ((...)) 打印原始表达式(print_cmd.c:871);(d) [[ ]] 裸项 -n 包裹(parse.y:5187);(e) $'...' 打印时解码(parse.y:5566)。dump/重 source 往返稳定,5 个探针文件与 WSL GNU 5.3.0 diff 干净。感谢报告!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions