save command: Don't use BufWriter to write external strem to a file (#9377)

# Description
Fixes: #9293

The problem is caused by `save` makes a `BufferWriter` for output file,
when external commands redirect it's output to a file, the content is
bufferred first...

To fix the issue, I'd like to introduce a `--no-buf` flag for `save`
command, and it's only used in redirection scenario.

Sorry it's hard to test against it in test, because it requires external
command to sleep or pause...

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

# 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 -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` 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
> ```
-->

# 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.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
WindSoilder 2023-06-10 16:07:26 +08:00 committed by GitHub
parent ce71ea0b5c
commit 191cd2c970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ use nu_protocol::{
Type, Value, Type, Value,
}; };
use std::fs::File; use std::fs::File;
use std::io::{BufWriter, Write}; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::thread; use std::thread;
@ -349,7 +349,9 @@ fn stream_to_file(
span: Span, span: Span,
progress: bool, progress: bool,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let mut writer = BufWriter::new(file); // https://github.com/nushell/nushell/pull/9377 contains the reason
// for not using BufWriter<File>
let mut writer = file;
let mut bytes_processed: u64 = 0; let mut bytes_processed: u64 = 0;
let bytes_processed_p = &mut bytes_processed; let bytes_processed_p = &mut bytes_processed;