diff --git a/Cargo.toml b/Cargo.toml index beec5aa..2146ea5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,14 @@ readme = "README.md" categories = ["os", "filesystem"] keywords = ["which", "which-rs", "unix", "command"] +[features] +regex = ["dep:regex"] +tracing = ["dep:tracing"] + [dependencies] either = "1.9.0" regex = { version = "1.10.2", optional = true } +tracing = { version = "0.1.40", optional = true } [target.'cfg(any(windows, unix, target_os = "redox"))'.dependencies] home = "0.5.9" diff --git a/src/checker.rs b/src/checker.rs index 9668443..0044d22 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -14,7 +14,10 @@ impl Checker for ExecutableChecker { #[cfg(any(unix, target_os = "wasi", target_os = "redox"))] fn is_valid(&self, path: &Path) -> bool { use rustix::fs as rfs; - rfs::access(path, rfs::Access::EXEC_OK).is_ok() + let ret = rfs::access(path, rfs::Access::EXEC_OK).is_ok(); + #[cfg(feature = "tracing")] + tracing::trace!("{} EXEC_OK = {ret}", path.display()); + ret } #[cfg(windows)] @@ -34,26 +37,37 @@ impl ExistedChecker { impl Checker for ExistedChecker { #[cfg(target_os = "windows")] fn is_valid(&self, path: &Path) -> bool { - fs::symlink_metadata(path) + let ret = fs::symlink_metadata(path) .map(|metadata| { let file_type = metadata.file_type(); + #[cfg(feature = "tracing")] + tracing::trace!("{} is_file() = {}, is_symlink() = {}", new_path.display(), file_type.is_file(), file_type.is_symlink()); file_type.is_file() || file_type.is_symlink() }) .unwrap_or(false) - && (path.extension().is_some() || matches_arch(path)) + && (path.extension().is_some() || matches_arch(path)); + #[cfg(feature = "tracing")] + tracing::trace!("{} has_extension = {}, ExistedChecker::is_valid() = {ret}", path.display(), path.extension().is_some()); + ret } #[cfg(not(target_os = "windows"))] fn is_valid(&self, path: &Path) -> bool { - fs::metadata(path) + let ret = fs::metadata(path) .map(|metadata| metadata.is_file()) - .unwrap_or(false) + .unwrap_or(false); + #[cfg(feature = "tracing")] + tracing::trace!("{} is_file() = {ret}", path.display()); + ret } } #[cfg(target_os = "windows")] fn matches_arch(path: &Path) -> bool { - winsafe::GetBinaryType(&path.display().to_string()).is_ok() + let ret = winsafe::GetBinaryType(&path.display().to_string()).is_ok(); + #[cfg(feature = "tracing")] + tracing::trace!("{} matches_arch() = {ret}", path.display()); + ret } pub struct CompositeChecker { diff --git a/src/finder.rs b/src/finder.rs index cd73968..9a1efa8 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -78,10 +78,17 @@ impl Finder { let binary_path_candidates = match cwd { Some(cwd) if path.has_separator() => { + #[cfg(feature = "tracing")] + tracing::trace!( + "{} has a path seperator, so only CWD will be searched.", + path.display() + ); // Search binary in cwd if the path have a path separator. Either::Left(Self::cwd_search_candidates(path, cwd).into_iter()) } _ => { + #[cfg(feature = "tracing")] + tracing::trace!("{} has no path seperators, so only paths in PATH environment variable will be searched.", path.display()); // Search binary in PATHs(defined in environment variable). let paths = env::split_paths(&paths.ok_or(Error::CannotGetCurrentDirAndPathListEmpty)?) @@ -201,8 +208,18 @@ impl Finder { }); // Check if path already have executable extension if has_executable_extension(&p, path_extensions) { + #[cfg(feature = "tracing")] + tracing::trace!( + "{} already has an executable extension, not modifying it further", + p.display() + ); Box::new(iter::once(p)) } else { + #[cfg(feature = "tracing")] + tracing::trace!( + "{} has no extension, using PATHEXT environment variable to infer one", + p.display() + ); // Appended paths with windows executable extensions. // e.g. path `c:/windows/bin[.ext]` will expand to: // [c:/windows/bin.ext] @@ -215,7 +232,8 @@ impl Finder { // Append the extension. let mut p = p.clone().into_os_string(); p.push(e); - + #[cfg(feature = "tracing")] + tracing::trace!("possible extension: {}", p.display()); PathBuf::from(p) })), ) @@ -230,6 +248,11 @@ fn tilde_expansion(p: &PathBuf) -> Cow<'_, PathBuf> { if o == "~" { let mut new_path = home_dir().unwrap_or_default(); new_path.extend(component_iter); + #[cfg(feature = "tracing")] + tracing::trace!( + "found tilde, substituting in user's home directory to get {}", + new_path.display() + ); Cow::Owned(new_path) } else { Cow::Borrowed(p)