nushell/crates/nu-cli/src
WindSoilder b150f9f5d8
Avoid blocking when o+e> redirects too much stderr message (#8784)
# Description

Fixes: #8565

Here is another pr #7240 tried to address the issue, but it works in a
wrong way.

After this change `o+e>` won't redirect all stdout message then stderr
message and it works more like how bash does.

# User-Facing Changes

For the given python code:
```python
# test.py
import sys

print('aa'*300, flush=True)
print('bb'*999999, file=sys.stderr, flush=True)
print('cc'*300, flush=True)
```

Running `python test.py out+err> a.txt` shoudn't hang nushell, and
`a.txt` keeps output in the same order

## About the change
The core idea is that when doing lite-parsing, introduce a new variant
`LiteElement::SameTargetRedirection` if we meet `out+err>` redirection
token(which is generated by lex function),

During converting from lite block to block,
LiteElement::SameTargetRedirection will be converted to
PipelineElement::SameTargetRedirection.

Then in the block eval process, if we get
PipelineElement::SameTargetRedirection, we'll invoke `run-external` with
`--redirect-combine` flag, then pipe the result into save command

## What happened internally?

Take the following command as example:
`^ls o+e> log.txt`

lex parsing result(`Tokens`) are not changed, but `LiteBlock` and
`Block` is changed after this pr.
### LiteBlock before
```rust
LiteBlock {
    block: [
        LitePipeline { commands: [
            Command(None, LiteCommand { comments: [], parts: [Span { start: 39041, end: 39044 }] }),
            // actually the span of first Redirection is wrong too..
            Redirection(Span { start: 39058, end: 39062 }, StdoutAndStderr, LiteCommand { comments: [], parts: [Span { start: 39050, end: 39057 }] }),
        ]
    }]
}
```
### LiteBlock after
```rust
LiteBlock { 
    block: [
        LitePipeline {
            commands: [
                SameTargetRedirection {
                    cmd: (None, LiteCommand { comments: [], parts: [Span { start: 147945, end: 147948}]}),
                    redirection: (Span { start: 147949, end: 147957 }, LiteCommand { comments: [], parts: [Span { start: 147958, end: 147965 }]})
                }
            ]
        }
    ]
}
```
### Block before
```rust
Pipeline {
    elements: [
        Expression(None, Expression {
            expr: ExternalCall(Expression { expr: String("ls"), span: Span { start: 39042, end: 39044 }, ty: String, custom_completion: None }, [], false),
            span: Span { start: 39041, end: 39044 },
            ty: Any, custom_completion: None 
        }),
        Redirection(Span { start: 39058, end: 39062 }, StdoutAndStderr, Expression { expr: String("out.txt"), span: Span { start: 39050, end: 39057 }, ty: String, custom_completion: None })] }
```
### Block after
```rust
Pipeline {
    elements: [
        SameTargetRedirection { 
            cmd: (None, Expression {
                expr: ExternalCall(Expression { expr: String("ls"), span: Span { start: 147946, end: 147948 }, ty: String, custom_completion: None}, [], false),
                span: Span { start: 147945, end: 147948},
                ty: Any, custom_completion: None
            }),
            redirection: (Span { start: 147949, end: 147957}, Expression {expr: String("log.txt"), span: Span { start: 147958, end: 147965 },ty: String,custom_completion: None}
        }
    ]
}
```

# 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` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-utils/standard_library/tests.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.
2023-05-17 17:47:03 -05:00
..
commands fix: use buffer.len() instead of cursor_pos, so the .expect() isn't useless (#9053) 2023-05-08 13:02:01 -05:00
completions Avoid blocking when o+e> redirects too much stderr message (#8784) 2023-05-17 17:47:03 -05:00
menus Parameter defaults to $nu.scope.commands (#9152) 2023-05-11 13:59:56 -05:00
config_files.rs remove nu_cli crate dependency from nu_std (#8807) 2023-04-08 13:53:43 +02:00
eval_cmds.rs Move CLI related commands to nu-cli (#8832) 2023-04-10 10:56:47 +12:00
eval_file.rs Add $env.CURRENT_FILE variable (#8861) 2023-04-13 23:33:29 +03:00
lib.rs Move CLI related commands to nu-cli (#8832) 2023-04-10 10:56:47 +12:00
nu_highlight.rs Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
print.rs print pipeline contents in print (#8305) 2023-03-04 11:31:01 +13:00
prompt.rs FIX: add a space after the default left prompt (#9074) 2023-05-08 13:00:44 -05:00
prompt_update.rs remove nu_cli crate dependency from nu_std (#8807) 2023-04-08 13:53:43 +02:00
reedline_config.rs Remove old alias implementation (#8797) 2023-04-07 21:09:38 +03:00
repl.rs REFACTOR: move the banner from the rust source to the standard library (#8406) 2023-05-10 07:05:01 -05:00
syntax_highlight.rs Avoid blocking when o+e> redirects too much stderr message (#8784) 2023-05-17 17:47:03 -05:00
util.rs remove nu_cli crate dependency from nu_std (#8807) 2023-04-08 13:53:43 +02:00
validation.rs Remove old alias implementation (#8797) 2023-04-07 21:09:38 +03:00