Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fix-path-join-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "minor:bug"
"@tauri-apps/api": "minor:bug"
---

Fixed path joining behavior where `path.join('', 'a')` incorrectly returns "/a" instead of "a".
35 changes: 29 additions & 6 deletions crates/tauri/src/path/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ pub fn normalize(path: String) -> String {
}

#[command(root = "crate")]
pub fn join(mut paths: Vec<String>) -> String {
pub fn join(paths: Vec<String>) -> String {
let path = PathBuf::from(
paths
.iter_mut()
.map(|p| {
// Add a `MAIN_SEPARATOR` if it doesn't already have one.
.into_iter()
.map(|mut p| {
// Add a `MAIN_SEPARATOR` if it doesn't already have one and is not an empty string.
// Doing this to ensure that the vector elements are separated in
// the resulting string so path.components() can work correctly when called
// in `normalize_path_no_absolute()` later on.
if !p.ends_with('/') && !p.ends_with('\\') {
if !p.is_empty() && !p.ends_with('/') && !p.ends_with('\\') {
p.push(MAIN_SEPARATOR);
}
p.to_string()
p
})
.collect::<String>(),
);
Expand Down Expand Up @@ -287,4 +287,27 @@ mod tests {
"some-json-file.json.html"
);
}

#[test]
fn join() {
fn check(paths: Vec<&str>, expected_unix: &str, expected_windows: &str) {
let expected = if cfg!(windows) {
expected_windows
} else {
expected_unix
};
let paths = paths.into_iter().map(String::from).collect();
assert_eq!(super::join(paths), expected);
}

check(vec![""], ".", ".");
check(vec!["", ""], ".", ".");
check(vec!["a"], "a", "a");
check(vec!["", "a"], "a", "a");
check(vec!["a", "b"], "a/b", r"a\b");
check(vec!["a", "", "b"], "a/b", r"a\b");
check(vec!["a", "/b", "c"], "a/b/c", r"a\b\c");
check(vec!["a", "b/c", "d"], "a/b/c/d", r"a\b\c\d");
check(vec!["a/", "b"], "a/b", r"a\b");
}
}
Loading