Skip to content

Commit 23b9da7

Browse files
fix path joining behavior where path.join('', 'a') incorrectly returns "/a" instead of "a" (#13313) (#13324)
* fix path joining behavior where path.join('', 'a') incorrectly returns "/a" instead of "a" (#13313) * Clean up --------- Co-authored-by: Tony <[email protected]>
1 parent 8a8c1f9 commit 23b9da7

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

.changes/fix-path-join-error.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "minor:bug"
3+
"@tauri-apps/api": "minor:bug"
4+
---
5+
6+
Fixed path joining behavior where `path.join('', 'a')` incorrectly returns "/a" instead of "a".

crates/tauri/src/path/plugin.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,19 @@ pub fn normalize(path: String) -> String {
132132
}
133133

134134
#[command(root = "crate")]
135-
pub fn join(mut paths: Vec<String>) -> String {
135+
pub fn join(paths: Vec<String>) -> String {
136136
let path = PathBuf::from(
137137
paths
138-
.iter_mut()
139-
.map(|p| {
140-
// Add a `MAIN_SEPARATOR` if it doesn't already have one.
138+
.into_iter()
139+
.map(|mut p| {
140+
// Add a `MAIN_SEPARATOR` if it doesn't already have one and is not an empty string.
141141
// Doing this to ensure that the vector elements are separated in
142142
// the resulting string so path.components() can work correctly when called
143143
// in `normalize_path_no_absolute()` later on.
144-
if !p.ends_with('/') && !p.ends_with('\\') {
144+
if !p.is_empty() && !p.ends_with('/') && !p.ends_with('\\') {
145145
p.push(MAIN_SEPARATOR);
146146
}
147-
p.to_string()
147+
p
148148
})
149149
.collect::<String>(),
150150
);
@@ -287,4 +287,27 @@ mod tests {
287287
"some-json-file.json.html"
288288
);
289289
}
290+
291+
#[test]
292+
fn join() {
293+
fn check(paths: Vec<&str>, expected_unix: &str, expected_windows: &str) {
294+
let expected = if cfg!(windows) {
295+
expected_windows
296+
} else {
297+
expected_unix
298+
};
299+
let paths = paths.into_iter().map(String::from).collect();
300+
assert_eq!(super::join(paths), expected);
301+
}
302+
303+
check(vec![""], ".", ".");
304+
check(vec!["", ""], ".", ".");
305+
check(vec!["a"], "a", "a");
306+
check(vec!["", "a"], "a", "a");
307+
check(vec!["a", "b"], "a/b", r"a\b");
308+
check(vec!["a", "", "b"], "a/b", r"a\b");
309+
check(vec!["a", "/b", "c"], "a/b/c", r"a\b\c");
310+
check(vec!["a", "b/c", "d"], "a/b/c/d", r"a\b\c\d");
311+
check(vec!["a/", "b"], "a/b", r"a\b");
312+
}
290313
}

0 commit comments

Comments
 (0)