Repository navigation
perf(executor): single-handle cd/pwd comsub path resolution (rubash#375) - #484
Merged
Merged
Conversation
Profiled the remaining per-firing cost of the bats-core gather lane: the $(cd X && pwd) command-substitution fast path spent ~128µs per call, 95% of it in fs::canonicalize + Path::is_dir — two CreateFileW round trips per call on NTFS (measured 122µs -> 615ms/5000 in-process). The dynamic stack array views and the allocation-free DEBUG-trap consult from the earlier #375 work are already down to single-digit µs per firing. Resolve both facts off ONE handle open on Windows: CreateFileW with FILE_FLAG_BACKUP_SEMANTICS (query access, full share modes), the directory attribute from GetFileInformationByHandle, and the final symlink-resolved verbatim path from GetFinalPathNameByHandleW — the same two outputs fs::canonicalize + is_dir delivered, so admission and output of the fast path are unchanged: - missing target: CreateFileW fails exactly where canonicalize errors (status 1, empty substitution, as before) - existing file: backup semantics opens it, is_dir == false (status 1) - junctions/reparse points resolve to the on-disk target, matching the old canonicalize behavior byte for byte (junction parity probes) $(cd X && pwd) x2000: 370ms -> 329ms; x5000: 878ms -> 779ms (~11%). DEBUG-trap-only and no-trap loops are unchanged (within noise). POSIX keeps the std::fs::canonicalize pair — getcwd-scale cost there. Full cargo test --release: 630 passed; the 2 failures (pwd::tests::logical_mode_reports_posix_drive_pwd_by_default, path::tests::virtual_system_root_operand_resolution_keeps_option_b_and_ legacy_verbatim) fail identically on a clean baseline of this commit's parent (environment-dependent, pre-existing). Closes #375
caomengxuan666
added a commit
that referenced
this pull request
Oct 10, 2026
…osing command runs (rubash#485) (#494) * fix(executor): comsub reap publishes its status to $? before the enclosing command runs (rubash#485) GNU command_substitute (subst.c:7143) reaps the substitution child in the parent and the child's exit status becomes last_command_exit_value at that moment - BEFORE the enclosing command runs - so every later expansion on the same command line sees it, and a SUCCESSFUL substitution clobbers the previous status just the same. Probes, WSL GNU bash 5.3.0 (/usr/local/bin/bash, 2026-10-10): echo "$(cd nope && pwd)" $? -> " 1" (rubash: " 0" - the issue repro) echo "$(exit 3)" $? -> " 3" false; echo "$(true) $?" -> " 0" (unconditional reap contract) true; echo $? "$(exit 3)" -> "0 " (left-to-right: $? BEFORE keeps old) echo "$(exit 3)$?" -> "3" (same-word trailing fragment) echo a`exit 3`b $? -> "ab 3" (embedded backtick word) v="$(exit 3)$?" -> v=3, rc=3 (assignment keeps last comsub) false; x=$(true) y=$? -> y=0, rc=0 f(){ local v="$(exit 3)$?"; } -> v=3, rc=0 (declaration builtin masks) rubash recorded the substitution's status only in the last_command_substitution_status cell and promoted it at the two consumption sites (vanished-words null command, assignment RHS tail), so the word-expansion path never published it: every mid-line $? read the stale pre-expansion value. Fix - publish the reaped status at each parent-side completion point: - embedded_mutations.rs run_ast_command_substitution_with_context and run_function_command_substitution tails: exit_code = status at the fork boundary instead of restoring the pre-comsub value (the in-place executor IS the parent side of GNU's waitpid; saved_exit_code dropped exactly the status GNU publishes) - embedded_mutations.rs expand_command_substitution_mut_typed_with_context: the three fallbacks that run through the &self expand_command_substitution_with_context wrapper (specialized cd/pwd path incl. the #484 fast path, specialized builtins, final fallback) promote the wrapper's status cell - the wrapper is &self and cannot write the field - command_prepare.rs whole-word quoted "$(...)" fast path: same promotion after the wrapper call - parameter_core.rs expand_word_mut_with_context: embedded-backtick words route to the mutable embedded walker (the production path for every $( )-bearing word) instead of the legacy &self expand_word fallback, whose completions cannot publish status Unchanged on purpose (GNU-verified): the enclosing command's own status still overwrites when it runs (echo $(false); $? = 0); set -e aborts on a failed comsub assignment with its rc (D case, 1 / 4) and does NOT abort on a word-attached comsub failure; local/declare mask the assignment status (rc 0); nested comsub reports the outer body's own last status to the parent. Known adjacent gap, NOT touched (pre-existing, separate subsystem): in a pipeline, stage words are expanded in a pre-pass, so `true; echo $? "$(exit 3)" | tr -d ' '` prints 3 where GNU prints 0 - the later word's comsub runs before the earlier word's $? expands. The pre-pass predates this fix (which only made its status observable); filing separately. Also pre-existing: the cd/pwd comsub fast path stays silent on stderr where GNU prints the cd diagnostic (status and stdout match). Verification: tests/issue485_comsub_status_propagation.rs 13/13 (oracle probes above baked as assertions; set -e D case included). Adjacent suites green: issue70, issue343_345, issue440 (#455 lane), issue71, issue154, issue305, issue319, issue335, issue354, issue362, issue368, issue376, issue379, issue384, issue416_417, posixexp2, comsub_nonascii, conformance runner 22/23 (03-cond-unclosed-bracket pre-existing, GNU aborts the same shape with rc 2). Full cargo test --no-fail-fast: failure set byte-identical to the pre-fix tree (68 host-env failures compared name-by-name with the three fixed files reverted: pwd/path drive rendering, tilde-user fixture, bashdb fixtures, prompt/cli env classes; empty both-way diff); lib 636 passed / 2 failed = the master host-env pair. Closes #485 * fix(executor): cd/pwd comsub shortcut prints the #491 diagnostic once, not per admission probe The #491 merge added the cd diagnostic to command_substitution_cd_pwd_output - but the mut_typed word path consults that helper TWICE: first as the admission probe (command_substitution_uses_specialized_path calls it with .is_some() to admit the specialized path), then for the real expansion inside the &self wrapper's inner walk. Each failure printed the diagnostic, so `x="$(cd nope && pwd)"` and `local y="$(cd nope && pwd)"` reported `cd: nope: No such file or directory` twice where GNU bash 5.3.0 prints it once (probe vs script-file oracle, /usr/local/bin/bash, byte-compare on both shapes). Split the helper: the single-arg form (real expansion) keeps the emission; the probe goes through command_substitution_cd_pwd_output_quiet_probe, which suppresses only the diagnostic - status cell, output, and admission result are unchanged. Verification: both #485 suites green (tests/issue485_comsub_status_ propagation.rs 13/13, tests/issue485_comsub_status_word_propagation.rs 13/13); 17-form oracle matrix byte-identical to GNU 5.3.0 including the cd shapes now that the diagnostic prints once; full cargo test --no-fail-fast failure set identical to the pre-merge baseline (68 host-env failures, empty both-way diff).
This was referenced Oct 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
perf #375 的第一刀(部分修复,非 closes):$(cd X && pwd) 命令替换快路径的单句柄化——一次 CreateFileW(FILE_FLAG_BACKUP_SEMANTICS) 同时取目录属性+物理路径,替代 std 的两次独立 CreateFileW 往返(95% 耗时所在)。刻意不做 memo 缓存(不满足 #117 可证等价纪律),输出/准入逐位等价(junction/缺失目录/file-not-dir/../尾斜杠 before/after 二进制 diff 验证)。基准:$(cd && pwd)×5000 878→779ms(-11%),bats_debug_trap 形状 -8%,无 trap/纯 trap 噪声内。剩余范围见 #375 重开评论。