Skip to content

Print stderr streams to stderr in pipeline_data::print_if_stream() - #11929

Merged
IanManske merged 1 commit into
nushell:mainfrom
devyn:print_if_stream-write-stderr
Feb 24, 2024
Merged

IanManske merged 1 commit into
nushell:mainfrom
devyn:print_if_stream-write-stderr

Conversation

@devyn

@devyn devyn commented Feb 22, 2024

Copy link
Copy Markdown
Contributor

Related to #11928 - tee --stderr doesn't really work as expected without it

Description

Print stderr streams to stderr in pipeline_data::print_if_stream()

This corrects unexpected behavior if a stream from an external program is transformed while still preserving its stderr output. Before this change, that output is just drained and discarded. Worse, it's drained to a buffer, which could be really slow and memory hungry if there's a lot of output on stderr.

This is needed to make tee --stderr function in a non-surprising way. See #11928

User-Facing Changes

A script that was erroneously not producing stderr output before might now, but I can't think of a lot of examples of an external stream being transformed without being converted.

Tests + Formatting

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

This corrects unexpected behavior if a stream from an external program
is transformed while still preserving its stderr output. Before this
change, that output is just drained and discarded. Worse, it's drained
to a buffer, which could be really slow and memory hungry if there's
a lot of output on stderr.

This is needed to make `tee --stderr` function in a non-surprising way.
@fdncred

fdncred commented Feb 22, 2024

Copy link
Copy Markdown
Contributor

I'm wondering if there are unintended consequences to this. I'm not sure how to tell and suspect that this could be a breaking change. I'm not saying we shouldn't land this, but I'm just wondering how it will show up in normal nushell behavior.

@IanManske

Copy link
Copy Markdown
Member

I think this is probably fine to land. Worse case scenario, there's some extra stderr output. Looking through the code, it looks like this will mainly affect calls to ExternalStream::print. This is used in few places, like to print the final PipelineData or in the print command. The only potentially questionable behavior I was able find is with the print command:

do { nu -c "print -e a" } | print

This previously prints nothing, but now prints "a" to stderr.

@fdncred

fdncred commented Feb 22, 2024 •

Copy link
Copy Markdown
Contributor

do { nu -c "print -e a" } | print

That's interesting. So, print "a" to stderr then print that to stdout? I guess that's what that pipeline is saying. lol I really have no idea if that is right or wrong.

@IanManske

IanManske commented Feb 22, 2024 •

Copy link
Copy Markdown
Member

print "a" to stderr then print that to stdout?

That's what I imagine the pipeline means. But what really happens is that "a" is printed to stderr, captured, and then reprinted to stderr instead of stdout.

This is a really minor point, so I think it's fine to ignore it. Our stdio is already pretty inconsistent...

@devyn

devyn commented Feb 22, 2024 •

Copy link
Copy Markdown
Contributor Author

That is a little bit inconsistent, but worst case, if someone really desires sending everything to stdout in print, they can probably use o+e>| print? Although I tested that, and error redirection for pipes doesn't seem to actually work - I would expect e>| to go to stdout, and for both in o+e>| to go to stdout, and it doesn't actually do that

For example, try this:

do { nu -c "print 'foo'" } e>| complete

this just causes the stdout to not be captured

do { nu -c "print -e 'foo'" } e>| complete

this doesn't capture anything (!), both stdout and stderr are missing

do { nu -c "print -e 'foo'" } o+e>| complete

this does capture stderr, but it still appears as stderr

@fdncred

fdncred commented Feb 22, 2024

Copy link
Copy Markdown
Contributor

@WindSoilder Any thoughts on the above use case?

@IanManske

Copy link
Copy Markdown
Member

Yeah, @devyn your examples should ideally work but don't currently. Some other ways that currently work include:

  • Removing the e>| redirection pipe
    do { nu -c "print -e 'foo'" } | complete | get stderr | # ...
  • Using print instead (without do)
    nu -c "print -e 'foo'" e>| print

So, I think these workarounds should suffice. Either way, I actually think the new behavior with do { nu -c "print -e a" } | print makes more sense.

@WindSoilder

Copy link
Copy Markdown
Contributor

Sorry, not really sure, I believe there are some issues while using do | complete with e>|pipeline

@IanManske IanManske added the deprecated:pr-errors Deprecated: use A:error-handling, A:error-unhelpful, or A:error-silent-fail instead label Feb 24, 2024
@IanManske IanManske added this to the v0.91.0 milestone Feb 24, 2024
@IanManske

Copy link
Copy Markdown
Member

I think this is good to land, thanks @devyn !

@IanManske
IanManske merged commit 098527b into nushell:main Feb 24, 2024
kik4444 pushed a commit to kik4444/nushell-fork that referenced this pull request Feb 28, 2024
…ushell#11929)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

Related to nushell#11928 - `tee --stderr` doesn't really work as expected
without it

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Print stderr streams to stderr in `pipeline_data::print_if_stream()`

This corrects unexpected behavior if a stream from an external program
is transformed while still preserving its stderr output. Before this
change, that output is just drained and discarded. Worse, it's drained
to a buffer, which could be really slow and memory hungry if there's a
lot of output on stderr.

This is needed to make `tee --stderr` function in a non-surprising way.
See nushell#11928

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

A script that was erroneously not producing stderr output before might
now, but I can't think of a lot of examples of an external stream being
transformed without being converted.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`


# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
fdncred pushed a commit that referenced this pull request Feb 28, 2024
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

[Related conversation on
Discord](https://discord.com/channels/601130461678272522/615329862395101194/1209951539901366292)

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

This is inspired by the Unix tee command, but significantly more
powerful. Rather than just writing to a file, you can do any kind of
stream operation that Nushell supports within the closure.

The equivalent of Unix `tee -a file.txt` would be, for example, `command
| tee { save -a file.txt }` - but of course this is Nushell, and you can
do the same with structured data to JSON objects, or even just run any
other command on the system with it.

A `--stderr` flag is provided for operating on the stderr stream from
external programs. This may produce unexpected results if the stderr
stream is not then also printed by something else - nushell currently
doesn't. See #11929 for the fix for that.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

If someone was using the system `tee` command, they might be surprised
to find that it's different.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`


# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated:pr-errors Deprecated: use A:error-handling, A:error-unhelpful, or A:error-silent-fail instead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants