From 2f4f8632ded0a5f71b7304845496d9d3ad5bbe70 Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Tue, 16 Jun 2020 11:01:11 +0200 Subject: [PATCH] nicer errors --- .../src/env/directory_specific_environment.rs | 18 ++++++++++--- crates/nu-cli/src/env/environment.rs | 25 ++++++++++--------- crates/nu-cli/src/env/environment_syncer.rs | 6 +++-- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/crates/nu-cli/src/env/directory_specific_environment.rs b/crates/nu-cli/src/env/directory_specific_environment.rs index fd25a284a0..2b4a6b01b0 100644 --- a/crates/nu-cli/src/env/directory_specific_environment.rs +++ b/crates/nu-cli/src/env/directory_specific_environment.rs @@ -8,6 +8,7 @@ use std::{ io::{Error, ErrorKind, Result}, path::PathBuf, }; +use nu_errors::ShellError; type EnvKey = String; type EnvVal = OsString; @@ -82,12 +83,23 @@ impl DirectorySpecificEnvironment { Ok(keyvals_to_restore) } - pub fn env_vars_to_add(&mut self) -> Result> { + pub fn env_vars_to_add(&mut self) -> std::result::Result, ShellError> { let current_dir = std::env::current_dir()?; let mut working_dir = Some(current_dir.as_path()); let mut vars_to_add = IndexMap::new(); + // let mut file = OpenOptions::new() + // .write(true) + // .append(true) + // .create(true) + // .open("toadd.txt") + // .unwrap( + // ); + + // write!(&mut file, "1: {:?}\n", vars_to_add).unwrap(); + //WE CRASHING SOMEWHERE HERE + //Start in the current directory, then traverse towards the root with working_dir to see if we are in a subdirectory of a valid directory. while let Some(wdir) = working_dir { if self.allowed_directories.contains(wdir) { @@ -96,9 +108,9 @@ impl DirectorySpecificEnvironment { toml_doc .get("env") - .ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing"))? + .ok_or_else(|| Err(ShellError::untagged_runtime_error("env section missing")))? .as_table() - .ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section malformed"))? + .ok_or_else(|| Err(ShellError::untagged_runtime_error("env section malformed")))? .iter() .for_each(|(directory_env_key, directory_env_val)| { if !vars_to_add.contains_key(directory_env_key) { diff --git a/crates/nu-cli/src/env/environment.rs b/crates/nu-cli/src/env/environment.rs index 0cca777255..d1fb49fb72 100644 --- a/crates/nu-cli/src/env/environment.rs +++ b/crates/nu-cli/src/env/environment.rs @@ -60,20 +60,21 @@ impl Environment { } pub fn maintain_directory_environment(&mut self) -> std::io::Result<()> { - self.direnv.env_vars_to_delete()?.iter().for_each(|k| { - self.remove_env(&k); - }); + // self.direnv.env_vars_to_delete()?.iter().for_each(|k| { + // self.remove_env(&k); + // }); - self.direnv - .overwritten_values_to_restore()? - .iter() - .for_each(|(k, v)| { - self.add_env(&k, &v.to_string_lossy(), true); - }); + // self.direnv + // .overwritten_values_to_restore()? + // .iter() + // .for_each(|(k, v)| { + // self.add_env(&k, &v.to_string_lossy(), true); + // }); - self.direnv.env_vars_to_add()?.iter().for_each(|(k, v)| { - self.add_env(&k, &v.to_string_lossy(), true); - }); + // self.direnv.env_vars_to_add()?.iter().for_each(|(k, v)| { + // std::env::set_var(k, v); + // self.add_env(&k, &v.to_string_lossy(), true); + // }); Ok(()) } diff --git a/crates/nu-cli/src/env/environment_syncer.rs b/crates/nu-cli/src/env/environment_syncer.rs index 7c378ea47e..195a733c4a 100644 --- a/crates/nu-cli/src/env/environment_syncer.rs +++ b/crates/nu-cli/src/env/environment_syncer.rs @@ -3,6 +3,7 @@ use crate::data::config::{Conf, NuConfig}; use crate::env::environment::{Env, Environment}; use parking_lot::Mutex; use std::sync::Arc; +use nu_errors::ShellError; pub struct EnvironmentSyncer { pub env: Arc>>, @@ -41,10 +42,9 @@ impl EnvironmentSyncer { environment.morph(&*self.config); } - pub fn sync_env_vars(&mut self, ctx: &mut Context) { + pub fn sync_env_vars(&mut self, ctx: &mut Context) -> Result<(), ShellError> { let mut environment = self.env.lock(); - environment.maintain_directory_environment().ok(); if environment.env().is_some() { for (name, value) in ctx.with_host(|host| host.vars()) { if name != "path" && name != "PATH" { @@ -52,6 +52,7 @@ impl EnvironmentSyncer { // that aren't loaded from config. environment.add_env(&name, &value, false); + environment.maintain_directory_environment()?; // clear the env var from the session // we are about to replace them @@ -72,6 +73,7 @@ impl EnvironmentSyncer { } } } + Ok(()) } pub fn sync_path_vars(&mut self, ctx: &mut Context) {