Skip to content

Commit 6a36810

Browse files
committed
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)
1 parent d88387a commit 6a36810

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
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/single-instance/src/platform_impl/macos.rs

Lines changed: 16 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,23 @@ 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) = {
109+
let mut split = s.split("\0\0");
110+
(
111+
split.next().unwrap_or_default(),
112+
split.next().unwrap_or_default(),
113+
)
114+
};
107115
let args: Vec<String> =
108-
s.split('\0').map(String::from).collect();
109-
cb(app.app_handle(), args, cwd.clone());
116+
args.split('\0').map(String::from).collect();
117+
cb(app.app_handle(), args, cwd.to_string());
110118
}
111119
Err(e) => {
112120
tracing::debug!("single_instance failed to be notified: {e}")

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tab_spaces = 4

0 commit comments

Comments
 (0)