Skip to content

[bug] P0 Command substitution with a compound body returns EMPTY when the enclosing function call has a stdout redirection — direnv stdlib path_add exports PATH='' (silent env wipe) #362

Closed

Description

@caomengxuan666

Found by ecosweep4 lane (wt22/nest22; corpus: direnv stdlib.sh per-function drive; worktree base a522eb3, release build; oracle WSL GNU Bash 5.3.0 script-file runs).

Reproducer

f2() { v=$(IFS=:; echo hi); echo "compound=[$v]" >&2; }
f2 >/dev/null        # any outer stdout redirect on the call
  • rubash: compound=[] (capture lost; the hi goes to the outer redirect target)
  • GNU 5.3.0: compound=[hi]

Full matrix (target/issue-suites/results/ecosweep4/direnv/cs2.sh, both-side captures cs2.{rub,gnu}.err):

shape body rubash under f >/dev/null GNU
simple v=$(echo hi) [hi] OK [hi]
compound ; v=$(IFS=:; echo hi) EMPTY [hi]
group v=$( { echo hi; } ) EMPTY [hi]
sequence v=$(printf a; printf b) EMPTY [ab]
compound+param-op v=$(IFS=:; echo "a:b:c") then ${v%%:*} EMPTY [a:b]
array join v=$(IFS=:; echo "${arr[*]}") EMPTY [/x:/y]

Without the outer redirect, the same compound comsubs capture fine (f2 bare call OK). So the trigger is: compound comsub body × active stdout redirection on the enclosing function call — the body's writes go to the redirected fd 1 instead of the substitution capture.

Real-world hit (why it is P0)

direnv stdlib.sh path_add() (the implementation behind PATH_add / every layout_* / load_prefix):

path=$(
  IFS=:
  echo "${path_array[*]}"
)
export "$var_name=$path"

Any .envrc doing layout go / layout node / layout perl / load_prefix (these call layout X >/dev/null internally in common wrappers, or are called redirected by user code) ends up with export PATH= — an EMPTY PATH — under rubash, while GNU exports the joined path. Verified live: layout go >/dev/null under rubash yields GOPATH=[] and PATH= exported empty (xtrace: the inner echo prints the full joined PATH, then + path= and + export PATH= — xtrace artifacts at target/issue-suites/results/ecosweep4/direnv/lg.err). Everything downstream dies (tr: command not found).

Affected direnv functions measured this lane: layout_go (GOPATH lost, PATH wiped), layout_node, layout_perl (PERL_LOCAL_LIB_ROOT lost), layout_php; up_PATH_add standalone (no redirect) passes.

Relation to known family

rubash#289 (CLOSED) fixed command -v/-V describe output bypassing the thread-local STDOUT_CAPTURE; per its close comment the capture "owns fd 1" during substitution. This issue is the same interplay with a different trigger: ordinary echo/printf writes in a compound comsub body follow the OUTER redirect instead of the capture. GNU anchor: subst.c:7143 command_substitute — dup2(fildes[1], 1) (subst.c:7320) makes fd 1 the pipe write end for the WHOLE body (compound or not) for the duration of the substitution; an enclosing function-call redirect is irrelevant because the substitution saves/restores fd 1 around itself (execute_cmd.c redirection stacking), it never lets body output escape to the caller's redirected fd 1.

Probes

  • target/issue-suites/results/ecosweep4/direnv/cs2.sh + .rub.err/.gnu.err (6-shape matrix)
  • .../direnv/lg.sh + lg.err (layout_go xtrace showing + path= empty after the join echo)
  • per-function verdicts: .../direnv/verdict.txt (52 drivers, 35/52 identical; reds map to this issue + message-form items below)

Suggested Rust owners to inspect: the comsub executor's compound-body path where fd 1 is swapped for the capture (the #289 fix introduced the buffered single-truth path for command -v; the compound-body echo/printf path still seems to write to the live fd 1, which an active caller redirect has already moved).

Activity

  1. added
    bug
    on Sep 30, 2026
  2. added 3 commits that reference this issue on Sep 30, 2026
  3. caomengxuan666 commented on Sep 30, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by wt22/p0fix (645ecff, master 3ade4b2): three in-process comsub body executors had fd-1 rebinding (subshell path Q16, function shortcut #161) — but the COMPOUND-body AST path (run_ast_command_substitution_with_context, the ;/&&/newline dispatch entry) executed in place with NO rebind, so body writes followed the caller's redirected fd 1 into the outer target (verified: hi landed in outer.txt). GNU subst.c:7143 command_substitute -> dup2(fildes[1],1) at 7306-7320 covers the WHOLE body. Fix: the same fd-1 rebind bracket (saved_fd_table restore already existed). 16-shape separated-stream matrix 15/16 byte-identical (the one exception is the pre-existing dup-snapshot family, fails on master identically); comsub/comsub2/comsub-eof/comsub-posix/lastpipe/exp/more-exp/dstack/ifs all 0 diff; direnv layout_node/layout_php turn green (31->33/52). 11 regressions.

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