Resolve TeX inputs with the default suffix alternatives

This commit is contained in:
gloamxun committed 2026-11-19 20:01:08 +08:00
1 parent 901a50c36c
commit ee22f25ee0
5 files changed
+204 -10

No files matched your search

+76 -7
View File
@@ -341,6 +341,9 @@ pub struct InputSnapshot {
replay_gaps: Vec<String>,
#[serde(default)]
project: Option<Arc<ProjectSnapshot>>,
// Older captures only probed the supplied name when it had a suffix.
#[serde(default)]
tex_suffix_fallback: bool,
}
/// A failed enumeration is not a complete namespace. Retain the usable prefix
/// only as evidence and replay its actual error, never as a successful index.
@@ -498,6 +501,7 @@ impl InputSnapshot {
}
pub struct FileWorld {
config: WorldConfig,
tex_suffix_fallback: bool,
indexes: BTreeMap<PathBuf, Arc<DirectoryIndex>>,
current_project_indexes: BTreeSet<PathBuf>,
index_failures: BTreeMap<PathBuf, DirectoryFailure>,
@@ -633,6 +637,7 @@ impl FileWorld {
};
Ok(Self {
config,
tex_suffix_fallback: true,
indexes: BTreeMap::new(),
current_project_indexes: BTreeSet::new(),
index_failures: BTreeMap::new(),
@@ -673,6 +678,7 @@ impl FileWorld {
let live_paths = project.generated().map(Path::to_path_buf).collect();
Ok(Self {
config,
tex_suffix_fallback: true,
indexes: BTreeMap::new(),
current_project_indexes: BTreeSet::new(),
index_failures: BTreeMap::new(),
@@ -750,6 +756,7 @@ impl FileWorld {
};
Ok(Self {
config,
tex_suffix_fallback: snapshot.tex_suffix_fallback,
indexes: snapshot.indexes.clone(),
current_project_indexes: BTreeSet::new(),
index_failures: snapshot.index_failures.clone(),
@@ -783,6 +790,7 @@ impl FileWorld {
}
Arc::new(InputSnapshot {
schema: 1,
tex_suffix_fallback: self.tex_suffix_fallback,
config: self.config.clone(),
files: self.input_files.clone(),
file_modifications: self.file_modifications.clone(),
@@ -1230,15 +1238,35 @@ impl FileWorld {
}
paths
}
fn variants(kind: ResourceKind, name: &str) -> Vec<PathBuf> {
let p = Path::new(name);
fn variants(kind: ResourceKind, name: &str, tex_suffix_fallback: bool) -> Vec<PathBuf> {
let path = Path::new(name);
let mut values = Vec::new();
if p.extension().is_none() {
values.extend(kind.extensions().iter().map(|e| p.with_extension(e)));
if path.extension().is_none() {
values.extend(
kind.extensions()
.iter()
.map(|extension| path.with_extension(extension)),
);
} else if tex_suffix_fallback && !deferred::directory_only(path) {
// Kpathsea tries the standard suffix before an unrecognized one
// within a search path entry. The recognized TeX suffixes are
// tried as supplied.
const TEX_SUFFIXES: &[&str] =
&["tex", "sty", "cls", "fd", "aux", "bbl", "def", "clo", "ldf"];
let extension = path.extension().and_then(|extension| extension.to_str());
let recognized = extension.is_some_and(|extension| {
TEX_SUFFIXES.iter().any(|known| {
extension == *known || (cfg!(windows) && extension.eq_ignore_ascii_case(known))
})
});
if !recognized {
values.push(PathBuf::from(format!("{name}.tex")));
}
}
values.push(p.to_owned());
values.push(path.to_owned());
values
}
fn existing(path: &Path) -> Result<Option<PathBuf>, String> {
match path.metadata() {
Ok(m) if !m.is_dir() => path
@@ -1370,8 +1398,17 @@ impl FileWorld {
name: &str,
from: &Path,
) -> Result<Option<PathBuf>, String> {
self.resolve_variants(kind, name, from, Self::variants(kind, name))
self.resolve_variants(kind, name, from, Self::variants(kind, name, false))
}
/// Apply the TeX input scanner's default suffix alternatives. Raw host
/// files and recorded format dependencies still keep their names.
pub fn resolve_tex_input(&mut self, name: &str, from: &Path) -> Result<Option<PathBuf>, String> {
let kind = ResourceKind::Tex;
let fallback = self.config.input_policy == InputPolicy::Tex && self.tex_suffix_fallback;
self.resolve_variants(kind, name, from, Self::variants(kind, name, fallback))
}
fn resolve_variants(
&mut self,
kind: ResourceKind,
@@ -1500,7 +1537,39 @@ impl FileWorld {
name: &str,
from: &Path,
) -> Result<SnapshotFile, String> {
let path = self.resolve(kind, name, from)?.ok_or_else(|| {
let path = self.resolve(kind, name, from)?;
self.read_shared_resolved(kind, name, from, path)
}
/// Read a normal TeX source, including an unrecognized suffix's .tex
/// alternative when supported by the active input policy.
pub fn read_tex_input(&mut self, name: &str, from: &Path) -> Result<LoadedResource, String> {
let path = self.resolve_tex_input(name, from)?;
self.read_shared_resolved(ResourceKind::Tex, name, from, path)
.map(SnapshotFile::into_loaded)
}
/// TeX file queries use the same lookup as TeX input. A missing lookup
/// is returned as None without opening an arbitrary fallback file.
pub fn read_tex_input_optional(
&mut self,
name: &str,
from: &Path,
) -> Result<Option<LoadedResource>, String> {
let name = if name.is_empty() { ".tex" } else { name };
self.resolve_tex_input(name, from)?
.map(|path| self.read_path(path))
.transpose()
}
fn read_shared_resolved(
&mut self,
kind: ResourceKind,
name: &str,
from: &Path,
path: Option<PathBuf>,
) -> Result<SnapshotFile, String> {
let path = path.ok_or_else(|| {
format!(
"native {kind:?} resource {name} not found from {}",
from.display()