From 4105255a5a120f0d31153cef635dec6273b467b5 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 24 Jan 2024 07:02:53 -0800 Subject: [PATCH] resolving external highlight should take current PATH into account (#11618) # Description When resolving external commands, the current `PATH` was not taken into account. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-cli/src/nu_highlight.rs | 3 ++- crates/nu-cli/src/repl.rs | 1 + crates/nu-cli/src/syntax_highlight.rs | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/nu_highlight.rs b/crates/nu-cli/src/nu_highlight.rs index 89b7f88509..ba533515d2 100644 --- a/crates/nu-cli/src/nu_highlight.rs +++ b/crates/nu-cli/src/nu_highlight.rs @@ -28,7 +28,7 @@ impl Command for NuHighlight { fn run( &self, engine_state: &EngineState, - _stack: &mut Stack, + stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { @@ -40,6 +40,7 @@ impl Command for NuHighlight { let highlighter = crate::NuHighlighter { engine_state, + stack: std::sync::Arc::new(stack.clone()), config, }; diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index c4d04bf3dc..60e2b44e52 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -236,6 +236,7 @@ pub fn evaluate_repl( .use_bracketed_paste(cfg!(not(target_os = "windows")) && config.bracketed_paste) .with_highlighter(Box::new(NuHighlighter { engine_state: engine_reference.clone(), + stack: std::sync::Arc::new(stack.clone()), config: config.clone(), })) .with_validator(Box::new(NuValidator { diff --git a/crates/nu-cli/src/syntax_highlight.rs b/crates/nu-cli/src/syntax_highlight.rs index 81e4d2a093..432db0eee5 100644 --- a/crates/nu-cli/src/syntax_highlight.rs +++ b/crates/nu-cli/src/syntax_highlight.rs @@ -1,15 +1,17 @@ use log::trace; use nu_ansi_term::Style; use nu_color_config::{get_matching_brackets_style, get_shape_color}; +use nu_engine::env; use nu_parser::{flatten_block, parse, FlatShape}; use nu_protocol::ast::{Argument, Block, Expr, Expression, PipelineElement, RecordItem}; -use nu_protocol::engine::{EngineState, StateWorkingSet}; +use nu_protocol::engine::{EngineState, Stack, StateWorkingSet}; use nu_protocol::{Config, Span}; use reedline::{Highlighter, StyledText}; use std::sync::Arc; pub struct NuHighlighter { pub engine_state: Arc, + pub stack: Arc, pub config: Config, } @@ -32,7 +34,17 @@ impl Highlighter for NuHighlighter { working_set.get_span_contents(Span::new(span.start, span.end)); let str_word = String::from_utf8_lossy(str_contents).to_string(); - if which::which(str_word).ok().is_some() { + let paths = env::path_str(&self.engine_state, &self.stack, *span).ok(); + let res = if let Ok(cwd) = + env::current_dir_str(&self.engine_state, &self.stack) + { + which::which_in(str_word, paths.as_ref(), cwd).ok() + } else { + which::which_in_global(str_word, paths.as_ref()) + .ok() + .and_then(|mut i| i.next()) + }; + if res.is_some() { *shape = FlatShape::ExternalResolved; } }