Fix config creation during printing (#9353)

This commit is contained in:
Jakub Žádník 2023-06-04 22:04:28 +03:00 committed by GitHub
parent df15fc24fe
commit 82e6873702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 66 additions and 100 deletions

View file

@ -100,12 +100,7 @@ pub fn eval_config_contents(
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
match nu_engine::env::current_dir(engine_state, stack) { match nu_engine::env::current_dir(engine_state, stack) {
Ok(cwd) => { Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack) { if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
}
if let Err(e) = engine_state.set_current_working_dir(cwd) {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e); report_error(&working_set, &e);
} }

View file

@ -53,12 +53,6 @@ Since this command has no output, there is no point in piping it with other comm
let no_newline = call.has_flag("no-newline"); let no_newline = call.has_flag("no-newline");
let to_stderr = call.has_flag("stderr"); let to_stderr = call.has_flag("stderr");
// We merge stack to make sure we render the changes if any were made in the `block`
//
// CONSIDERED TO BE A CODE SMELL AND IT BETTER BE RESOLVED UPWARDS THE CALLING STACK
let engine = engine_state.clone_with_env(stack)?;
let engine_state = &engine;
// This will allow for easy printing of pipelines as well // This will allow for easy printing of pipelines as well
if !args.is_empty() { if !args.is_empty() {
for arg in args { for arg in args {

View file

@ -172,8 +172,7 @@ pub fn evaluate_repl(
PipelineData::empty(), PipelineData::empty(),
false, false,
); );
engine_state.merge_env(stack)?; engine_state.merge_env(stack, get_guaranteed_cwd(engine_state, stack))?;
engine_state.set_current_working_dir(get_guaranteed_cwd(engine_state, stack))?;
} }
engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64); engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64);
@ -192,18 +191,14 @@ pub fn evaluate_repl(
loop { loop {
let loop_start_time = std::time::Instant::now(); let loop_start_time = std::time::Instant::now();
let cwd = get_guaranteed_cwd(engine_state, stack);
start_time = std::time::Instant::now(); start_time = std::time::Instant::now();
// Before doing anything, merge the environment from the previous REPL iteration into the // Before doing anything, merge the environment from the previous REPL iteration into the
// permanent state. // permanent state.
if let Err(err) = engine_state.merge_env(stack) { if let Err(err) = engine_state.merge_env(stack, cwd) {
report_error_new(engine_state, &err); report_error_new(engine_state, &err);
} }
let cwd = get_guaranteed_cwd(engine_state, stack);
if let Err(err) = engine_state.set_current_working_dir(cwd) {
report_error_new(engine_state, &err);
}
perf( perf(
"merge env", "merge env",
start_time, start_time,

View file

@ -246,10 +246,6 @@ pub fn eval_source(
match b { match b {
Ok(pipeline_data) => { Ok(pipeline_data) => {
// we merge stack here because the block could change the envirenemnt,
// and we need to render it while do print.
let _ = engine_state.merge_env(stack);
let config = engine_state.get_config(); let config = engine_state.get_config();
let result; let result;
if let PipelineData::ExternalStream { if let PipelineData::ExternalStream {

View file

@ -768,8 +768,7 @@ fn run_external_completion(block: &str, input: &str) -> Vec<Suggestion> {
assert!(engine_state.merge_delta(delta).is_ok()); assert!(engine_state.merge_delta(delta).is_ok());
// Merge environment into the permanent state // Merge environment into the permanent state
assert!(engine_state.merge_env(&mut stack).is_ok()); assert!(engine_state.merge_env(&mut stack, &dir).is_ok());
assert!(engine_state.set_current_working_dir(&dir).is_ok());
let latest_block_id = engine_state.num_blocks() - 1; let latest_block_id = engine_state.num_blocks() - 1;

View file

@ -61,8 +61,8 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
); );
// Merge environment into the permanent state // Merge environment into the permanent state
assert!(engine_state.merge_env(&mut stack).is_ok()); let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(engine_state.set_current_working_dir(&dir).is_ok()); assert!(merge_result.is_ok());
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
} }
@ -100,8 +100,8 @@ pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
); );
// Merge environment into the permanent state // Merge environment into the permanent state
assert!(engine_state.merge_env(&mut stack).is_ok()); let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(engine_state.set_current_working_dir(&dir).is_ok()); assert!(merge_result.is_ok());
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
} }
@ -171,8 +171,5 @@ pub fn merge_input(
.is_ok()); .is_ok());
// Merge environment into the permanent state // Merge environment into the permanent state
engine_state.merge_env(stack)?; engine_state.merge_env(stack, &dir)
engine_state.set_current_working_dir(&dir)?;
Ok(())
} }

View file

@ -167,13 +167,9 @@ pub fn check_example_evaluates_to_expected_output(
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy())); stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
engine_state engine_state
.merge_env(&mut stack) .merge_env(&mut stack, cwd)
.expect("Error merging environment"); .expect("Error merging environment");
engine_state
.set_current_working_dir(cwd)
.expect("Error setting CWD");
let empty_input = PipelineData::empty(); let empty_input = PipelineData::empty();
let result = eval(example.example, empty_input, cwd, engine_state); let result = eval(example.example, empty_input, cwd, engine_state);

View file

@ -1,7 +1,7 @@
use crate::text_style::Alignment; use crate::text_style::Alignment;
use crate::{color_record_to_nustyle, lookup_ansi_color_style, TextStyle}; use crate::{color_record_to_nustyle, lookup_ansi_color_style, TextStyle};
use nu_ansi_term::{Color, Style}; use nu_ansi_term::{Color, Style};
use nu_engine::eval_block; use nu_engine::{env::get_config, eval_block};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
CliError, IntoPipelineData, Value, CliError, IntoPipelineData, Value,
@ -138,7 +138,7 @@ impl<'a> StyleComputer<'a> {
// The main constructor. // The main constructor.
pub fn from_config(engine_state: &'a EngineState, stack: &'a Stack) -> StyleComputer<'a> { pub fn from_config(engine_state: &'a EngineState, stack: &'a Stack) -> StyleComputer<'a> {
let config = engine_state.get_config(); let config = get_config(engine_state, stack);
// Create the hashmap // Create the hashmap
#[rustfmt::skip] #[rustfmt::skip]

View file

@ -281,10 +281,8 @@ pub fn eval_hook(
} }
} }
engine_state.merge_env(stack)?;
let cwd = get_guaranteed_cwd(engine_state, stack); let cwd = get_guaranteed_cwd(engine_state, stack);
engine_state.set_current_working_dir(cwd)?; engine_state.merge_env(stack, cwd)?;
Ok(output) Ok(output)
} }

