diff --git a/crates/nu-command/src/filesystem/ls.rs b/crates/nu-command/src/filesystem/ls.rs index 02263cc7a3..022970778f 100644 --- a/crates/nu-command/src/filesystem/ls.rs +++ b/crates/nu-command/src/filesystem/ls.rs @@ -343,11 +343,7 @@ fn is_hidden_dir(dir: impl AsRef) -> bool { } fn path_contains_hidden_folder(path: &Path, folders: &[PathBuf]) -> bool { - let path_str = path.to_str().expect("failed to read path"); - if folders - .iter() - .any(|p| path_str.starts_with(&p.to_str().expect("failed to read hidden paths"))) - { + if folders.iter().any(|p| path.starts_with(p.as_path())) { return true; } false diff --git a/crates/nu-command/tests/commands/ls.rs b/crates/nu-command/tests/commands/ls.rs index f5708df52d..f699d85341 100644 --- a/crates/nu-command/tests/commands/ls.rs +++ b/crates/nu-command/tests/commands/ls.rs @@ -513,3 +513,28 @@ fn list_a_directory_not_exists() { assert!(actual.err.contains("directory not found")); }) } + +#[cfg(target_os = "linux")] +#[test] +fn list_directory_contains_invalid_utf8() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + + Playground::setup( + "ls_test_directory_contains_invalid_utf8", + |dirs, _sandbox| { + let v: [u8; 4] = [7, 196, 144, 188]; + let s = OsStr::from_bytes(&v); + + let cwd = dirs.test(); + let path = cwd.join(s); + + std::fs::create_dir_all(&path).expect("failed to create directory"); + + let actual = nu!(cwd, "ls"); + + assert!(actual.out.contains("warning: get non-utf8 filename")); + assert!(actual.err.contains("No matches found for")); + }, + ) +}