Skip to content

feat: add ability to combine outputs in WithExec - #10924

Merged
eunomie merged 1 commit into
dagger:mainfrom
eunomie:combined-output
Aug 22, 2025
Merged

eunomie merged 1 commit into
dagger:mainfrom
eunomie:combined-output

Conversation

@eunomie

@eunomie eunomie commented Aug 21, 2025 •

Copy link
Copy Markdown
Member

Allow to get a combined output of both standard output and standard error at once.

A new function combinedOutput is available, in addition to stdout and stderr. It means you can pick the one you want depending on the context. For instance to pick the combined output if the command failed.

$ dagger -q <<EOF
container | from alpine |
with-new-file /test.sh "echo out; echo err >&2" |
with-exec -- sh /test.sh |
combined-output
EOF

out
err

Warning

As always while working with streams, order in the output is not guaranteed.

fixes #3496

Comment thread core/container_exec.go Resolved
@eunomie

eunomie commented Aug 21, 2025

Copy link
Copy Markdown
Member Author

The PR's behaviour is to redirect standard error to standard output so combined output can be retrieved using stdout.
Currently standard error is still available independently by calling stderr.

❓ Should stderr be empty if it's redirected to standard output?
Other option would be to have stderr returning the exact same content than stdout (and so keep the CombinedOutput flag name)

@vito

vito commented Aug 21, 2025

Copy link
Copy Markdown
Contributor

Nice, wanted this for a while, thanks for picking it up! Related issue: #3496

API wise it feels confusing to call Container.stdout and get both (even though it's opt-in).

I think we could change the engine to just always write a combined output file in addition to the separate ones, and just add a getter for it:

stdoutWriters = append(stdoutWriters, stdoutFile)
var stderrWriters []io.Writer
if state.procInfo.Stderr != nil {
stderrWriters = append(stderrWriters, state.procInfo.Stderr)
}
stderrPath := filepath.Join(state.metaMount.Source, MetaMountStderrPath)
stderrFile, err := os.OpenFile(stderrPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return fmt.Errorf("open stderr file: %w", err)
}
state.cleanups.Add("close container stderr file", stderrFile.Close)
stderrWriters = append(stderrWriters, stderrFile)

Then you can just call Container.output without having to opt-in ahead of time.

We'll be storing extra data for each command but it seems a small price to pay for the simplicity imo.

@eunomie

eunomie commented Aug 21, 2025 •

Copy link
Copy Markdown
Member Author

We'll be storing extra data for each command but it seems a small price to pay for the simplicity imo.

That's why I haven't done it at start, but if it sounds like a better approach, I'll do that 👍

Then you can just call Container.output without having to opt-in ahead of time.

I think I would prefer something like Container.combinedOutput. It's more verbose for sure, but it explains itself better.

Thinking a bit more about that, this also means we can retrieve one or the other after the withExec, and that's probably way better. I can imagine some cases where I run a withExec to get the output, in a normal case. But if something goes wrong, for instance if the exit code is not the one I expected, I'd like to get the combined output to print it to the user.
By having a flag in the withExec this means I have to choose up front. By having a combinedOutput function, it means I can pick the one I need when I want.

Thanks for the suggestion, I'll update that 😃

Comment thread core/integration/module_test.go Resolved
@jedevc

jedevc commented Aug 22, 2025

Copy link
Copy Markdown
Contributor

Hey, looks nice! 👀

@eunomie
eunomie force-pushed the combined-output branch 2 times, most recently from 726a34f to 8472abd Compare August 22, 2025 09:22
@eunomie
eunomie marked this pull request as ready for review August 22, 2025 09:23
@eunomie
eunomie requested review from a team as code owners August 22, 2025 09:23
@eunomie

eunomie commented Aug 22, 2025

Copy link
Copy Markdown
Member Author

I pushed a new version:

  • with a combinedOutput func (like stdout and stderr) instead of a configuration on withExec
  • always write both stdout and stderr to a new file to be able to retrieve the combined output
  • move test to ContainerSuite

A new function `combinedOutput` is available, in addition to `stdout`
and `stderr`. It means you can pick the one you want depending on the context.
For instance to pick the combined output if the command failed.

    $ dagger -q <<EOF
    container | from alpine |
    with-new-file /test.sh "echo out; echo err >&2" |
    with-exec -- sh /test.sh |
    combined-output
    EOF

    out
    err

Warning: as always while working with streams, order in the output is not guaranteed.

fixes dagger#3496

Signed-off-by: Yves Brissaud <[email protected]>
@jedevc
jedevc requested a review from vito August 22, 2025 13:09
@jedevc jedevc added area/engine/api needs/changelog Pull requests that require a changelog entry labels Aug 22, 2025
@eunomie
eunomie merged commit 3dfb4ee into dagger:main Aug 22, 2025
@eunomie
eunomie deleted the combined-output branch August 22, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/engine/api needs/changelog Pull requests that require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for combined stdout and stderr output

3 participants