Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Package: report its paths with `/` on Windows too (#7184)

skylar taylor-barrickead6d2197f5d
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs
index 0f4ec1591..7a86eb0ca 100644
--- a/crates/engine/src/file_cmds.rs
+++ b/crates/engine/src/file_cmds.rs
@@ -242,6 +242,17 @@ pub fn same_file(a: &str, b: &str) -> bool {
false
}
+/// A path as command results report it: `/` between components, whatever the platform.
+///
+/// `join` already builds with `/`, but a directory from the caller or from `std::path::absolute`
+/// carries the native separator, which would leave a reported path mixed. The control channel
+/// takes `/` and rejects alternate separators (`AuthorizedWorkspace` in `crates/automation`), so
+/// a result spelt this way can be handed straight back to it. Verbatim and UNC prefixes keep
+/// theirs: Win32 does not accept `/` after a leading `\\`.
+pub(crate) fn forward_slashes(path: String) -> String {
+ if path.starts_with(r"\\") { path } else { path.replace('\\', "/") }
+}
+
pub(crate) fn join(dir: &str, name: &str) -> String {
if dir.is_empty() {
name.to_string()
diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs
index ed0f2cefc..3cc99aa27 100644
--- a/crates/engine/src/print_cmds.rs
+++ b/crates/engine/src/print_cmds.rs
@@ -805,7 +805,10 @@ fn package(s: &mut Session, p: &Value) -> Result<Value> {
let dir = p.get("dir").and_then(Value::as_str).filter(|v| !v.is_empty()).ok_or_else(|| bad(cmd, "missing \"dir\" (where to create the package folder)"))?;
// The copies are relinked by absolute path: a relative one would be resolved against the
// package document's folder, not the working directory `dir` is relative to.
+ // `folder`, `document` and `links` are built on it with `join`, so it is spelt the same way:
+ // `/` throughout, not a native prefix with `/` after it.
let dir = std::path::absolute(dir).map_or_else(|_| dir.to_string(), |d| d.to_string_lossy().into_owned());
+ let dir = crate::file_cmds::forward_slashes(dir);
let d = s.active().ok_or(EngineError::NoDocument)?;
let doc = (*d.doc).clone();
let document = d.path.clone();
diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs
index 34c1cda6f..ddd1a65a1 100644
--- a/crates/engine/src/print_cmds/tests.rs
+++ b/crates/engine/src/print_cmds/tests.rs
@@ -5,7 +5,9 @@ fn tmp(name: &str) -> String {
let d = std::env::temp_dir().join(format!("photocraft-print-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
- d.to_string_lossy().into_owned()
+ // Spelt the way command results spell paths, so an expectation built with `/` matches on
+ // Windows too. Win32 takes `/` as a separator, so the directory is still usable as one.
+ d.to_string_lossy().replace('\\', "/")
}
fn session(mode: &str, depth: u32) -> Session {
@@ -254,7 +256,12 @@ fn package_copies_links_and_relinks() {
let copied = format!("{dir}/pkg/Print Me/Links/art.png");
assert_eq!(r["links"], json!([copied]));
assert!(std::path::Path::new(&copied).is_file());
+ // Reported paths are `/`-spelt on every platform, not the native separator up to `dir` and
+ // `/` after it: a client hands them back to the control channel, which takes only `/`.
let docp = r["document"].as_str().unwrap();
+ for p in [r["folder"].as_str().unwrap(), docp, r["links"][0].as_str().unwrap()] {
+ assert!(!p.contains('\\'), "{p}");
+ }
assert!(docp.ends_with("Print Me/Print Me.pcraft"));
let back = photocraft_io::import("Print Me.pcraft", &std::fs::read(docp).unwrap()).unwrap().document;
let linked: Vec<String> = back