loader: bind Analysis to the captured manifest and lock bytes
Read marqdo.yaml and marqdo.lock exactly once while loading, parse those bytes, and record sha256 digests on LoadedProject. This removes the post-preparation lock re-read (and its silent empty digest on IO failure) and lets Analysis name the exact static snapshot that selected the program. Add a regression test that editing the manifest after load cannot change the captured digest.
This commit is contained in:
1 parent
629fb9834d
commit
d8212aaa01
1 file changed
+83
-7
@@ -119,6 +119,14 @@ fn source_digest(source: &str) -> String {
|
|||||||
hex::encode(hasher.finalize())
|
hex::encode(hasher.finalize())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `sha256:<hex>` of exact bytes, the wire form used by Analysis snapshots.
|
||||||
|
fn bytes_digest(bytes: &[u8]) -> String {
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(bytes);
|
||||||
|
format!("sha256:{}", hex::encode(hasher.finalize()))
|
||||||
|
}
|
||||||
|
|
||||||
/// A resolved source location inside a loaded file.
|
/// A resolved source location inside a loaded file.
|
||||||
pub struct LocatedSource<'a> {
|
pub struct LocatedSource<'a> {
|
||||||
/// The owning file.
|
/// The owning file.
|
||||||
@@ -167,6 +175,13 @@ pub struct LoadedProject {
|
|||||||
pub manifest: Option<Manifest>,
|
pub manifest: Option<Manifest>,
|
||||||
/// Verified lock file, when present (lock v4).
|
/// Verified lock file, when present (lock v4).
|
||||||
pub lock: Option<LockFile>,
|
pub lock: Option<LockFile>,
|
||||||
|
/// SHA-256 of the exact `marqdo.lock` bytes captured while loading, or an
|
||||||
|
/// empty string when the project has no lock. Analysis must never re-read
|
||||||
|
/// the file: a later edit cannot change the snapshot the program used.
|
||||||
|
pub lock_digest: String,
|
||||||
|
/// SHA-256 of the exact `marqdo.yaml` bytes captured while loading, or an
|
||||||
|
/// empty string when there is no manifest.
|
||||||
|
pub manifest_digest: String,
|
||||||
/// Resolved `marqdo.yaml.extensions` activation sources (static paths only).
|
/// Resolved `marqdo.yaml.extensions` activation sources (static paths only).
|
||||||
pub extension_sources: Vec<marqdo_package::ResolvedExtension>,
|
pub extension_sources: Vec<marqdo_package::ResolvedExtension>,
|
||||||
}
|
}
|
||||||
@@ -251,19 +266,33 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
|||||||
.map(|parent| parent.to_path_buf())
|
.map(|parent| parent.to_path_buf())
|
||||||
.unwrap_or_else(|| PathBuf::from(".")),
|
.unwrap_or_else(|| PathBuf::from(".")),
|
||||||
};
|
};
|
||||||
let manifest = if root.join(MANIFEST_FILE).is_file() {
|
// Read each snapshot input exactly once. The digest recorded in Analysis
|
||||||
Some(Manifest::load(&root.join(MANIFEST_FILE))?)
|
// is computed from these bytes, not from a second read after preparation.
|
||||||
|
let manifest_path = root.join(MANIFEST_FILE);
|
||||||
|
let (manifest, manifest_digest) = if manifest_path.is_file() {
|
||||||
|
let bytes = std::fs::read(&manifest_path)
|
||||||
|
.map_err(|error| format!("cannot read {MANIFEST_FILE}: {error}"))?;
|
||||||
|
let yaml = String::from_utf8(bytes.clone())
|
||||||
|
.map_err(|error| format!("{MANIFEST_FILE} is not valid UTF-8: {error}"))?;
|
||||||
|
let manifest = Manifest::from_yaml(&yaml)
|
||||||
|
.map_err(|error| format!("{MANIFEST_FILE}: {error}"))?;
|
||||||
|
(Some(manifest), bytes_digest(&bytes))
|
||||||
} else {
|
} else {
|
||||||
None
|
(None, String::new())
|
||||||
};
|
};
|
||||||
let lock = if root.join(LOCK_FILE).is_file() {
|
let lock_path = root.join(LOCK_FILE);
|
||||||
let lock = LockFile::load(&root.join(LOCK_FILE))
|
let (lock, lock_digest) = if lock_path.is_file() {
|
||||||
|
let bytes = std::fs::read(&lock_path)
|
||||||
|
.map_err(|error| format!("cannot read {LOCK_FILE}: {error}"))?;
|
||||||
|
let yaml = String::from_utf8(bytes.clone())
|
||||||
|
.map_err(|error| format!("{LOCK_FILE} is not valid UTF-8: {error}"))?;
|
||||||
|
let lock = LockFile::from_yaml(&yaml)
|
||||||
.map_err(|error| format!("{LOCK_FILE}: {error}"))?;
|
.map_err(|error| format!("{LOCK_FILE}: {error}"))?;
|
||||||
if let Some(manifest) = &manifest {
|
if let Some(manifest) = &manifest {
|
||||||
lock.validate_root(manifest)
|
lock.validate_root(manifest)
|
||||||
.map_err(|error| format!("{LOCK_FILE}: {error}"))?;
|
.map_err(|error| format!("{LOCK_FILE}: {error}"))?;
|
||||||
}
|
}
|
||||||
Some(lock)
|
(Some(lock), bytes_digest(&bytes))
|
||||||
} else {
|
} else {
|
||||||
if manifest
|
if manifest
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -275,7 +304,7 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
|||||||
.to_string(),
|
.to_string(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
None
|
(None, String::new())
|
||||||
};
|
};
|
||||||
let resolver = PackageResolver::new(root.clone(), manifest.clone(), lock.clone());
|
let resolver = PackageResolver::new(root.clone(), manifest.clone(), lock.clone());
|
||||||
let entry_module = resolver.resolve_entry(&entry)?;
|
let entry_module = resolver.resolve_entry(&entry)?;
|
||||||
@@ -326,6 +355,8 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
|||||||
official_aliases,
|
official_aliases,
|
||||||
manifest,
|
manifest,
|
||||||
lock,
|
lock,
|
||||||
|
lock_digest,
|
||||||
|
manifest_digest,
|
||||||
extension_sources,
|
extension_sources,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -683,3 +714,48 @@ fn collect_identifiers_expr(expr: &Expr, names: &mut std::collections::BTreeSet<
|
|||||||
Expr::Int(_) | Expr::Float(_) | Expr::Text(_) | Expr::Bool(_) | Expr::None => {}
|
Expr::Int(_) | Expr::Float(_) | Expr::Text(_) | Expr::Bool(_) | Expr::None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
|
static COUNTER: AtomicU32 = AtomicU32::new(0);
|
||||||
|
|
||||||
|
fn temp_project(files: &[(&str, &str)]) -> PathBuf {
|
||||||
|
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||||
|
let root = std::env::temp_dir().join(format!(
|
||||||
|
"marqdo-loader-test-{}-{id}",
|
||||||
|
std::process::id()
|
||||||
|
));
|
||||||
|
let _ = std::fs::remove_dir_all(&root);
|
||||||
|
std::fs::create_dir_all(&root).unwrap();
|
||||||
|
for (name, source) in files {
|
||||||
|
std::fs::write(root.join(name), source).unwrap();
|
||||||
|
}
|
||||||
|
root
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn project_snapshot_digests_are_captured_once() {
|
||||||
|
let root = temp_project(&[
|
||||||
|
("marqdo.yaml", "project:\n name: snap\n version: 0.1.0\n"),
|
||||||
|
("index.mq.md", "## @exec main\n\n`return 1`\n"),
|
||||||
|
]);
|
||||||
|
let entry = root.join("index.mq.md");
|
||||||
|
let project = load(&entry).expect("load project");
|
||||||
|
let manifest_bytes = std::fs::read(root.join("marqdo.yaml")).unwrap();
|
||||||
|
assert_eq!(project.manifest_digest, bytes_digest(&manifest_bytes));
|
||||||
|
assert!(project.lock_digest.is_empty());
|
||||||
|
|
||||||
|
// The snapshot is what the program used; editing the manifest after
|
||||||
|
// loading cannot change the recorded digest.
|
||||||
|
std::fs::write(
|
||||||
|
root.join("marqdo.yaml"),
|
||||||
|
"project:\n name: snap\n version: 0.2.0\n",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(project.manifest_digest, bytes_digest(&manifest_bytes));
|
||||||
|
let _ = std::fs::remove_dir_all(&root);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user