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()
+88
View File
@@ -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"
);
}
+31
View File
@@ -493,6 +493,37 @@ CPU 采样独立于浏览器延迟验收,明细保存在
`target/p4-warm-cpu-profile-main/original-a4/symbolized.json`,
SHA-256 为 `fa098bc394fb0247977e1393572759cada44513f86efc801d0c40e13979e5e12`。
### TeX 输入的默认扩展名候选
默认 TeX 编译在每个搜索目录中为带未知后缀的输入先试附加 `.tex`,并保留
已识别后缀及目录优先级。实际 `\input`、`\openin` 与 `\pdffilesize`
共用候选,通用宿主资源和格式镜像沿用各自的原解析。捕获显式保存所用规则,
旧输入快照继续按其原候选轨迹严格重放。74项定向测试通过,覆盖原生作业、
格式、项目、源快照和旧的活动文件别名回归。
[Kpathsea 默认候选顺序](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/kpathsea/tex-file.c#L999-L1038)
同一原样 `lipsum` 和未知后缀冲突样例分别由 pdfTeX、旧原生和新版独立编译。
新版实际读入 TeX Live 的 `lipsum.ltd.tex`,两个用例生成 PDF 且词序分别与
pdfTeX 一致。旧原生在 `lipsum.ltd` 缺失处停止。回执
`target/p3-tex-suffix-search/reference-jobs-v3/receipt.json` SHA-256 为
`5a2887155491071aea7d66e12d4938b0a8b99be44790a41b47487f4a6f708eed`。回归 `directed-v3.log` 的 SHA-256 为
`543f11b7e406963bac3ca3f7f03eeadfc988856314b58ad1db8edb7d628f07d2`。首轮全工作区在通用活动文件别名重放发现未捕获的扩展候选,
修订版保持原接口及该用例。首轮全工作区日志和第二版真实包加载诊断均保留。
同一原样 A4 和旧实际格式,两版各交替做30次主文件与30次子文件编辑。逐轮
执行与复用步数、PDF、SVG、来源版本及映射一致,最后 aux 相同。用新版和
最后两份真实编辑缓冲区独立全量,零复用步骤,产生相同输出。
| 编辑范围 | 基线可见 p50 / p95 (ms) | 新版可见 p50 / p95 (ms) |
| --- | ---: | ---: |
| 主文件正文 | 212.7 / 271.5 | 219.8 / 286.8 |
| 子文件正文 | 199.9 / 256.6 | 191.2 / 221.4 |
峰值 RSS 分别为 591964 和 591972 KiB。
浏览器回执 `target/p3-tex-suffix-search/edge-a4/run-1/receipt.json`
SHA-256 为 `3f65e945629d6da49af4ee59ef0fe7285e3dde5e98b46397b069b8756943d8b8`。独立全量回执 `full-equivalence/receipt.json`
SHA-256 为 `ca8cc3879c21de6369230b14c51ac3435fd9bb8b6fc0b73f795b7f49ae100858`。
### schema27 与主线兼容性
合并了真实重复盒及 PDF 书签布局,并保留主线的宏参数分隔符和流式失败报告。
+5
View File
@@ -329,6 +329,11 @@ SHA-256 为 `7184b2901abd49deb0439a7ea8a5e39ad302ee9663aaa07b43ace697bd74ec5b`
SHA-256 为 `6a29764fe8e66fe7899a2ce1c0ca3639e5cc018974d53e0b0a291c2afdcba7b9`。
这些结果仍需主干合并后复验。
主线另以原版 `lipsum` 的独立 article 用例复现并验证了 `lipsum.ltd` 的查找。
TeX 输入与 LaTeX3 使用的 `\pdffilesize` 实际读入发行版 `lipsum.ltd.tex`。
输出 PDF 的文字顺序与 pdfTeX 一致。隔离 schema29 的整份 arXiv 尚需与该改动
合并后复跑。[后缀查找阶段证据](P2_M1_PROGRESS.md)
### ACM 的三类官方样例
新增 ACM acmart v2.21 的单栏期刊 `acmsmall`、双栏期刊 `acmtog` 和会议
+4 -3
View File
@@ -1483,7 +1483,8 @@ impl SourceProvider for Files {
self.emit_diagnostic(WriteDestination::TerminalOnly, terminal, false)
}
fn read(&mut self, name: &str, from: &str) -> Result<SourceFile, String> {
let resource = self.resource(ResourceKind::Tex, name, from)?;
let resource = self.world.read_tex_input(name, Path::new(from))?;
self.observe_resource(ResourceKind::Tex, &resource);
let path = resource.version.path;
Ok(SourceFile {
name: path.display().to_string(),
@@ -1512,7 +1513,7 @@ impl SourceProvider for Files {
fn file_size(&mut self, name: &str, from: &str) -> Result<Option<u64>, String> {
let Some(resource) = self
.world
.read_optional(ResourceKind::Tex, name, Path::new(from))?
.read_tex_input_optional(name, Path::new(from))?
else {
return Ok(None);
};
@@ -1527,7 +1528,7 @@ impl SourceProvider for Files {
let result = (|| -> Result<Option<InputOpened>, String> {
let Some(path) = self
.world
.resolve(ResourceKind::Tex, name, Path::new(from))?
.resolve_tex_input(name, Path::new(from))?
else {
return Ok(None);
};