Skip to content

Commit d6a1416

Browse files
committed
add optional tracing feature, trace which logic
1 parent 7a71ac6 commit d6a1416

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ readme = "README.md"
1212
categories = ["os", "filesystem"]
1313
keywords = ["which", "which-rs", "unix", "command"]
1414

15+
[features]
16+
regex = ["dep:regex"]
17+
tracing = ["dep:tracing"]
18+
1519
[dependencies]
1620
either = "1.9.0"
1721
regex = { version = "1.10.2", optional = true }
22+
tracing = { version = "0.1.40", optional = true }
1823

1924
[target.'cfg(any(windows, unix, target_os = "redox"))'.dependencies]
2025
home = "0.5.9"

src/checker.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ impl Checker for ExecutableChecker {
1414
#[cfg(any(unix, target_os = "wasi", target_os = "redox"))]
1515
fn is_valid(&self, path: &Path) -> bool {
1616
use rustix::fs as rfs;
17-
rfs::access(path, rfs::Access::EXEC_OK).is_ok()
17+
let ret = rfs::access(path, rfs::Access::EXEC_OK).is_ok();
18+
#[cfg(feature = "tracing")]
19+
tracing::trace!("{} EXEC_OK = {ret}", path.display());
20+
ret
1821
}
1922

2023
#[cfg(windows)]
@@ -34,26 +37,37 @@ impl ExistedChecker {
3437
impl Checker for ExistedChecker {
3538
#[cfg(target_os = "windows")]
3639
fn is_valid(&self, path: &Path) -> bool {
37-
fs::symlink_metadata(path)
40+
let ret = fs::symlink_metadata(path)
3841
.map(|metadata| {
3942
let file_type = metadata.file_type();
43+
#[cfg(feature = "tracing")]
44+
tracing::trace!("{} is_file() = {}, is_symlink() = {}", new_path.display(), file_type.is_file(), file_type.is_symlink());
4045
file_type.is_file() || file_type.is_symlink()
4146
})
4247
.unwrap_or(false)
43-
&& (path.extension().is_some() || matches_arch(path))
48+
&& (path.extension().is_some() || matches_arch(path));
49+
#[cfg(feature = "tracing")]
50+
tracing::trace!("{} has_extension = {}, ExistedChecker::is_valid() = {ret}", path.display(), path.extension().is_some());
51+
ret
4452
}
4553

4654
#[cfg(not(target_os = "windows"))]
4755
fn is_valid(&self, path: &Path) -> bool {
48-
fs::metadata(path)
56+
let ret = fs::metadata(path)
4957
.map(|metadata| metadata.is_file())
50-
.unwrap_or(false)
58+
.unwrap_or(false);
59+
#[cfg(feature = "tracing")]
60+
tracing::trace!("{} is_file() = {ret}", path.display());
61+
ret
5162
}
5263
}
5364

5465
#[cfg(target_os = "windows")]
5566
fn matches_arch(path: &Path) -> bool {
56-
winsafe::GetBinaryType(&path.display().to_string()).is_ok()
67+
let ret = winsafe::GetBinaryType(&path.display().to_string()).is_ok();
68+
#[cfg(feature = "tracing")]
69+
tracing::trace!("{} matches_arch() = {ret}", path.display());
70+
ret
5771
}
5872

5973
pub struct CompositeChecker {

src/finder.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,17 @@ impl Finder {
7878

7979
let binary_path_candidates = match cwd {
8080
Some(cwd) if path.has_separator() => {
81+
#[cfg(feature = "tracing")]
82+
tracing::trace!(
83+
"{} has a path seperator, so only CWD will be searched.",
84+
path.display()
85+
);
8186
// Search binary in cwd if the path have a path separator.
8287
Either::Left(Self::cwd_search_candidates(path, cwd).into_iter())
8388
}
8489
_ => {
90+
#[cfg(feature = "tracing")]
91+
tracing::trace!("{} has no path seperators, so only paths in PATH environment variable will be searched.", path.display());
8592
// Search binary in PATHs(defined in environment variable).
8693
let paths =
8794
env::split_paths(&paths.ok_or(Error::CannotGetCurrentDirAndPathListEmpty)?)
@@ -201,8 +208,18 @@ impl Finder {
201208
});
202209
// Check if path already have executable extension
203210
if has_executable_extension(&p, path_extensions) {
211+
#[cfg(feature = "tracing")]
212+
tracing::trace!(
213+
"{} already has an executable extension, not modifying it further",
214+
p.display()
215+
);
204216
Box::new(iter::once(p))
205217
} else {
218+
#[cfg(feature = "tracing")]
219+
tracing::trace!(
220+
"{} has no extension, using PATHEXT environment variable to infer one",
221+
p.display()
222+
);
206223
// Appended paths with windows executable extensions.
207224
// e.g. path `c:/windows/bin[.ext]` will expand to:
208225
// [c:/windows/bin.ext]
@@ -215,7 +232,8 @@ impl Finder {
215232
// Append the extension.
216233
let mut p = p.clone().into_os_string();
217234
p.push(e);
218-
235+
#[cfg(feature = "tracing")]
236+
tracing::trace!("possible extension: {}", p.display());
219237
PathBuf::from(p)
220238
})),
221239
)
@@ -230,6 +248,11 @@ fn tilde_expansion(p: &PathBuf) -> Cow<'_, PathBuf> {
230248
if o == "~" {
231249
let mut new_path = home_dir().unwrap_or_default();
232250
new_path.extend(component_iter);
251+
#[cfg(feature = "tracing")]
252+
tracing::trace!(
253+
"found tilde, substituting in user's home directory to get {}",
254+
new_path.display()
255+
);
233256
Cow::Owned(new_path)
234257
} else {
235258
Cow::Borrowed(p)

0 commit comments

Comments
 (0)