1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Path searching utilities.
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use crate::sys;
use crate::sys::fs::PathExt;
/// Encapsulates the result of a path search.
pub struct ExecutablePathSearch<PI, N> {
paths: VecDeque<PI>,
filename: N,
}
impl<PI, N> Iterator for ExecutablePathSearch<PI, N>
where
PI: AsRef<Path>,
N: AsRef<Path>,
{
type Item = PathBuf;
fn next(&mut self) -> Option<Self::Item> {
while let Some(path) = self.paths.pop_front() {
let path = PathBuf::from(path.as_ref()).join(self.filename.as_ref());
// Ask the platform to resolve the path to an actual executable
// file, which on Windows may involve appending a
// PATHEXT extension. The helper takes ownership so Unix
// — where no resolution is needed — can return the path unchanged
// without allocating.
//
// A directory carries the execute bit on Unix but is never a
// command. Filter the *resolved* path rather than the
// input: on Windows, resolution appends a
// PATHEXT extension, so a `prog` directory must not stop `prog.exe`
// in the same PATH entry from being found.
if let Some(resolved) = sys::fs::resolve_executable(path)
&& !resolved.is_dir()
{
return Some(resolved);
}
}
None
}
}
pub(crate) struct ExecutablePathPrefixSearch<PI> {
paths: VecDeque<PI>,
queued_items: VecDeque<PathBuf>,
filename_prefix: String,
case_insensitive: bool,
}
impl<PI> Iterator for ExecutablePathPrefixSearch<PI>
where
PI: AsRef<Path>,
{
type Item = PathBuf;
fn next(&mut self) -> Option<Self::Item> {
// If we already found some items and queued them, then yield one now.
if let Some(item) = self.queued_items.pop_front() {
return Some(item);
}
while let Some(path) = self.paths.pop_front() {
let path = PathBuf::from(path.as_ref());
if let Ok(readdir) = path.read_dir() {
for entry in readdir.flatten() {
if let Ok(mut filename) = entry.file_name().into_string() {
if self.case_insensitive {
filename = filename.to_ascii_lowercase();
}
if !filename.starts_with(&self.filename_prefix) {
continue;
}
}
let entry_path = entry.path();
if let Ok(file_type) = entry.file_type() {
if file_type.is_file() && entry_path.executable() {
self.queued_items.push_back(entry_path);
continue;
}
if file_type.is_symlink() && entry_path.executable() {
self.queued_items.push_back(entry_path);
}
}
}
}
if let Some(item) = self.queued_items.pop_front() {
return Some(item);
}
}
None
}
}
/// Search for the given executable name in the provided paths.
///
/// # Arguments
///
/// * `paths` - An iterator over the paths to search.
/// * `filename` - The name of the executable file to search for.
pub fn search_for_executable<P, PI, N>(paths: P, filename: N) -> ExecutablePathSearch<PI, N>
where
P: Iterator<Item = PI>,
PI: AsRef<Path>,
N: AsRef<Path>,
{
ExecutablePathSearch {
paths: paths.collect(),
filename,
}
}
/// Resolves a command name the way the shell does when it is about to run it.
///
/// Returns the first executable in search order, or -- if there is none -- the
/// first entry that exists and is not a directory, which the shell reports as
/// the command and then fails to run.
///
/// # Arguments
///
/// * `paths` - An iterator over the paths to search.
/// * `filename` - The name of the command to resolve.
pub fn resolve_command<P, PI, N>(paths: P, filename: N) -> Option<PathBuf>
where
P: IntoIterator<Item = PI>,
PI: AsRef<Path>,
N: AsRef<Path>,
{
let mut first_non_executable = None;
for dir in paths {
let path = dir.as_ref().join(filename.as_ref());
// Remember the first non-directory entry seen, in case no executable
// turns up.
if first_non_executable.is_none() && path.metadata().is_ok_and(|m| !m.is_dir()) {
first_non_executable = Some(path.clone());
}
// Resolve, then reject directories; see `ExecutablePathSearch::next`.
if let Some(resolved) = sys::fs::resolve_executable(path)
&& !resolved.is_dir()
{
return Some(resolved);
}
}
first_non_executable
}
pub(crate) fn search_for_executable_with_prefix<P, PI>(
paths: P,
filename_prefix: &str,
case_insensitive: bool,
) -> ExecutablePathPrefixSearch<PI>
where
P: Iterator<Item = PI>,
PI: AsRef<Path>,
{
let stored_prefix = if case_insensitive {
filename_prefix.to_ascii_lowercase()
} else {
filename_prefix.into()
};
ExecutablePathPrefixSearch {
paths: paths.collect(),
queued_items: VecDeque::new(),
filename_prefix: stored_prefix,
case_insensitive,
}
}
#[cfg(test)]
#[expect(clippy::panic_in_result_fn)]
mod tests {
use anyhow::Result;
use super::*;
/// A directory carries the execute bit on Unix, so it must not be mistaken
/// for a command; an executable later in the search order takes its
/// place.
#[test]
fn directory_is_not_a_command() -> Result<()> {
let scratch = tempfile::tempdir()?;
let first = scratch.path().join("first");
let second = scratch.path().join("second");
std::fs::create_dir_all(first.join("prog"))?;
std::fs::create_dir_all(&second)?;
std::fs::write(second.join("prog"), "")?;
let paths = [first.as_path(), second.as_path()];
// The plain (non-executable) file wins over the directory, rather than
// the directory being reported as the command.
assert_eq!(resolve_command(paths, "prog"), Some(second.join("prog")));
// ...and a directory is never yielded as an executable at all.
assert_eq!(search_for_executable(paths.iter(), "prog").next(), None);
Ok(())
}
/// On Windows, `PATHEXT` resolution has to run before directories are
/// rejected: a `prog` directory must not keep `prog.bat` in the same
/// PATH entry from being found.
#[cfg(windows)] // ast-grep-ignore: platform-cfg-outside-sys
#[test]
fn same_named_directory_does_not_hide_a_pathext_match() -> Result<()> {
let scratch = tempfile::tempdir()?;
std::fs::create_dir_all(scratch.path().join("prog"))?;
std::fs::write(scratch.path().join("prog.bat"), "@echo off\r\n")?;
let paths = [scratch.path()];
for found in [
search_for_executable(paths.iter(), "prog").next(),
resolve_command(paths, "prog"),
] {
assert!(
found.as_ref().is_some_and(|path| path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("bat"))),
"unexpected resolution: {found:?}"
);
}
Ok(())
}
}