Skip to content

Commit c3f9259

Browse files
committed
Expand ~ and $ORIGIN at the start of search paths.
1 parent b8f8e4f commit c3f9259

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

jaq-core/src/load/mod.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ impl<S: PartialEq> Term<S> {
213213
}
214214
}
215215

216+
#[cfg(feature = "std")]
217+
fn expand_prefix(path: &Path, pre: &str, f: impl FnOnce() -> Option<PathBuf>) -> Option<PathBuf> {
218+
let rest = path.strip_prefix(pre).ok()?;
219+
let mut replace = f()?;
220+
replace.push(rest);
221+
Some(replace)
222+
}
223+
216224
#[cfg(feature = "std")]
217225
impl<'a> Import<'a, &'a str> {
218226
fn meta_paths(&self) -> Vec<PathBuf> {
@@ -240,10 +248,24 @@ impl<'a> Import<'a, &'a str> {
240248
}
241249
rel.set_extension(ext);
242250

251+
#[cfg(target_os = "windows")]
252+
let home = "USERPROFILE";
253+
#[cfg(not(target_os = "windows"))]
254+
let home = "HOME";
255+
256+
use std::env;
257+
let home = || env::var_os(home).map(PathBuf::from);
258+
let origin = || env::current_exe().ok()?.parent().map(PathBuf::from);
259+
243260
self.meta_paths()
244261
.iter()
245262
.chain(paths)
246-
.map(|path| parent.join(path).join(&rel))
263+
.map(|path| {
264+
let home = expand_prefix(path, "~", home);
265+
let orig = expand_prefix(path, "$ORIGIN", origin);
266+
let path = home.as_ref().or(orig.as_ref()).unwrap_or(path);
267+
parent.join(path).join(&rel)
268+
})
247269
.filter_map(|path| path.canonicalize().ok())
248270
.find(|path| path.is_file())
249271
.ok_or_else(|| "file not found".into())

0 commit comments

Comments
 (0)