1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use nu_engine::{command_prelude::*, get_eval_block_with_early_return};
use nu_protocol::{engine::Closure, IoStream, RawStream};
use std::{sync::mpsc, thread};

#[derive(Clone)]
pub struct Tee;

impl Command for Tee {
    fn name(&self) -> &str {
        "tee"
    }

    fn usage(&self) -> &str {
        "Copy a stream to another command in parallel."
    }

    fn extra_usage(&self) -> &str {
        r#"This is useful for doing something else with a stream while still continuing to
use it in your pipeline."#
    }

    fn signature(&self) -> Signature {
        Signature::build("tee")
            .input_output_type(Type::Any, Type::Any)
            .switch(
                "stderr",
                "For external commands: copy the standard error stream instead.",
                Some('e'),
            )
            .required(
                "closure",
                SyntaxShape::Closure(None),
                "The other command to send the stream to.",
            )
            .category(Category::Filters)
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                example: "http get http://example.org/ | tee { save example.html }",
                description: "Save a webpage to a file while also printing it",
                result: None,
            },
            Example {
                example:
                    "nu -c 'print -e error; print ok' | tee --stderr { save error.log } | complete",
                description: "Save error messages from an external command to a file without \
                    redirecting them",
                result: None,
            },
            Example {
                example: "1..100 | tee { each { print } } | math sum | wrap sum",
                description: "Print numbers and their sum",
                result: None,
            },
        ]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let use_stderr = call.has_flag(engine_state, stack, "stderr")?;

        let Spanned {
            item: Closure { block_id, captures },
            span: closure_span,
        } = call.req(engine_state, stack, 0)?;

        let closure_engine_state = engine_state.clone();
        let mut closure_stack = stack
            .captures_to_stack_preserve_stdio(captures)
            .reset_pipes();

        let metadata = input.metadata();
        let metadata_clone = metadata.clone();

        let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);

        match input {
            // Handle external streams specially, to make sure they pass through
            PipelineData::ExternalStream {
                stdout,
                stderr,
                exit_code,
                span,
                metadata,
                trim_end_newline,
            } => {
                let known_size = if use_stderr {
                    stderr.as_ref().and_then(|s| s.known_size)
                } else {
                    stdout.as_ref().and_then(|s| s.known_size)
                };

                let with_stream = move |rx: mpsc::Receiver<Result<Vec<u8>, ShellError>>| {
                    let iter = rx.into_iter();
                    let input_from_channel = PipelineData::ExternalStream {
                        stdout: Some(RawStream::new(
                            Box::new(iter),
                            closure_engine_state.ctrlc.clone(),
                            span,
                            known_size,
                        )),
                        stderr: None,
                        exit_code: None,
                        span,
                        metadata: metadata_clone,
                        trim_end_newline,
                    };
                    let result = eval_block_with_early_return(
                        &closure_engine_state,
                        &mut closure_stack,
                        closure_engine_state.get_block(block_id),
                        input_from_channel,
                    );
                    // Make sure to drain any iterator produced to avoid unexpected behavior
                    result.and_then(|data| data.drain())
                };

                if use_stderr {
                    let stderr = stderr
                        .map(|stderr| {
                            let iter = tee(stderr.stream, with_stream)
                                .map_err(|e| e.into_spanned(call.head))?;
                            Ok::<_, ShellError>(RawStream::new(
                                Box::new(iter.map(flatten_result)),
                                stderr.ctrlc,
                                stderr.span,
                                stderr.known_size,
                            ))
                        })
                        .transpose()?;
                    Ok(PipelineData::ExternalStream {
                        stdout,
                        stderr,
                        exit_code,
                        span,
                        metadata,
                        trim_end_newline,
                    })
                } else {
                    let stdout = stdout
                        .map(|stdout| {
                            let iter = tee(stdout.stream, with_stream)
                                .map_err(|e| e.into_spanned(call.head))?;
                            Ok::<_, ShellError>(RawStream::new(
                                Box::new(iter.map(flatten_result)),
                                stdout.ctrlc,
                                stdout.span,
                                stdout.known_size,
                            ))
                        })
                        .transpose()?;
                    Ok(PipelineData::ExternalStream {
                        stdout,
                        stderr,
                        exit_code,
                        span,
                        metadata,
                        trim_end_newline,
                    })
                }
            }
            // --stderr is not allowed if the input is not an external stream
            _ if use_stderr => Err(ShellError::UnsupportedInput {
                msg: "--stderr can only be used on external streams".into(),
                input: "the input to `tee` is not an external stream".into(),
                msg_span: call.head,
                input_span: input.span().unwrap_or(call.head),
            }),
            // Handle others with the plain iterator
            _ => {
                let teed = tee(input.into_iter(), move |rx| {
                    let input_from_channel = rx.into_pipeline_data_with_metadata(
                        metadata_clone,
                        closure_engine_state.ctrlc.clone(),
                    );
                    let result = eval_block_with_early_return(
                        &closure_engine_state,
                        &mut closure_stack,
                        closure_engine_state.get_block(block_id),
                        input_from_channel,
                    );
                    // Make sure to drain any iterator produced to avoid unexpected behavior
                    result.and_then(|data| data.drain())
                })
                .map_err(|e| e.into_spanned(call.head))?
                .map(move |result| result.unwrap_or_else(|err| Value::error(err, closure_span)))
                .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone());

                Ok(teed)
            }
        }
    }

    fn stdio_redirect(&self) -> (Option<IoStream>, Option<IoStream>) {
        (Some(IoStream::Capture), Some(IoStream::Capture))
    }
}

