diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 529267cae..52279fa2e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -95,6 +95,7 @@ Scripting improvements - An ``alias`` that delegates to a command with the same name no longer triggers an error about recursive completion (:issue:`7389`). - ``math`` now has a ``--base`` option to output the result in hexadecimal or octal (:issue:`7496`). - ``string`` subcommands now quit early when used with ``--quiet`` (:issue:`7495`). +- Failed redirections will now set ``$status`` (:issue:`7540`). Interactive improvements ------------------------ diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 289205910..b30a4de4c 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -1391,7 +1391,13 @@ end_execution_reason_t parse_execution_context_t::run_1_job(const ast::job_t &jo } // Actually execute the job. - if (!exec_job(*this->parser, job, block_io)) { + if (!exec_job(*parser, job, block_io)) { + // No process in the job successfully launched. + // Ensure statuses are set (#7540). + if (auto statuses = job->get_statuses()) { + parser->set_last_statuses(statuses.value()); + parser->libdata().status_count++; + } remove_job(*this->parser, job.get()); } diff --git a/tests/checks/pipestatus.fish b/tests/checks/pipestatus.fish index fed646453..30fa24901 100644 --- a/tests/checks/pipestatus.fish +++ b/tests/checks/pipestatus.fish @@ -174,3 +174,31 @@ end | begin end echo $pipestatus : $status #CHECK: 0 0 : 0 + +# Check that failed redirections correctly handle pipestatus, etc. +# See #7540. +command true > /not/a/valid/path +echo $pipestatus : $status +#CHECK: 1 : 1 +#CHECKERR: warning: An error occurred while redirecting file '/not/a/valid/path' +#CHECKERR: open: No such file or directory + +# Here the first process will launch, the second one will not. +command true | command true | command true > /not/a/valid/path +echo $pipestatus : $status +#CHECK: 0 0 1 : 1 +#CHECKERR: warning: An error occurred while redirecting file '/not/a/valid/path' +#CHECKERR: open: No such file or directory + +# Pipeline breaks do not result in dangling jobs. +command true | command cat > /not/a/valid/path ; jobs +#CHECKERR: warning: An error occurred while redirecting file '/not/a/valid/path' +#CHECKERR: open: No such file or directory +#CHECK: jobs: There are no jobs + +# Regression test for #7038 +cat /dev/zero | dd > /not/a/valid/path +echo 'Not hung' +#CHECKERR: warning: An error occurred while redirecting file '/not/a/valid/path' +#CHECKERR: open: No such file or directory +#CHECK: Not hung