# Description The PR overhauls how IO redirection is handled, allowing more explicit and fine-grain control over `stdout` and `stderr` output as well as more efficient IO and piping. To summarize the changes in this PR: - Added a new `IoStream` type to indicate the intended destination for a pipeline element's `stdout` and `stderr`. - The `stdout` and `stderr` `IoStream`s are stored in the `Stack` and to avoid adding 6 additional arguments to every eval function and `Command::run`. The `stdout` and `stderr` streams can be temporarily overwritten through functions on `Stack` and these functions will return a guard that restores the original `stdout` and `stderr` when dropped. - In the AST, redirections are now directly part of a `PipelineElement` as a `Option<Redirection>` field instead of having multiple different `PipelineElement` enum variants for each kind of redirection. This required changes to the parser, mainly in `lite_parser.rs`. - `Command`s can also set a `IoStream` override/redirection which will apply to the previous command in the pipeline. This is used, for example, in `ignore` to allow the previous external command to have its stdout redirected to `Stdio::null()` at spawn time. In contrast, the current implementation has to create an os pipe and manually consume the output on nushell's side. File and pipe redirections (`o>`, `e>`, `e>|`, etc.) have precedence over overrides from commands. This PR improves piping and IO speed, partially addressing #10763. Using the `throughput` command from that issue, this PR gives the following speedup on my setup for the commands below: | Command | Before (MB/s) | After (MB/s) | Bash (MB/s) | | --------------------------- | -------------:| ------------:| -----------:| | `throughput o> /dev/null` | 1169 | 52938 | 54305 | | `throughput \| ignore` | 840 | 55438 | N/A | | `throughput \| null` | Error | 53617 | N/A | | `throughput \| rg 'x'` | 1165 | 3049 | 3736 | | `(throughput) \| rg 'x'` | 810 | 3085 | 3815 | (Numbers above are the median samples for throughput) This PR also paves the way to refactor our `ExternalStream` handling in the various commands. For example, this PR already fixes the following code: ```nushell ^sh -c 'echo -n "hello "; sleep 0; echo "world"' | find "hello world" ``` This returns an empty list on 0.90.1 and returns a highlighted "hello world" on this PR. Since the `stdout` and `stderr` `IoStream`s are available to commands when they are run, then this unlocks the potential for more convenient behavior. E.g., the `find` command can disable its ansi highlighting if it detects that the output `IoStream` is not the terminal. Knowing the output streams will also allow background job output to be redirected more easily and efficiently. # User-Facing Changes - External commands returned from closures will be collected (in most cases): ```nushell 1..2 | each {|_| nu -c "print a" } ``` This gives `["a", "a"]` on this PR, whereas this used to print "a\na\n" and then return an empty list. ```nushell 1..2 | each {|_| nu -c "print -e a" } ``` This gives `["", ""]` and prints "a\na\n" to stderr, whereas this used to return an empty list and print "a\na\n" to stderr. - Trailing new lines are always trimmed for external commands when piping into internal commands or collecting it as a value. (Failure to decode the output as utf-8 will keep the trailing newline for the last binary value.) In the current nushell version, the following three code snippets differ only in parenthesis placement, but they all also have different outputs: 1. `1..2 | each { ^echo a }` ``` a a ╭────────────╮ │ empty list │ ╰────────────╯ ``` 2. `1..2 | each { (^echo a) }` ``` ╭───┬───╮ │ 0 │ a │ │ 1 │ a │ ╰───┴───╯ ``` 3. `1..2 | (each { ^echo a })` ``` ╭───┬───╮ │ 0 │ a │ │ │ │ │ 1 │ a │ │ │ │ ╰───┴───╯ ``` But in this PR, the above snippets will all have the same output: ``` ╭───┬───╮ │ 0 │ a │ │ 1 │ a │ ╰───┴───╯ ``` - All existing flags on `run-external` are now deprecated. - File redirections now apply to all commands inside a code block: ```nushell (nu -c "print -e a"; nu -c "print -e b") e> test.out ``` This gives "a\nb\n" in `test.out` and prints nothing. The same result would happen when printing to stdout and using a `o>` file redirection. - External command output will (almost) never be ignored, and ignoring output must be explicit now: ```nushell (^echo a; ^echo b) ``` This prints "a\nb\n", whereas this used to print only "b\n". This only applies to external commands; values and internal commands not in return position will not print anything (e.g., `(echo a; echo b)` still only prints "b"). - `complete` now always captures stderr (`do` is not necessary). # After Submitting The language guide and other documentation will need to be updated.
257 lines
8.1 KiB
Rust
257 lines
8.1 KiB
Rust
use crate::util::eval_source;
|
|
use log::info;
|
|
use log::trace;
|
|
use miette::{IntoDiagnostic, Result};
|
|
use nu_engine::eval_block;
|
|
use nu_engine::{convert_env_values, current_dir};
|
|
use nu_parser::parse;
|
|
use nu_path::canonicalize_with;
|
|
use nu_protocol::debugger::WithoutDebug;
|
|
use nu_protocol::report_error;
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
|
Config, PipelineData, ShellError, Span, Value,
|
|
};
|
|
use nu_utils::stdout_write_all_and_flush;
|
|
|
|
/// Main function used when a file path is found as argument for nu
|
|
pub fn evaluate_file(
|
|
path: String,
|
|
args: &[String],
|
|
engine_state: &mut EngineState,
|
|
stack: &mut Stack,
|
|
input: PipelineData,
|
|
) -> Result<()> {
|
|
// Translate environment variables from Strings to Values
|
|
if let Some(e) = convert_env_values(engine_state, stack) {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &e);
|
|
std::process::exit(1);
|
|
}
|
|
|
|
let cwd = current_dir(engine_state, stack)?;
|
|
|
|
let file_path = canonicalize_with(&path, cwd).unwrap_or_else(|e| {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(
|
|
&working_set,
|
|
&ShellError::FileNotFoundCustom {
|
|
msg: format!("Could not access file '{}': {:?}", path, e.to_string()),
|
|
span: Span::unknown(),
|
|
},
|
|
);
|
|
std::process::exit(1);
|
|
});
|
|
|
|
let file_path_str = file_path.to_str().unwrap_or_else(|| {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(
|
|
&working_set,
|
|
&ShellError::NonUtf8Custom {
|
|
msg: format!(
|
|
"Input file name '{}' is not valid UTF8",
|
|
file_path.to_string_lossy()
|
|
),
|
|
span: Span::unknown(),
|
|
},
|
|
);
|
|
std::process::exit(1);
|
|
});
|
|
|
|
let file = std::fs::read(&file_path)
|
|
.into_diagnostic()
|
|
.unwrap_or_else(|e| {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(
|
|
&working_set,
|
|
&ShellError::FileNotFoundCustom {
|
|
msg: format!(
|
|
"Could not read file '{}': {:?}",
|
|
file_path_str,
|
|
e.to_string()
|
|
),
|
|
span: Span::unknown(),
|
|
},
|
|
);
|
|
std::process::exit(1);
|
|
});
|
|
|
|
engine_state.start_in_file(Some(file_path_str));
|
|
|
|
let parent = file_path.parent().unwrap_or_else(|| {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(
|
|
&working_set,
|
|
&ShellError::FileNotFoundCustom {
|
|
msg: format!("The file path '{file_path_str}' does not have a parent"),
|
|
span: Span::unknown(),
|
|
},
|
|
);
|
|
std::process::exit(1);
|
|
});
|
|
|
|
stack.add_env_var(
|
|
"FILE_PWD".to_string(),
|
|
Value::string(parent.to_string_lossy(), Span::unknown()),
|
|
);
|
|
stack.add_env_var(
|
|
"CURRENT_FILE".to_string(),
|
|
Value::string(file_path.to_string_lossy(), Span::unknown()),
|
|
);
|
|
stack.add_env_var(
|
|
"PROCESS_PATH".to_string(),
|
|
Value::string(path, Span::unknown()),
|
|
);
|
|
|
|
let source_filename = file_path
|
|
.file_name()
|
|
.expect("internal error: script missing filename");
|
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
|
trace!("parsing file: {}", file_path_str);
|
|
let block = parse(&mut working_set, Some(file_path_str), &file, false);
|
|
|
|
if let Some(err) = working_set.parse_errors.first() {
|
|
report_error(&working_set, err);
|
|
std::process::exit(1);
|
|
}
|
|
|
|
for block in &mut working_set.delta.blocks {
|
|
if block.signature.name == "main" {
|
|
block.signature.name = source_filename.to_string_lossy().to_string();
|
|
} else if block.signature.name.starts_with("main ") {
|
|
block.signature.name =
|
|
source_filename.to_string_lossy().to_string() + " " + &block.signature.name[5..];
|
|
}
|
|
}
|
|
|
|
let _ = engine_state.merge_delta(working_set.delta);
|
|
|
|
if engine_state.find_decl(b"main", &[]).is_some() {
|
|
let args = format!("main {}", args.join(" "));
|
|
|
|
let pipeline_data =
|
|
eval_block::<WithoutDebug>(engine_state, stack, &block, PipelineData::empty());
|
|
let pipeline_data = match pipeline_data {
|
|
Err(ShellError::Return { .. }) => {
|
|
// allows early exists before `main` is run.
|
|
return Ok(());
|
|
}
|
|
|
|
x => x,
|
|
}
|
|
.unwrap_or_else(|e| {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &e);
|
|
std::process::exit(1);
|
|
});
|
|
|
|
let result = pipeline_data.print(engine_state, stack, true, false);
|
|
|
|
match result {
|
|
Err(err) => {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
std::process::exit(1);
|
|
}
|
|
Ok(exit_code) => {
|
|
if exit_code != 0 {
|
|
std::process::exit(exit_code as i32);
|
|
}
|
|
}
|
|
}
|
|
|
|
if !eval_source(
|
|
engine_state,
|
|
stack,
|
|
args.as_bytes(),
|
|
"<commandline>",
|
|
input,
|
|
true,
|
|
) {
|
|
std::process::exit(1);
|
|
}
|
|
} else if !eval_source(engine_state, stack, &file, file_path_str, input, true) {
|
|
std::process::exit(1);
|
|
}
|
|
|
|
info!("evaluate {}:{}:{}", file!(), line!(), column!());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn print_table_or_error(
|
|
engine_state: &mut EngineState,
|
|
stack: &mut Stack,
|
|
mut pipeline_data: PipelineData,
|
|
config: &mut Config,
|
|
) -> Option<i64> {
|
|
let exit_code = match &mut pipeline_data {
|
|
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
|
|
_ => None,
|
|
};
|
|
|
|
// Change the engine_state config to use the passed in configuration
|
|
engine_state.set_config(config.clone());
|
|
|
|
if let PipelineData::Value(Value::Error { error, .. }, ..) = &pipeline_data {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &**error);
|
|
std::process::exit(1);
|
|
}
|
|
|
|
if let Some(decl_id) = engine_state.find_decl("table".as_bytes(), &[]) {
|
|
let command = engine_state.get_decl(decl_id);
|
|
if command.get_block_id().is_some() {
|
|
print_or_exit(pipeline_data, engine_state, config);
|
|
} else {
|
|
// The final call on table command, it's ok to set redirect_output to false.
|
|
let call = Call::new(Span::new(0, 0));
|
|
let table = command.run(engine_state, stack, &call, pipeline_data);
|
|
|
|
match table {
|
|
Ok(table) => {
|
|
print_or_exit(table, engine_state, config);
|
|
}
|
|
Err(error) => {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &error);
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
print_or_exit(pipeline_data, engine_state, config);
|
|
}
|
|
|
|
// Make sure everything has finished
|
|
if let Some(exit_code) = exit_code {
|
|
let mut exit_code: Vec<_> = exit_code.into_iter().collect();
|
|
exit_code
|
|
.pop()
|
|
.and_then(|last_exit_code| match last_exit_code {
|
|
Value::Int { val: code, .. } => Some(code),
|
|
_ => None,
|
|
})
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) {
|
|
for item in pipeline_data {
|
|
if let Value::Error { error, .. } = item {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &*error);
|
|
|
|
std::process::exit(1);
|
|
}
|
|
|
|
let out = item.to_expanded_string("\n", config) + "\n";
|
|
let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{err}"));
|
|
}
|
|
}
|