From 676457acd32f530bbddb36425259e8d9ae984ada Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Wed, 23 Feb 2022 10:54:47 -0500 Subject: [PATCH] Better ls paths (#4612) * Fix ls paths... again * Fix ls paths... again * Always expand paths inside of glob_from * Expand in ls before we check for directory info --- crates/nu-command/src/filesystem/ls.rs | 19 ++++++++++--------- crates/nu-engine/src/glob_from.rs | 6 +----- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/crates/nu-command/src/filesystem/ls.rs b/crates/nu-command/src/filesystem/ls.rs index 49f426e63..d60df4ca0 100644 --- a/crates/nu-command/src/filesystem/ls.rs +++ b/crates/nu-command/src/filesystem/ls.rs @@ -29,11 +29,8 @@ impl Command for Ls { fn signature(&self) -> nu_protocol::Signature { Signature::build("ls") - .optional( - "pattern", - SyntaxShape::GlobPattern, - "the glob pattern to use", - ) + // Using a string instead of a glob pattern shape so it won't auto-expand + .optional("pattern", SyntaxShape::String, "the glob pattern to use") .switch("all", "Show hidden files", Some('a')) .switch( "long", @@ -69,18 +66,22 @@ impl Command for Ls { let ctrl_c = engine_state.ctrlc.clone(); let call_span = call.head; let cwd = current_dir(engine_state, stack)?; - let pattern_arg = call.opt::>(engine_state, stack, 0)?; + + let pattern_arg: Option> = call.opt(engine_state, stack, 0)?; let (path, p_tag, absolute_path) = match pattern_arg { Some(p) => { let p_tag = p.span; let mut p = PathBuf::from(p.item); - if p.is_dir() { + + let expanded = nu_path::expand_path_with(&p, &cwd); + if expanded.is_dir() { if permission_denied(&p) { #[cfg(unix)] let error_msg = format!( "The permissions of {:o} do not allow access for this user", - p.metadata() + expanded + .metadata() .expect( "this shouldn't be called since we already know there is a dir" ) @@ -96,7 +97,7 @@ impl Command for Ls { p_tag, )); } - if is_empty_dir(&p) { + if is_empty_dir(&expanded) { return Ok(Value::nothing(call_span).into_pipeline_data()); } p.push("*"); diff --git a/crates/nu-engine/src/glob_from.rs b/crates/nu-engine/src/glob_from.rs index 2edf51150..054ca6be2 100644 --- a/crates/nu-engine/src/glob_from.rs +++ b/crates/nu-engine/src/glob_from.rs @@ -25,11 +25,7 @@ pub fn glob_from( ShellError, > { let path = PathBuf::from(&pattern.item); - let path = if path.is_relative() { - expand_path_with(path, cwd) - } else { - path - }; + let path = expand_path_with(path, cwd); let (prefix, pattern) = if path.to_string_lossy().contains('*') { // Path is a glob pattern => do not check for existence