From de91b8a6fa5b31af9223f90520810373ea4fdfcf Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Tue, 8 Apr 2025 10:39:31 +0200 Subject: [PATCH] single-instance: fix `cwd` in single instance on macOS which was the `cwd` of the first instance, instead of the second how it should be and is on windows and linux. also add rustfmt.toml to enforce the correct formatting (4 spaces for indent) --- .changes/fix-macos-cwd-single-instance.md | 5 ++++ .../src/platform_impl/macos.rs | 24 ++++++++++++------- rustfmt.toml | 1 + 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 .changes/fix-macos-cwd-single-instance.md create mode 100644 rustfmt.toml diff --git a/.changes/fix-macos-cwd-single-instance.md b/.changes/fix-macos-cwd-single-instance.md new file mode 100644 index 00000000..2d528d1f --- /dev/null +++ b/.changes/fix-macos-cwd-single-instance.md @@ -0,0 +1,5 @@ +--- +single-instance: patch +--- + +fix `cwd` in single instance on macOS, which was the cwd of the first instance, instead of the second (like it is on windows and linux) diff --git a/plugins/single-instance/src/platform_impl/macos.rs b/plugins/single-instance/src/platform_impl/macos.rs index 70284eae..cefd7c3c 100644 --- a/plugins/single-instance/src/platform_impl/macos.rs +++ b/plugins/single-instance/src/platform_impl/macos.rs @@ -77,6 +77,13 @@ fn socket_cleanup(socket: &PathBuf) { fn notify_singleton(socket: &PathBuf) -> Result<(), Error> { let stream = UnixStream::connect(socket)?; let mut bf = BufWriter::new(&stream); + let cwd = std::env::current_dir() + .unwrap_or_default() + .to_str() + .unwrap_or_default() + .to_string(); + bf.write_all(cwd.as_bytes())?; + bf.write_all(b"\0\0")?; let args_joined = std::env::args().collect::>().join("\0"); bf.write_all(args_joined.as_bytes())?; bf.flush()?; @@ -91,12 +98,6 @@ fn listen_for_other_instances( ) { match UnixListener::bind(socket) { Ok(listener) => { - let cwd = std::env::current_dir() - .unwrap_or_default() - .to_str() - .unwrap_or_default() - .to_string(); - tauri::async_runtime::spawn(async move { for stream in listener.incoming() { match stream { @@ -104,9 +105,16 @@ fn listen_for_other_instances( let mut s = String::new(); match stream.read_to_string(&mut s) { Ok(_) => { + let (cwd, args) = { + let mut split = s.split("\0\0"); + ( + split.next().unwrap_or_default(), + split.next().unwrap_or_default(), + ) + }; let args: Vec = - s.split('\0').map(String::from).collect(); - cb(app.app_handle(), args, cwd.clone()); + args.split('\0').map(String::from).collect(); + cb(app.app_handle(), args, cwd.to_string()); } Err(e) => { tracing::debug!("single_instance failed to be notified: {e}") diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000..b6f799d6 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +tab_spaces = 4