Skip to content

Commit dc84f8d

Browse files
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 <[email protected]>
1 parent ff384cb commit dc84f8d

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
single-instance: patch
3+
---
4+
5+
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)

plugins/deep-link/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ mod imp {
114114
/// ## Platform-specific:
115115
///
116116
/// - **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.
117-
/// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`].
118-
/// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect.
117+
/// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`].
118+
/// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect.
119119
pub fn get_current(&self) -> crate::Result<Option<Vec<url::Url>>> {
120120
self.plugin_handle
121121
.run_mobile_plugin::<GetCurrentResponse>("getCurrent", ())
@@ -226,8 +226,8 @@ mod imp {
226226
/// ## Platform-specific:
227227
///
228228
/// - **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.
229-
/// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`].
230-
/// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect.
229+
/// Note that you must manually check the arguments when registering deep link schemes dynamically with [`Self::register`].
230+
/// Additionally, the deep link might have been provided as a CLI argument so you should check if its format matches what you expect.
231231
pub fn get_current(&self) -> crate::Result<Option<Vec<url::Url>>> {
232232
return Ok(self.current.lock().unwrap().clone());
233233
}
@@ -350,7 +350,7 @@ mod imp {
350350
/// ## Platform-specific:
351351
///
352352
/// - **Windows**: Requires admin rights if the protocol is registered on local machine
353-
/// (this can happen when registered from the NSIS installer when the install mode is set to both or per machine)
353+
/// (this can happen when registered from the NSIS installer when the install mode is set to both or per machine)
354354
/// - **Linux**: Can only unregister the scheme if it was initially registered with [`register`](`Self::register`). May not work on older distros.
355355
/// - **macOS / Android / iOS**: Unsupported, will return [`Error::UnsupportedPlatform`](`crate::Error::UnsupportedPlatform`).
356356
pub fn unregister<S: AsRef<str>>(&self, _protocol: S) -> crate::Result<()> {

plugins/single-instance/src/platform_impl/macos.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ fn socket_cleanup(socket: &PathBuf) {
7777
fn notify_singleton(socket: &PathBuf) -> Result<(), Error> {
7878
let stream = UnixStream::connect(socket)?;
7979
let mut bf = BufWriter::new(&stream);
80+
let cwd = std::env::current_dir()
81+
.unwrap_or_default()
82+
.to_str()
83+
.unwrap_or_default()
84+
.to_string();
85+
bf.write_all(cwd.as_bytes())?;
86+
bf.write_all(b"\0\0")?;
8087
let args_joined = std::env::args().collect::<Vec<String>>().join("\0");
8188
bf.write_all(args_joined.as_bytes())?;
8289
bf.flush()?;
@@ -91,22 +98,17 @@ fn listen_for_other_instances<A: Runtime>(
9198
) {
9299
match UnixListener::bind(socket) {
93100
Ok(listener) => {
94-
let cwd = std::env::current_dir()
95-
.unwrap_or_default()
96-
.to_str()
97-
.unwrap_or_default()
98-
.to_string();
99-
100101
tauri::async_runtime::spawn(async move {
101102
for stream in listener.incoming() {
102103
match stream {
103104
Ok(mut stream) => {
104105
let mut s = String::new();
105106
match stream.read_to_string(&mut s) {
106107
Ok(_) => {
108+
let (cwd, args) = s.split_once("\0\0").unwrap_or_default();
107109
let args: Vec<String> =
108-
s.split('\0').map(String::from).collect();
109-
cb(app.app_handle(), args, cwd.clone());
110+
args.split('\0').map(String::from).collect();
111+
cb(app.app_handle(), args, cwd.to_string());
110112
}
111113
Err(e) => {
112114
tracing::debug!("single_instance failed to be notified: {e}")

0 commit comments

Comments
 (0)