View file

@ -1,7 +1,7 @@
use lscolors::{LsColors, Style}; use lscolors::{LsColors, Style};
use nu_color_config::color_from_hex; use nu_color_config::color_from_hex;
use nu_color_config::{StyleComputer, TextStyle}; use nu_color_config::{StyleComputer, TextStyle};
use nu_engine::{env_to_string, CallExt}; use nu_engine::{env::get_config, env_to_string, CallExt};
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
@ -102,12 +102,6 @@ impl Command for Table {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
// We merge stack to make sure we render the changes if any were made in the `block`
//
// CONSIDERED TO BE A CODE SMELL AND IT BETTER BE RESOLVED UPWARDS THE CALLING STACK
let engine = engine_state.clone_with_env(stack)?;
let engine_state = &engine;
let start_num: Option<i64> = call.get_flag(engine_state, stack, "start-number")?; let start_num: Option<i64> = call.get_flag(engine_state, stack, "start-number")?;
let row_offset = start_num.unwrap_or_default() as usize; let row_offset = start_num.unwrap_or_default() as usize;
let list: bool = call.has_flag("list"); let list: bool = call.has_flag("list");
@ -237,7 +231,7 @@ fn handle_table_command(
term_width: Option<i64>, term_width: Option<i64>,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
let config = engine_state.get_config(); let config = get_config(engine_state, stack);
match input { match input {
PipelineData::ExternalStream { .. } => Ok(input), PipelineData::ExternalStream { .. } => Ok(input),
@ -293,7 +287,7 @@ fn handle_table_command(
table_view, table_view,
term_width, term_width,
ctrlc, ctrlc,
config, &config,
) )
} }
PipelineData::Value(Value::LazyRecord { val, .. }, ..) => { PipelineData::Value(Value::LazyRecord { val, .. }, ..) => {
@ -454,7 +448,7 @@ fn handle_row_stream(
Some(PipelineMetadata { Some(PipelineMetadata {
data_source: DataSource::Ls, data_source: DataSource::Ls,
}) => { }) => {
let config = engine_state.config.clone(); let config = get_config(engine_state, stack);
let ctrlc = ctrlc.clone(); let ctrlc = ctrlc.clone();
let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") { let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
Some(v) => Some(env_to_string("LS_COLORS", &v, engine_state, stack)?), Some(v) => Some(env_to_string("LS_COLORS", &v, engine_state, stack)?),
@ -652,13 +646,13 @@ impl PagingTableCreator {
return Ok(None); return Ok(None);
} }
let config = self.engine_state.get_config(); let config = get_config(&self.engine_state, &self.stack);
let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack); let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack);
let term_width = get_width_param(self.width_param); let term_width = get_width_param(self.width_param);
let ctrlc = self.ctrlc.clone(); let ctrlc = self.ctrlc.clone();
let span = self.head; let span = self.head;
let opts = BuildConfig::new(ctrlc, config, &style_computer, span, term_width); let opts = BuildConfig::new(ctrlc, &config, &style_computer, span, term_width);
let view = TableView::Expanded { let view = TableView::Expanded {
limit, limit,
flatten, flatten,
@ -673,24 +667,24 @@ impl PagingTableCreator {
return Ok(None); return Ok(None);
} }
let config = self.engine_state.get_config(); let config = get_config(&self.engine_state, &self.stack);
let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack); let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack);
let term_width = get_width_param(self.width_param); let term_width = get_width_param(self.width_param);
let ctrlc = self.ctrlc.clone(); let ctrlc = self.ctrlc.clone();
let span = self.head; let span = self.head;
let opts = BuildConfig::new(ctrlc, config, &style_computer, span, term_width); let opts = BuildConfig::new(ctrlc, &config, &style_computer, span, term_width);
build_table_batch(batch, TableView::Collapsed, 0, opts) build_table_batch(batch, TableView::Collapsed, 0, opts)
} }
fn build_general(&mut self, batch: Vec<Value>) -> StringResult { fn build_general(&mut self, batch: Vec<Value>) -> StringResult {
let term_width = get_width_param(self.width_param); let term_width = get_width_param(self.width_param);
let config = &self.engine_state.get_config(); let config = get_config(&self.engine_state, &self.stack);
let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack); let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack);
let ctrlc = self.ctrlc.clone(); let ctrlc = self.ctrlc.clone();
let span = self.head; let span = self.head;
let row_offset = self.row_offset; let row_offset = self.row_offset;
let opts = BuildConfig::new(ctrlc, config, &style_computer, span, term_width); let opts = BuildConfig::new(ctrlc, &config, &style_computer, span, term_width);
build_table_batch(batch, TableView::General, row_offset, opts) build_table_batch(batch, TableView::General, row_offset, opts)
} }
@ -762,7 +756,7 @@ impl Iterator for PagingTableCreator {
match table { match table {
Ok(Some(table)) => { Ok(Some(table)) => {
let table = maybe_strip_color(table, self.engine_state.get_config()); let table = maybe_strip_color(table, &get_config(&self.engine_state, &self.stack));
let mut bytes = table.as_bytes().to_vec(); let mut bytes = table.as_bytes().to_vec();
bytes.push(b'\n'); // nu-table tables don't come with a newline on the end bytes.push(b'\n'); // nu-table tables don't come with a newline on the end
@ -898,7 +892,7 @@ fn create_empty_placeholder(
engine_state: &EngineState, engine_state: &EngineState,
stack: &Stack, stack: &Stack,
) -> String { ) -> String {
let config = engine_state.get_config(); let config = get_config(engine_state, stack);
if !config.table_show_empty { if !config.table_show_empty {
return String::new(); return String::new();
} }
@ -910,7 +904,7 @@ fn create_empty_placeholder(
let out = TableOutput::new(table, false, false); let out = TableOutput::new(table, false, false);
let style_computer = &StyleComputer::from_config(engine_state, stack); let style_computer = &StyleComputer::from_config(engine_state, stack);
let config = create_table_config(config, style_computer, &out); let config = create_table_config(&config, style_computer, &out);
out.table out.table
.draw(config, termwidth) .draw(config, termwidth)

View file

@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use nu_protocol::ast::{Call, Expr, PathMember}; use nu_protocol::ast::{Call, Expr, PathMember};
use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Span, Value, VarId}; use nu_protocol::{Config, PipelineData, ShellError, Span, Value, VarId};
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
@ -303,6 +303,18 @@ pub fn find_in_dirs_env(
Ok(check_dir(lib_dirs).or_else(|| check_dir(lib_dirs_fallback))) Ok(check_dir(lib_dirs).or_else(|| check_dir(lib_dirs_fallback)))
} }
/// Get config
///
/// This combines config stored in permanent state and any runtime updates to the environment. This
/// is the canonical way to fetch config at runtime when you have Stack available.
pub fn get_config(engine_state: &EngineState, stack: &Stack) -> Config {
if let Some(mut config_record) = stack.get_env_var(engine_state, "config") {
config_record.into_config(engine_state.get_config()).0
} else {
engine_state.get_config().clone()
}
}
fn get_converted_value( fn get_converted_value(
engine_state: &EngineState, engine_state: &EngineState,
stack: &Stack, stack: &Stack,

View file

@ -279,7 +279,11 @@ impl EngineState {
} }
/// Merge the environment from the runtime Stack into the engine state /// Merge the environment from the runtime Stack into the engine state
pub fn merge_env(&mut self, stack: &mut Stack) -> Result<(), ShellError> { pub fn merge_env(
&mut self,
stack: &mut Stack,
cwd: impl AsRef<Path>,
) -> Result<(), ShellError> {
for mut scope in stack.env_vars.drain(..) { for mut scope in stack.env_vars.drain(..) {
for (overlay_name, mut env) in scope.drain() { for (overlay_name, mut env) in scope.drain() {
if let Some(env_vars) = self.env_vars.get_mut(&overlay_name) { if let Some(env_vars) = self.env_vars.get_mut(&overlay_name) {
@ -306,27 +310,12 @@ impl EngineState {
} }
} }
Ok(())
}
/// Set a CWD.
pub fn set_current_working_dir(&mut self, cwd: impl AsRef<Path>) -> Result<(), ShellError> {
// TODO: better error // TODO: better error
std::env::set_current_dir(cwd)?; std::env::set_current_dir(cwd)?;
Ok(()) Ok(())
} }
/// Merge the environment from the runtime Stack into the engine state
///
/// A merge which does not consume the stack env.
pub fn clone_with_env(&self, stack: &Stack) -> Result<Self, ShellError> {
let mut engine = self.clone();
let mut stack = stack.clone();
engine.merge_env(&mut stack)?;
Ok(engine)
}
/// Mark a starting point if it is a script (e.g., nu spam.nu) /// Mark a starting point if it is a script (e.g., nu spam.nu)
pub fn start_in_file(&mut self, file_path: Option<&str>) { pub fn start_in_file(&mut self, file_path: Option<&str>) {
self.currently_parsed_cwd = if let Some(path) = file_path { self.currently_parsed_cwd = if let Some(path) = file_path {
@ -1665,6 +1654,10 @@ impl<'a> StateWorkingSet<'a> {
self.permanent_state.get_env_var(name) self.permanent_state.get_env_var(name)
} }
/// Returns a reference to the config stored at permanent state
///
/// At runtime, you most likely want to call nu_engine::env::get_config because this method
/// does not capture environment updates during runtime.
pub fn get_config(&self) -> &Config { pub fn get_config(&self) -> &Config {
&self.permanent_state.config &self.permanent_state.config
} }

View file

@ -98,8 +98,7 @@ use std dirs [
)?; )?;
let cwd = current_dir(engine_state, &stack)?; let cwd = current_dir(engine_state, &stack)?;
engine_state.merge_env(&mut stack)?; engine_state.merge_env(&mut stack, cwd)?;
engine_state.set_current_working_dir(cwd)?;
Ok(()) Ok(())
} }

View file

@ -136,7 +136,18 @@ pub(crate) fn read_default_env_file(engine_state: &mut EngineState, stack: &mut
info!("read_config_file {}:{}:{}", file!(), line!(), column!()); info!("read_config_file {}:{}:{}", file!(), line!(), column!());
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
merge_env(engine_state, stack) match nu_engine::env::current_dir(engine_state, stack) {
Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
}
}
Err(e) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
}
}
} }
fn eval_default_config( fn eval_default_config(
@ -161,18 +172,9 @@ fn eval_default_config(
); );
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
merge_env(engine_state, stack)
}
fn merge_env(engine_state: &mut EngineState, stack: &mut Stack) {
match nu_engine::env::current_dir(engine_state, stack) { match nu_engine::env::current_dir(engine_state, stack) {
Ok(cwd) => { Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack) { if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
}
if let Err(e) = engine_state.set_current_working_dir(cwd) {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e); report_error(&working_set, &e);
} }

View file

@ -188,11 +188,7 @@ pub fn nu_repl() {
// Before doing anything, merge the environment from the previous REPL iteration into the // Before doing anything, merge the environment from the previous REPL iteration into the
// permanent state. // permanent state.
if let Err(err) = engine_state.merge_env(&mut stack) { if let Err(err) = engine_state.merge_env(&mut stack, &cwd) {
outcome_err(&engine_state, &err);
}
if let Err(err) = engine_state.set_current_working_dir(&cwd) {
outcome_err(&engine_state, &err); outcome_err(&engine_state, &err);
} }