Skip to content

Add stdout parameter to ctx.actions.run - #30408

Open
fmeum wants to merge 1 commit into
bazelbuild:masterfrom
fmeum:actions-run-stdout
Open

Add stdout parameter to ctx.actions.run#30408
fmeum wants to merge 1 commit into
bazelbuild:masterfrom
fmeum:actions-run-stdout

Conversation

@fmeum

@fmeum fmeum commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Description

When the new stdout parameter of ctx.actions.run is set, the stdout of the action is stored in the given file rather than printed in the terminal and reported via the BES. The file is otherwise an ordinary action output and follows BwoB download patterns. Since persistent worker communicate via stdout, actions that opt into them are not allowed to set stdout.

Note that adding stdout only was the uncontentious part of #5511 as it avoids any discussion of how stdout and stderr would behave if set to the same file (merging is not supported with remote execution etc.). The current PR does not make it harder to support stderr if needed in the future.

Motivation

Tools commonly emit useful output to stdout and going through ctx.actions.run_shell just to capture it is overkill and has a worse hermeticity story (dependency on a shell, which is problematic in cross-platform build cases).

Implements the most important part of #5511.

Build API Changes

Yes:

  1. Has this been discussed in a design doc or issue?
    Provide a way to redirect stdout/stderr into a file #5511, where the stdout case has seen unanimous support.
  2. Is the change backward compatible?
    Yes, it's a new parameter.

Checklist

  • I have added tests for the new use cases (if any).
  • I have updated the documentation (if applicable).

Release Notes

RELNOTES[NEW]: ctx.actions.run now supports redirecting stdout to a file via the new stdout parameter.

@google-cla

google-cla Bot commented Jul 22, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@fmeum
fmeum force-pushed the actions-run-stdout branch 6 times, most recently from 7ec8b60 to 40dc70d Compare July 22, 2026 12:17
@fmeum
fmeum force-pushed the actions-run-stdout branch 3 times, most recently from f6f3427 to 6410728 Compare July 22, 2026 12:59
Adds a `stdout` parameter to `ctx.actions.run` that accepts a File and
redirects the action's standard output stream into it. The captured
output becomes a regular output artifact of the action, but it is not
reported as regular action stdout: it is not printed to the terminal and
is not included in the build event stream.

The stdout output is exposed to execution via a new `Spawn#getStdout`,
stays part of `Spawn#getOutputFiles`, and is included in the action key
so that moving a file between `outputs` and `stdout` invalidates the
action:

  * Local and sandboxed execution redirect the process's stdout file
    descriptor directly into the output file instead of the regular
    stdout capture buffer, so nothing leaks to the terminal or the BEP.
  * Remote execution excludes the output from the REAPI command's
    output_paths (the server captures stdout separately) and
    reconstructs it from the action result's stdout digest. Unlike
    regular stdout/stderr, it is no longer downloaded eagerly: it honors
    build-without-the-bytes like any other output (metadata is injected
    and it is fetched on demand). Local-then-uploaded actions store the
    captured stdout as the action result's stdout digest to keep cache
    entries consistent with remotely executed ones.
  * Action rewinding works without special handling because the stdout
    output is an ordinary output artifact with a generating action key,
    so a lost stdout that feeds another action rewinds the producer.

Since persistent workers communicate with Bazel over stdout, combining
`stdout` with the `supports-workers` or `supports-multiplex-workers`
execution requirements is rejected at analysis time.

Only `stdout` is supported; `stderr`/`stdin` are intentionally left out.

RELNOTES[NEW]: `ctx.actions.run` now accepts a `stdout` parameter that
redirects the action's standard output into a declared output file.

Fixes bazelbuild#5511. See also bazelbuild#8275.
@fmeum
fmeum force-pushed the actions-run-stdout branch from 6410728 to 1aa04e5 Compare July 22, 2026 13:10
@fmeum
fmeum marked this pull request as ready for review July 22, 2026 13:34
@fmeum
fmeum requested review from a team as code owners July 22, 2026 13:34
@fmeum
fmeum requested review from mai93 and removed request for a team July 22, 2026 13:34
@github-actions github-actions Bot added team-Performance Issues for Performance teams team-Configurability platforms, toolchains, cquery, select(), config transitions team-Local-Exec Issues and PRs for the Execution (Local) team team-Remote-Exec Issues and PRs for the Execution (Remote) team team-Starlark-Integration Issues involving Bazel's integration with Starlark, excluding builtin symbols awaiting-review PR is awaiting review from an assigned reviewer team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts labels Jul 22, 2026
@fmeum
fmeum requested review from meisterT, tjgq and trybka and removed request for a team and mai93 July 22, 2026 13:35
@fmeum

fmeum commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

As requested by @trybka.
@tjgq Could you review this for RBE?
@meisterT Could you review this for local exec?

@meisterT

Copy link
Copy Markdown
Member

I would like to see a review from @tetromino or @brandjon first

@tetromino tetromino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StarlarkActionFactoryApi.java and StarlarkActionFactory.java LGTM. The spawn/bwob changes are a bit outside my expertise, and someone else ought to review them.


General question: shouldn't we support stdout for run_shell too?

@fmeum

fmeum commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

General question: shouldn't we support stdout for run_shell too?

I would say "no" for now since using a shell redirect is much less of an ask if you already use run_shell. More importantly, I think that run_shell should be Starlarkified and live in rules_sh with proper toolchain resolution for the shell before we add more functionality to it.

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

Labels

awaiting-review PR is awaiting review from an assigned reviewer team-Configurability platforms, toolchains, cquery, select(), config transitions team-Local-Exec Issues and PRs for the Execution (Local) team team-Performance Issues for Performance teams team-Remote-Exec Issues and PRs for the Execution (Remote) team team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts team-Starlark-Integration Issues involving Bazel's integration with Starlark, excluding builtin symbols

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants