diff --git a/crates/nu-command/src/filesystem/ls.rs b/crates/nu-command/src/filesystem/ls.rs index a595f4f506..c28af6ba5e 100644 --- a/crates/nu-command/src/filesystem/ls.rs +++ b/crates/nu-command/src/filesystem/ls.rs @@ -90,6 +90,13 @@ impl Command for Ls { let pattern_arg = opt_for_glob_pattern(engine_state, stack, call, 0)?; let pattern_arg = { if let Some(path) = pattern_arg { + // it makes no sense to list an empty string. + if path.item.as_ref().is_empty() { + return Err(ShellError::FileNotFoundCustom { + msg: "empty string('') directory or file does not exist".to_string(), + span: path.span, + }); + } match path.item { NuGlob::DoNotExpand(p) => Some(Spanned { item: NuGlob::DoNotExpand(nu_utils::strip_ansi_string_unlikely(p)), diff --git a/crates/nu-command/tests/commands/ls.rs b/crates/nu-command/tests/commands/ls.rs index 2fd1f3e216..14fd2d69ef 100644 --- a/crates/nu-command/tests/commands/ls.rs +++ b/crates/nu-command/tests/commands/ls.rs @@ -701,3 +701,13 @@ fn list_flag_false() { assert_eq!(actual.out, "false"); }) } + +#[test] +fn list_empty_string() { + Playground::setup("ls_empty_string", |dirs, sandbox| { + sandbox.with_files(vec![EmptyFile("yehuda.txt")]); + + let actual = nu!(cwd: dirs.test(), "ls ''"); + assert!(actual.err.contains("does not exist")); + }) +}