fn panic_error() -> ShellError {
    ShellError::NushellFailed {
        msg: "A panic occurred on a thread spawned by `tee`".into(),
    }
}

fn flatten_result<T, E>(result: Result<Result<T, E>, E>) -> Result<T, E> {
    result.unwrap_or_else(Err)
}

/// Copies the iterator to a channel on another thread. If an error is produced on that thread,
/// it is embedded in the resulting iterator as an `Err` as soon as possible. When the iterator
/// finishes, it waits for the other thread to finish, also handling any error produced at that
/// point.
fn tee<T>(
    input: impl Iterator<Item = T>,
    with_cloned_stream: impl FnOnce(mpsc::Receiver<T>) -> Result<(), ShellError> + Send + 'static,
) -> Result<impl Iterator<Item = Result<T, ShellError>>, std::io::Error>
where
    T: Clone + Send + 'static,
{
    // For sending the values to the other thread
    let (tx, rx) = mpsc::channel();

    let mut thread = Some(
        thread::Builder::new()
            .name("stderr consumer".into())
            .spawn(move || with_cloned_stream(rx))?,
    );

    let mut iter = input.into_iter();
    let mut tx = Some(tx);

    Ok(std::iter::from_fn(move || {
        if thread.as_ref().is_some_and(|t| t.is_finished()) {
            // Check for an error from the other thread
            let result = thread
                .take()
                .expect("thread was taken early")
                .join()
                .unwrap_or_else(|_| Err(panic_error()));
            if let Err(err) = result {
                // Embed the error early
                return Some(Err(err));
            }
        }

        // Get a value from the iterator
        if let Some(value) = iter.next() {
            // Send a copy, ignoring any error if the channel is closed
            let _ = tx.as_ref().map(|tx| tx.send(value.clone()));
            Some(Ok(value))
        } else {
            // Close the channel so the stream ends for the other thread
            drop(tx.take());
            // Wait for the other thread, and embed any error produced
            thread.take().and_then(|t| {
                t.join()
                    .unwrap_or_else(|_| Err(panic_error()))
                    .err()
                    .map(Err)
            })
        }
    }))
}

#[test]
fn tee_copies_values_to_other_thread_and_passes_them_through() {
    let (tx, rx) = mpsc::channel();

    let expected_values = vec![1, 2, 3, 4];

    let my_result = tee(expected_values.clone().into_iter(), move |rx| {
        for val in rx {
            let _ = tx.send(val);
        }
        Ok(())
    })
    .expect("io error")
    .collect::<Result<Vec<i32>, ShellError>>()
    .expect("should not produce error");

    assert_eq!(expected_values, my_result);

    let other_threads_result = rx.into_iter().collect::<Vec<_>>();

    assert_eq!(expected_values, other_threads_result);
}

#[test]
fn tee_forwards_errors_back_immediately() {
    use std::time::Duration;
    let slow_input = (0..100).inspect(|_| std::thread::sleep(Duration::from_millis(1)));
    let iter = tee(slow_input, |_| {
        Err(ShellError::IOError { msg: "test".into() })
    })
    .expect("io error");
    for result in iter {
        if let Ok(val) = result {
            // should not make it to the end
            assert!(val < 99, "the error did not come early enough");
        } else {
            // got the error
            return;
        }
    }
    panic!("never received the error");
}

#[test]
fn tee_waits_for_the_other_thread() {
    use std::sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    };
    use std::time::Duration;
    let waited = Arc::new(AtomicBool::new(false));
    let waited_clone = waited.clone();
    let iter = tee(0..100, move |_| {
        std::thread::sleep(Duration::from_millis(10));
        waited_clone.store(true, Ordering::Relaxed);
        Err(ShellError::IOError { msg: "test".into() })
    })
    .expect("io error");
    let last = iter.last();
    assert!(waited.load(Ordering::Relaxed), "failed to wait");
    assert!(
        last.is_some_and(|res| res.is_err()),
        "failed to return error from wait"
    );
}