From dc84f8d8bbaa70de3bb3185fbacb472993b996ef Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Fri, 18 Apr 2025 13:14:52 +0000 Subject: [PATCH] single-instance: fix `cwd` in single instance on macOS (#2609) * 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) * use split_once * remove rustfmt * fix indentation --------- Co-authored-by: Lucas Nogueira --- .changes/fix-macos-cwd-single-instance.md | 5 +++++ plugins/deep-link/src/lib.rs | 10 +++++----- .../single-instance/src/platform_impl/macos.rs | 18 ++++++++++-------- 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 .changes/fix-macos-cwd-single-instance.md 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/deep-link/src/lib.rs b/plugins/deep-link/src/lib.rs index 30584df5..d74864db 100644 --- a/plugins/deep-link/src/lib.rs +++ b/plugins/deep-link/src/lib.rs @@ -114,8 +114,8 @@ mod imp { /// ## Platform-specific: /// /// - **Windows / Linux**: This function reads the command line arguments and checks if there's only one value, which must be an URL with scheme matching one of the configured values. - /// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`]. - /// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect. + /// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`]. + /// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect. pub fn get_current(&self) -> crate::Result>> { self.plugin_handle .run_mobile_plugin::("getCurrent", ()) @@ -226,8 +226,8 @@ mod imp { /// ## Platform-specific: /// /// - **Windows / Linux**: This function reads the command line arguments and checks if there's only one value, which must be an URL with scheme matching one of the configured values. - /// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`]. - /// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect. + /// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`]. + /// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect. pub fn get_current(&self) -> crate::Result>> { return Ok(self.current.lock().unwrap().clone()); } @@ -350,7 +350,7 @@ mod imp { /// ## Platform-specific: /// /// - **Windows**: Requires admin rights if the protocol is registered on local machine - /// (this can happen when registered from the NSIS installer when the install mode is set to both or per machine) + /// (this can happen when registered from the NSIS installer when the install mode is set to both or per machine) /// - **Linux**: Can only unregister the scheme if it was initially registered with [`register`](`Self::register`). May not work on older distros. /// - **macOS / Android / iOS**: Unsupported, will return [`Error::UnsupportedPlatform`](`crate::Error::UnsupportedPlatform`). pub fn unregister>(&self, _protocol: S) -> crate::Result<()> { diff --git a/plugins/single-instance/src/platform_impl/macos.rs b/plugins/single-instance/src/platform_impl/macos.rs index 70284eae..63991513 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,10 @@ fn listen_for_other_instances( let mut s = String::new(); match stream.read_to_string(&mut s) { Ok(_) => { + let (cwd, args) = s.split_once("\0\0").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}")