Skip to content

fix: coproc time is a plain word, not the time keyword (#1653) - #1698

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix-1653-coproc-time-pipe
Oct 26, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix-1653-coproc-time-pipe

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Fixes #1653.

Bug

coproc time echo hi | cat set COPROC_PID in the parent shell; GNU leaves it unset (the coproc lives in the pipeline subshell, and its child reports time: command not found).

Root cause (GNU source)

parse.y:1125-1175 gives coproc only COPROC shell_command, COPROC WORD shell_command and COPROC simple_command (plus redirection variants). time is a timespec prefix on pipeline_command (parse.y:1416), never a shell_command starter — so after coproc it is never the time keyword. Rubash granted it keyword status in two places, which swallowed the whole time echo hi | cat as one timed coproc body in the parent.

Changes

  • src/parser/coproc_command.rs: removed the time arm from parse_coproc_compound_body and the two time branches from is_coproc_shell_command_start (plus the now-dead time_prefixed_compound_command_start). time is now the coproc NAME when a compound command follows (via the existing next-token check — coproc time { ...; } and coproc time for ... keep working as named coprocs) and otherwise the first word of the simple-command body, which stops at |.
  • src/executor/compound_exec.rs: the simple coproc body is respawned through rubash -c <text>, where a bare head word time would regain keyword status on re-parse (GNU forks the same parse tree). A bare head time is now rendered quoted ('time') so the child keeps it an ordinary command name and reports time: command not found (127) like GNU's forked child. Already-quoted heads keep their own spelling.
  • Tests: the 5 stale parser tests that asserted the old named-plus-timed model for coproc MYC time ... are rewritten to the GNU shapes (GNU: MYC: command not found, MYC is argv[0], not a name). New tests/issue1653_coproc_time.rs: 10 end-to-end cases under an isolated PATH (only cat/sleep, so time can never resolve via PATH).

Verification

  • Issue seed script: stdout now matches GNU exactly (declare: COPROC_PID: not found, done); with any trailing sleep/wait, stderr matches too (time: command not found).
  • tests/parser_coproc_tests: 31 passed. tests/issue1653_coproc_time: 10 passed. Coproc family (issue352, issue387, issue1370, issue1528) and time family (issue1369, issue1520, issue1545, issue1548, issue1611) all green.

Known adjacent gaps, not fixed here

  • The literal no-sleep seed's child diagnostic is racy: the coproc child is a respawned process, so its time: command not found can arrive after the parent exits (deterministic once the script outlives child startup). Same pre-existing class as a plain coproc nosuchcmd diagnostic landing late.
  • coproc echo hi | cat (no time involved) spuriously prints Broken pipe when the script outlives the subshell — pre-existing coproc-pipeline EPIPE reporting, needs its own issue.
  • coproc WORD time <compound> trailing shapes ({ ...; }, (( ... )), foo() {...}) still lack GNU's syntax error near for the trailing token: after the coproc simple body, the parser swallows the compound token instead of erroring. The [bug] coproc time CMD | CAT sets COPROC in parent; GNU leaves it unset (pipeline-of-coproc parse) #1653 direction (WORD is a command, not a name) is fixed; the trailing-token validation is a separate coproc-parser gap.

GNU parse.y:1125-1175 gives coproc only `COPROC shell_command`,
`COPROC WORD shell_command` and `COPROC simple_command`; `time` is a
timespec prefix on `pipeline_command` (parse.y:1416), never a
shell_command starter. After `coproc`, `time` is therefore never the
keyword: it is the coproc NAME when a compound command follows, and
otherwise the first word of the simple-command body.

Parser (src/parser/coproc_command.rs): drop the `time` arm from
parse_coproc_compound_body and the two time branches from
is_coproc_shell_command_start (removing the now-dead
time_prefixed_compound_command_start). `coproc time echo hi | cat`
now parses as a pipeline whose first stage is an unnamed coproc of
the simple command [time, echo, hi], so the coproc lives in the
pipeline subshell and the parent never sees COPROC_PID; the
`time`-as-name cases (`coproc time { ...; }`, `coproc time for ...`)
keep working through the caller's next-token check.

Executor (src/executor/compound_exec.rs): the simple coproc body is
respawned through `rubash -c <text>`, where a bare head word `time`
would regain keyword status on re-parse (GNU forks the same parse
tree). Quote a bare head `time` (`'time'`) when rendering the child
text, so the child keeps it an ordinary command name and reports
`time: command not found` (127) like GNU's forked child. An already
quoted head keeps its own spelling.

Tests: 5 stale parser tests asserted the old named-plus-timed model
for `coproc MYC time ...` (GNU: MYC is argv[0], `MYC: command not
found`); rewritten to the pipeline/simple-word shapes. New
tests/issue1653_coproc_time.rs (10 end-to-end cases, isolated PATH
so `time` can never resolve via PATH).

Known adjacent gaps, NOT fixed here (recorded, not hidden):
- The literal no-sleep seed's child diagnostic is racy: the coproc
  child is a respawned process, so its `time: command not found`
  can arrive after the parent exits (with any trailing sleep/wait
  it appears deterministically). Same pre-existing class as a plain
  `coproc nosuchcmd` whose diagnostic lands late.
- `coproc echo hi | cat` (no time involved) spuriously prints
  `Broken pipe` when the script outlives the subshell — pre-existing
  coproc-pipeline EPIPE reporting, needs its own issue.
- `coproc WORD time <compound>` trailing shapes (`{ ...; }`,
  `(( ... ))`, `foo() {...}`) still lack GNU's `syntax error near`
  for the trailing token: after the coproc simple body, the parser
  swallows the compound token instead of erroring. The #1653
  direction (WORD is a command, not a name) is fixed; the
  trailing-token validation is a separate coproc-parser gap.
@caomengxuan666
caomengxuan666 merged commit 29b6940 into master Oct 26, 2026
7 of 10 checks passed
@caomengxuan666
caomengxuan666 deleted the fix-1653-coproc-time-pipe branch October 30, 2026 01:41
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] coproc time CMD | CAT sets COPROC in parent; GNU leaves it unset (pipeline-of-coproc parse)

2 participants