diff --git a/crates/nu-command/src/path/expand.rs b/crates/nu-command/src/path/expand.rs index dda62aa070..2cae2a1649 100644 --- a/crates/nu-command/src/path/expand.rs +++ b/crates/nu-command/src/path/expand.rs @@ -111,11 +111,12 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value { Value::string(p.to_string_lossy(), span) } else if args.strict { Value::Error { - error: ShellError::LabeledError( + error: ShellError::SpannedLabeledError( "Could not expand path".into(), "could not be expanded (path might not exist, non-final \ component is not a directory, or other cause)" .into(), + span, ), } } else { diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 70faf4b8a1..20ec14b200 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -251,7 +251,7 @@ pub enum ShellError { SpannedLabeledErrorHelp(String, String, #[label("{1}")] Span, String), #[error("{0}")] - #[diagnostic()] + #[diagnostic(help("{1}"))] LabeledError(String, String), } diff --git a/src/main.rs b/src/main.rs index 3517afb58f..06c2c0b36e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -434,11 +434,30 @@ fn main() -> Result<()> { fn gather_parent_env_vars(engine_state: &mut EngineState, stack: &mut Stack) { let mut fake_env_file = String::new(); for (name, val) in std::env::vars() { + let c = if val.contains('"') { + if val.contains('\'') { + // environment variable containing both ' and " is ignored + let working_set = StateWorkingSet::new(engine_state); + report_error( + &working_set, + &ShellError::LabeledError( + format!("Environment variable was not captured: {}={}", name, val), + "Value should not contain both ' and \" at the same time.".into(), + ), + ); + continue; + } else { + '\'' + } + } else { + '"' + }; + fake_env_file.push_str(&name); fake_env_file.push('='); - fake_env_file.push('"'); + fake_env_file.push(c); fake_env_file.push_str(&val); - fake_env_file.push('"'); + fake_env_file.push(c); fake_env_file.push('\n'); } @@ -474,14 +493,34 @@ fn gather_parent_env_vars(engine_state: &mut EngineState, stack: &mut Stack) { }) = parts.get(2) { let bytes = engine_state.get_span_contents(span); - let bytes = bytes.strip_prefix(&[b'"']).unwrap_or(bytes); - let bytes = bytes.strip_suffix(&[b'"']).unwrap_or(bytes); + + if bytes.len() < 2 { + let working_set = StateWorkingSet::new(engine_state); + report_error( + &working_set, + &ShellError::NushellFailed(format!( + "Error capturing environment variable {}", + name + )), + ); + } + + let bytes = &bytes[1..bytes.len() - 1]; Value::String { val: String::from_utf8_lossy(bytes).to_string(), span: *span, } } else { + let working_set = StateWorkingSet::new(engine_state); + report_error( + &working_set, + &ShellError::NushellFailed(format!( + "Error capturing environment variable {}", + name + )), + ); + Value::String { val: "".to_string(), span: Span::new(full_span.end, full_span.end),