Check overlapping generated path ownership once during preparation

This commit is contained in:
gloamxun committed 2026-12-02 03:33:35 +08:00
1 parent 143775b427
commit b7e1c9078d
1 file changed
+90 -7
+90 -7
View File
@@ -482,8 +482,15 @@ impl Files {
ArtifactLedger::new(cwd.clone()) ArtifactLedger::new(cwd.clone())
}; };
preparation.phase = HostPreparationPhase::Protection; preparation.phase = HostPreparationPhase::Protection;
let declared: BTreeSet<_> = world
.project()
.map(|project| project.generated().collect())
.unwrap_or_default();
// The explicit role validates this path immediately before
// observing its actual version. Prune only the other claims here.
let immutable_claims: Vec<_> = ledger let immutable_claims: Vec<_> = ledger
.paths() .paths()
.filter(|path| !declared.contains(path))
.filter(|path| { .filter(|path| {
preparation.aliases_any( preparation.aliases_any(
path, path,
@@ -498,13 +505,9 @@ impl Files {
for path in immutable_claims { for path in immutable_claims {
ledger.forget(&path); ledger.forget(&path);
} }
let declared: Vec<_> = world
.project()
.map(|p| p.generated().map(Path::to_path_buf).collect())
.unwrap_or_default();
for path in declared { for path in declared {
if preparation.aliases_any( if preparation.aliases_any(
&path, path,
protected protected
.iter() .iter()
.chain(std::iter::once(&log_path)) .chain(std::iter::once(&log_path))
@@ -512,11 +515,11 @@ impl Files {
) { ) {
return Err("atomic generated role aliases a protected input or output".into()); return Err("atomic generated role aliases a protected input or output".into());
} }
let observed = file_version(&path); let observed = file_version(path);
preparation preparation
.observations .observations
.push(crate::native_host::HostObservation::VersionCheck { .push(crate::native_host::HostObservation::VersionCheck {
path: path.clone(), path: path.to_owned(),
expected: crate::native_host::VersionExpectation::Any, expected: crate::native_host::VersionExpectation::Any,
observed: observed.clone(), observed: observed.clone(),
}); });
@@ -3039,6 +3042,86 @@ mod generated_file_tests {
fs::remove_dir_all(root).unwrap(); fs::remove_dir_all(root).unwrap();
} }
#[cfg(unix)]
#[test]
fn preparation_checks_overlapping_generated_roles_once_before_reading_the_version() {
use crate::native_host::HostObservation;
use texrra_tex_core::resources::{ProjectDefinition, ProjectSnapshot};
let root = root().canonicalize().unwrap();
let source = root.join("main.tex");
let generated = root.join("actual.aux");
let ledger = {
let mut seed = files(&root, "main.tex").unwrap();
write(&mut seed, "actual.aux", "prior version");
seed.finish().unwrap();
seed.ledger_path.clone()
};
let original_ledger = fs::read(&ledger).unwrap();
let original_log = fs::read(root.join("job.log")).unwrap();
fs::write(&generated, b"new version before preparation").unwrap();
let expected = file_version(&generated).unwrap();
let project = ProjectSnapshot::new(ProjectDefinition {
configuration: WorldConfig::new(&root),
revision: 1,
roots: BTreeSet::from([root.clone()]),
files: BTreeMap::from([(source.clone(), Arc::from(br"\end".as_slice()))]),
directories: BTreeSet::new(),
aliases: BTreeMap::new(),
generated_roots: BTreeSet::from([root.clone()]),
generated: BTreeSet::from([generated.clone()]),
})
.unwrap();
let prepare = |preparation: &mut HostPreparation| {
let mut world = FileWorld::from_project(project.clone()).unwrap();
world.read(ResourceKind::Tex, "main.tex", &root).unwrap();
Files::prepare_observed(
&source,
&root.join("job.pdf"),
world,
Ok(root.join("policy")),
false,
&GeneratedInputPolicy::Observe,
preparation,
None,
EffectMode::Draft,
)
};
let mut preparation = HostPreparation::default();
let mut host = prepare(&mut preparation).unwrap_or_else(|error| panic!("{}", error.error));
let aliases = preparation.observations.iter().filter(|observation| {
matches!(observation, HostObservation::Alias { left, right, same: false }
if left == &source && right == &generated)
});
assert_eq!(aliases.count(), 1);
assert_eq!(
preparation.generated_initial.as_ref().unwrap().files().get(&generated),
Some(&expected)
);
assert_eq!(
host.read("actual.aux", "").unwrap().data,
b"new version before preparation"
);
assert_eq!(host.generated_reads.get(&generated), expected.as_ref());
drop(host);
fs::remove_file(&generated).unwrap();
fs::hard_link(&source, &generated).unwrap();
let mut preparation = HostPreparation::default();
let failure = prepare(&mut preparation).err().expect("hardlink aliases a source");
assert!(failure.error.contains("atomic generated role aliases"));
assert_eq!(preparation.phase, HostPreparationPhase::Protection);
assert!(preparation.observations.iter().any(|observation| {
matches!(observation, HostObservation::Alias { left, right, same: true }
if left == &source && right == &generated)
}));
assert_eq!(fs::read(&source).unwrap(), br"\end");
assert_eq!(fs::read(&generated).unwrap(), br"\end");
assert_eq!(fs::read(root.join("job.log")).unwrap(), original_log);
assert_eq!(fs::read(&ledger).unwrap(), original_ledger);
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)] #[cfg(unix)]
#[test] #[test]
fn preparation_alias_rejection_records_actual_hardlink_identity() { fn preparation_alias_rejection_records_actual_hardlink_identity() {