Resolve TeX inputs with the default suffix alternatives
This commit is contained in:
1 parent
901a50c36c
commit
ee22f25ee0
5 files changed
+204
-10
No files matched your search
@@ -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()
|
||||
|
||||
@@ -520,3 +520,91 @@ fn frozen_real_reference_resources_are_read_as_their_actual_bytes_and_versions()
|
||||
);
|
||||
fs::remove_dir_all(p).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tex_unknown_suffix_lookup_keeps_path_priority_and_format_names() {
|
||||
let root = directory().canonicalize().unwrap();
|
||||
let first = root.join("first");
|
||||
let second = root.join("second");
|
||||
put(first.join("chapter.fragment"), b"first");
|
||||
put(second.join("chapter.fragment.tex"), b"later");
|
||||
put(first.join("style.clo.tex"), b"not a style fallback");
|
||||
let mut config = WorldConfig::new(&root);
|
||||
config.paths.insert(
|
||||
ResourceKind::Tex,
|
||||
vec![
|
||||
SearchPath::directory(&first, false),
|
||||
SearchPath::directory(&second, false),
|
||||
],
|
||||
);
|
||||
let entry = root.join("main.tex");
|
||||
let mut world = FileWorld::new(config.clone()).unwrap();
|
||||
assert_eq!(
|
||||
world
|
||||
.read_tex_input("chapter.fragment", &entry)
|
||||
.unwrap()
|
||||
.data,
|
||||
b"first"
|
||||
);
|
||||
|
||||
let selected = first.join("chapter.fragment.tex");
|
||||
put(&selected, b"standard suffix");
|
||||
let resource = world.read_tex_input("chapter.fragment", &entry).unwrap();
|
||||
assert_eq!(resource.version.path, selected);
|
||||
assert_eq!(resource.data, b"standard suffix");
|
||||
assert_eq!(
|
||||
world
|
||||
.read_tex_input_optional("chapter.fragment", &entry)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.version
|
||||
.path,
|
||||
selected
|
||||
);
|
||||
assert_eq!(
|
||||
world
|
||||
.read_shared(ResourceKind::Tex, "chapter.fragment", &entry)
|
||||
.unwrap()
|
||||
.data
|
||||
.as_ref(),
|
||||
b"first"
|
||||
);
|
||||
assert_eq!(world.resolve_tex_input("style.clo", &entry).unwrap(), None);
|
||||
let mut capture = FileWorld::new(config.clone()).unwrap();
|
||||
capture.capture_inputs();
|
||||
capture.read_tex_input("chapter.fragment", &entry).unwrap();
|
||||
let captured = serde_json::to_vec(capture.input_snapshot().as_ref()).unwrap();
|
||||
|
||||
config.input_policy = InputPolicy::Project;
|
||||
let mut old_capture = FileWorld::new(config).unwrap();
|
||||
old_capture.capture_inputs();
|
||||
old_capture
|
||||
.read_tex_input("chapter.fragment", &entry)
|
||||
.unwrap();
|
||||
// The original serialization omitted the flag and recorded only the
|
||||
// supplied spelling for inputs such as chapter.fragment.
|
||||
let mut previous = serde_json::to_value(old_capture.input_snapshot().as_ref()).unwrap();
|
||||
previous
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("tex_suffix_fallback");
|
||||
previous["config"]["input_policy"] = serde_json::json!("Tex");
|
||||
|
||||
fs::remove_dir_all(&root).unwrap();
|
||||
let mut recent = FileWorld::from_snapshot(serde_json::from_slice(&captured).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
recent
|
||||
.read_tex_input("chapter.fragment", &entry)
|
||||
.unwrap()
|
||||
.data,
|
||||
b"standard suffix"
|
||||
);
|
||||
let mut older = FileWorld::from_snapshot(serde_json::from_value(previous).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
older
|
||||
.read_tex_input("chapter.fragment", &entry)
|
||||
.unwrap()
|
||||
.data,
|
||||
b"first"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user