Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Package: a missing link stays missing, with no empty file in Links/ (fixes #908)

Marco Arnulfob12e94c11d0e
diff --git a/crates/engine/src/cmd/package.rs b/crates/engine/src/cmd/package.rs
index 25f09537c..8daa68e4b 100644
--- a/crates/engine/src/cmd/package.rs
+++ b/crates/engine/src/cmd/package.rs
@@ -292,6 +292,11 @@ fn write_links(d: &Document, root: &OutputRoot, dir: &Path, only: Option<&[Asset
continue;
}
let Some(a) = d.assets.get(&id) else { continue };
+ // A missing link has no bytes: an empty file under its name would read as present, so it
+ // keeps its old (missing) path instead.
+ if a.data.is_empty() {
+ continue;
+ }
let name = link_name(a.link.as_deref().unwrap_or(&a.name), id, &mut taken);
let path = dir.join(&name);
root.write(&path, a.data.as_slice())?;
@@ -472,6 +477,49 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
+ /// A link whose file is gone has no bytes to copy: the package leaves it out of Links/, the
+ /// report lists it as missing, and the packaged document keeps it missing (#908).
+ #[test]
+ fn package_leaves_missing_links_missing() {
+ let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
+ let dir = std::env::temp_dir().join(format!("dc-package-missing-{}-{nonce}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let svg = r##"<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"><rect width="40" height="40"/></svg>"##;
+ let (gone, kept) = (dir.join("gone.svg"), dir.join("kept.svg"));
+ std::fs::write(&gone, svg).unwrap();
+ std::fs::write(&kept, svg.replace("<rect", "<circle r=\"9\"/><rect")).unwrap();
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"title": "TwoLinks"})).unwrap();
+ s.execute("file.place", &json!({"path": gone.to_string_lossy(), "x": 10, "y": 10})).unwrap();
+ s.execute("edit.deselectAll", &json!({})).unwrap();
+ s.execute("file.place", &json!({"path": kept.to_string_lossy(), "x": 10, "y": 200})).unwrap();
+ let idml = dir.join("TwoLinks.idml");
+ s.execute("file.exportIdml", &json!({"path": idml.to_string_lossy(), "embedImages": false})).unwrap();
+ std::fs::remove_file(&gone).unwrap();
+ let mut s = Session::new();
+ s.execute("file.openIdml", &json!({"path": idml.to_string_lossy()})).unwrap();
+ let status = |s: &mut Session, name: &str| {
+ let links = s.execute("links.list", &json!({})).unwrap();
+ links.as_array().unwrap().iter().find(|l| l["name"] == name).map(|l| l["status"].clone())
+ };
+ assert_eq!(status(&mut s, "gone.svg"), Some(json!("missing")));
+
+ let out = dir.join("Pkg");
+ let r = s.execute("file.package", &json!({"dir": out.to_string_lossy(), "idml": true})).unwrap();
+ assert!(!out.join("Links").join("gone.svg").exists(), "a missing link is not copied");
+ assert!(out.join("Links").join("kept.svg").exists());
+ assert!(!r["files"].to_string().contains("gone.svg"), "{}", r["files"]);
+ let report = r["report"].as_str().unwrap();
+ assert!(report.contains("gone.svg (missing)"), "{report}");
+ for (command, extension) in [("file.open", "designcraft"), ("file.openIdml", "idml")] {
+ let mut opened = Session::new();
+ opened.execute(command, &json!({"path": out.join(format!("TwoLinks.{extension}")).to_string_lossy()})).unwrap();
+ assert_eq!(status(&mut opened, "gone.svg"), Some(json!("missing")), "{extension}");
+ assert_eq!(status(&mut opened, "kept.svg"), Some(json!("ok")), "{extension}");
+ }
+ std::fs::remove_dir_all(dir).unwrap();
+ }
+
#[test]
fn package_writes_document_links_and_report() {
let dir = std::env::temp_dir().join(format!("dc-package-{}", std::process::id()));