fix(single-instance): forward non-Unicode arguments and cwd (#3644)

* Update windows.rs

* Create single-instance-non-unicode-args.md

* fix on linux and windows too

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
justanotheranonymoususerandLucas Nogueira authored and Lucas Nogueira committed 2026-10-09 15:25:15 -03:00
1 parent 677258138b
commit 323fe80df1
5 files changed
+24 -11

No files matched your search

+3 -2
View File
@@ -39,8 +39,9 @@ pub(crate) type SingleInstanceCallback<R> =
/// [`Builder::build`]. Use [`Builder`] directly if you need to set a custom [`Builder::dbus_id`].
///
/// `f` is called with the app handle, the second instance's command line arguments
/// (as collected by [`std::env::args`], so the first element is the executable path) and its
/// current working directory. If the `deep-link` feature is enabled, the arguments are first
/// (as collected by [`std::env::args_os`], so the first element is the executable path) and its
/// current working directory. Arguments and the working directory that are not valid Unicode
/// have their invalid parts replaced by U+FFFD. If the `deep-link` feature is enabled, the arguments are first
/// forwarded to [`tauri-plugin-deep-link`](https://crates.io/crates/tauri-plugin-deep-link)
/// before `f` runs.
///
@@ -74,11 +74,13 @@ pub fn init<R: Runtime>(
Some("org.SingleInstance.DBus"),
"ExecuteCallback",
&(
std::env::args().collect::<Vec<String>>(),
std::env::args_os()
.map(|arg| arg.to_string_lossy().into_owned())
.collect::<Vec<String>>(),
std::env::current_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default(),
.to_string_lossy()
.as_ref(),
),
);
}
@@ -80,12 +80,14 @@ fn notify_singleton(socket: &PathBuf) -> Result<(), Error> {
let mut bf = BufWriter::new(&stream);
let cwd = std::env::current_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
.to_string_lossy()
.into_owned();
bf.write_all(cwd.as_bytes())?;
bf.write_all(b"\0\0")?;
let args_joined = std::env::args().collect::<Vec<String>>().join("\0");
let args_joined = std::env::args_os()
.map(|arg| arg.to_string_lossy().into_owned())
.collect::<Vec<String>>()
.join("\0");
bf.write_all(args_joined.as_bytes())?;
bf.flush()?;
drop(bf);
@@ -86,9 +86,12 @@ pub fn init<R: Runtime>(callback: Box<SingleInstanceCallback<R>>) -> TauriPlugin
}
let cwd = std::env::current_dir().unwrap_or_default();
let cwd = cwd.to_str().unwrap_or_default();
let cwd = cwd.to_string_lossy();
let args = std::env::args().collect::<Vec<String>>().join("|");
let args = std::env::args_os()
.map(|arg| arg.to_string_lossy().into_owned())
.collect::<Vec<String>>()
.join("|");
let data = format!("{cwd}|{args}\0",);