enhance(fs): improve error message when resolving path in debug builds (#1939)

* enhance(fs): improve error message when resolving path in debug builds

closes #11338

* fix compilation

* deduplicate read_file code

* fix compile

* fix compile again

* fmt

* comma

* add change file

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Amr BashirandLucas Nogueira authored and GitHub committed 2025-10-09 08:58:08 -03:00
1 parent 67a7bf80f8
commit 6b5b1053ba
4 files changed
+118 -19

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
---
"fs": patch
"fs-js": patch
---
Enhance error messages.
+110 -18
View File
@@ -85,6 +85,7 @@ pub fn create<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<ResourceId> { ) -> CommandResult<ResourceId> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"create",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -119,6 +120,7 @@ pub fn open<R: Runtime>(
options: Option<OpenOptions>, options: Option<OpenOptions>,
) -> CommandResult<ResourceId> { ) -> CommandResult<ResourceId> {
let (file, _path) = resolve_file( let (file, _path) = resolve_file(
"open",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -167,6 +169,7 @@ pub async fn copy_file<R: Runtime>(
options: Option<CopyFileOptions>, options: Option<CopyFileOptions>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let resolved_from_path = resolve_path( let resolved_from_path = resolve_path(
"copy-file",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -174,6 +177,7 @@ pub async fn copy_file<R: Runtime>(
options.as_ref().and_then(|o| o.from_path_base_dir), options.as_ref().and_then(|o| o.from_path_base_dir),
)?; )?;
let resolved_to_path = resolve_path( let resolved_to_path = resolve_path(
"copy-file",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -208,6 +212,7 @@ pub fn mkdir<R: Runtime>(
options: Option<MkdirOptions>, options: Option<MkdirOptions>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"mkdir",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -255,6 +260,7 @@ pub async fn read_dir<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<Vec<DirEntry>> { ) -> CommandResult<Vec<DirEntry>> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"read-dir",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -331,8 +337,8 @@ pub async fn read<R: Runtime>(
Ok(tauri::ipc::Response::new(data)) Ok(tauri::ipc::Response::new(data))
} }
#[tauri::command] async fn read_file_inner<R: Runtime>(
pub async fn read_file<R: Runtime>( permission: &str,
webview: Webview<R>, webview: Webview<R>,
global_scope: GlobalScope<Entry>, global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>, command_scope: CommandScope<Entry>,
@@ -340,6 +346,7 @@ pub async fn read_file<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<tauri::ipc::Response> { ) -> CommandResult<tauri::ipc::Response> {
let (mut file, path) = resolve_file( let (mut file, path) = resolve_file(
permission,
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -367,6 +374,25 @@ pub async fn read_file<R: Runtime>(
Ok(tauri::ipc::Response::new(contents)) Ok(tauri::ipc::Response::new(contents))
} }
#[tauri::command]
pub async fn read_file<R: Runtime>(
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
path: SafeFilePath,
options: Option<BaseOptions>,
) -> CommandResult<tauri::ipc::Response> {
read_file_inner(
"read-file",
webview,
global_scope,
command_scope,
path,
options,
)
.await
}
// TODO, remove in v3, rely on `read_file` command instead // TODO, remove in v3, rely on `read_file` command instead
#[tauri::command] #[tauri::command]
pub async fn read_text_file<R: Runtime>( pub async fn read_text_file<R: Runtime>(
@@ -376,7 +402,15 @@ pub async fn read_text_file<R: Runtime>(
path: SafeFilePath, path: SafeFilePath,
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<tauri::ipc::Response> { ) -> CommandResult<tauri::ipc::Response> {
read_file(webview, global_scope, command_scope, path, options).await read_file_inner(
"read-text-file",
webview,
global_scope,
command_scope,
path,
options,
)
.await
} }
#[tauri::command] #[tauri::command]
@@ -388,6 +422,7 @@ pub fn read_text_file_lines<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<ResourceId> { ) -> CommandResult<ResourceId> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"read-text-file-lines",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -452,6 +487,7 @@ pub fn remove<R: Runtime>(
options: Option<RemoveOptions>, options: Option<RemoveOptions>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"remove",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -521,6 +557,7 @@ pub fn rename<R: Runtime>(
options: Option<RenameOptions>, options: Option<RenameOptions>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let resolved_old_path = resolve_path( let resolved_old_path = resolve_path(
"rename",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -528,6 +565,7 @@ pub fn rename<R: Runtime>(
options.as_ref().and_then(|o| o.old_path_base_dir), options.as_ref().and_then(|o| o.old_path_base_dir),
)?; )?;
let resolved_new_path = resolve_path( let resolved_new_path = resolve_path(
"rename",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -575,6 +613,7 @@ pub async fn seek<R: Runtime>(
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Metadata>>( fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Metadata>>(
permission: &str,
metadata_fn: F, metadata_fn: F,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
@@ -585,6 +624,7 @@ fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Meta
match path { match path {
SafeFilePath::Url(url) => { SafeFilePath::Url(url) => {
let (file, path) = resolve_file( let (file, path) = resolve_file(
permission,
webview, webview,
global_scope, global_scope,
command_scope, command_scope,
@@ -606,6 +646,7 @@ fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Meta
}) })
} }
SafeFilePath::Path(p) => get_fs_metadata( SafeFilePath::Path(p) => get_fs_metadata(
permission,
metadata_fn, metadata_fn,
webview, webview,
global_scope, global_scope,
@@ -618,6 +659,7 @@ fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Meta
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Metadata>>( fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Metadata>>(
permission: &str,
metadata_fn: F, metadata_fn: F,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
@@ -626,6 +668,7 @@ fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Meta
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<std::fs::Metadata> { ) -> CommandResult<std::fs::Metadata> {
get_fs_metadata( get_fs_metadata(
permission,
metadata_fn, metadata_fn,
webview, webview,
global_scope, global_scope,
@@ -636,6 +679,7 @@ fn get_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Meta
} }
fn get_fs_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Metadata>>( fn get_fs_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::Metadata>>(
permission: &str,
metadata_fn: F, metadata_fn: F,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
@@ -644,6 +688,7 @@ fn get_fs_metadata<R: Runtime, F: FnOnce(&PathBuf) -> std::io::Result<std::fs::M
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<std::fs::Metadata> { ) -> CommandResult<std::fs::Metadata> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
permission,
webview, webview,
global_scope, global_scope,
command_scope, command_scope,
@@ -668,6 +713,7 @@ pub fn stat<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<FileInfo> { ) -> CommandResult<FileInfo> {
let metadata = get_metadata( let metadata = get_metadata(
"stat",
|p| std::fs::metadata(p), |p| std::fs::metadata(p),
&webview, &webview,
&global_scope, &global_scope,
@@ -688,6 +734,7 @@ pub fn lstat<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<FileInfo> { ) -> CommandResult<FileInfo> {
let metadata = get_metadata( let metadata = get_metadata(
"lstat",
|p| std::fs::symlink_metadata(p), |p| std::fs::symlink_metadata(p),
&webview, &webview,
&global_scope, &global_scope,
@@ -716,6 +763,7 @@ pub async fn truncate<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"truncate",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -784,23 +832,13 @@ fn default_create_value() -> bool {
true true
} }
#[tauri::command] async fn write_file_inner<R: Runtime>(
pub async fn write_file<R: Runtime>( permission: &str,
webview: Webview<R>, webview: Webview<R>,
global_scope: GlobalScope<Entry>, global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>, command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>, request: tauri::ipc::Request<'_>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let data = match request.body() {
tauri::ipc::InvokeBody::Raw(data) => Cow::Borrowed(data),
tauri::ipc::InvokeBody::Json(serde_json::Value::Array(data)) => Cow::Owned(
data.iter()
.flat_map(|v| v.as_number().and_then(|v| v.as_u64().map(|v| v as u8)))
.collect(),
),
_ => return Err(anyhow::anyhow!("unexpected invoke body").into()),
};
let path = request let path = request
.headers() .headers()
.get("path") .get("path")
@@ -811,6 +849,7 @@ pub async fn write_file<R: Runtime>(
.map_err(|_| anyhow::anyhow!("path is not a valid UTF-8").into()) .map_err(|_| anyhow::anyhow!("path is not a valid UTF-8").into())
}) })
.and_then(|p| SafeFilePath::from_str(&p).map_err(CommandError::from))?; .and_then(|p| SafeFilePath::from_str(&p).map_err(CommandError::from))?;
let options: Option<WriteFileOptions> = request let options: Option<WriteFileOptions> = request
.headers() .headers()
.get("options") .get("options")
@@ -818,6 +857,7 @@ pub async fn write_file<R: Runtime>(
.and_then(|opts| serde_json::from_str(opts).ok()); .and_then(|opts| serde_json::from_str(opts).ok());
let (mut file, path) = resolve_file( let (mut file, path) = resolve_file(
permission,
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -853,6 +893,16 @@ pub async fn write_file<R: Runtime>(
}, },
)?; )?;
let data = match request.body() {
tauri::ipc::InvokeBody::Raw(data) => Cow::Borrowed(data),
tauri::ipc::InvokeBody::Json(serde_json::Value::Array(data)) => Cow::Owned(
data.iter()
.flat_map(|v| v.as_number().and_then(|v| v.as_u64().map(|v| v as u8)))
.collect(),
),
_ => return Err(anyhow::anyhow!("unexpected invoke body").into()),
};
file.write_all(&data) file.write_all(&data)
.map_err(|e| { .map_err(|e| {
format!( format!(
@@ -863,6 +913,16 @@ pub async fn write_file<R: Runtime>(
.map_err(Into::into) .map_err(Into::into)
} }
#[tauri::command]
pub async fn write_file<R: Runtime>(
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>,
) -> CommandResult<()> {
write_file_inner("write-file", webview, global_scope, command_scope, request).await
}
// TODO, remove in v3, rely on `write_file` command instead // TODO, remove in v3, rely on `write_file` command instead
#[tauri::command] #[tauri::command]
pub async fn write_text_file<R: Runtime>( pub async fn write_text_file<R: Runtime>(
@@ -871,7 +931,14 @@ pub async fn write_text_file<R: Runtime>(
command_scope: CommandScope<Entry>, command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>, request: tauri::ipc::Request<'_>,
) -> CommandResult<()> { ) -> CommandResult<()> {
write_file(webview, global_scope, command_scope, request).await write_file_inner(
"write-text-file",
webview,
global_scope,
command_scope,
request,
)
.await
} }
#[tauri::command] #[tauri::command]
@@ -883,6 +950,7 @@ pub fn exists<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<bool> { ) -> CommandResult<bool> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"exists",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -901,6 +969,7 @@ pub async fn size<R: Runtime>(
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<u64> { ) -> CommandResult<u64> {
let resolved_path = resolve_path( let resolved_path = resolve_path(
"size",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,
@@ -943,16 +1012,25 @@ fn get_dir_size(path: &PathBuf) -> CommandResult<u64> {
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
pub fn resolve_file<R: Runtime>( pub fn resolve_file<R: Runtime>(
permission: &str,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
command_scope: &CommandScope<Entry>, command_scope: &CommandScope<Entry>,
path: SafeFilePath, path: SafeFilePath,
open_options: OpenOptions, open_options: OpenOptions,
) -> CommandResult<(File, PathBuf)> { ) -> CommandResult<(File, PathBuf)> {
resolve_file_in_fs(webview, global_scope, command_scope, path, open_options) resolve_file_in_fs(
permission,
webview,
global_scope,
command_scope,
path,
open_options,
)
} }
fn resolve_file_in_fs<R: Runtime>( fn resolve_file_in_fs<R: Runtime>(
permission: &str,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
command_scope: &CommandScope<Entry>, command_scope: &CommandScope<Entry>,
@@ -960,6 +1038,7 @@ fn resolve_file_in_fs<R: Runtime>(
open_options: OpenOptions, open_options: OpenOptions,
) -> CommandResult<(File, PathBuf)> { ) -> CommandResult<(File, PathBuf)> {
let path = resolve_path( let path = resolve_path(
permission,
webview, webview,
global_scope, global_scope,
command_scope, command_scope,
@@ -980,6 +1059,7 @@ fn resolve_file_in_fs<R: Runtime>(
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
pub fn resolve_file<R: Runtime>( pub fn resolve_file<R: Runtime>(
permission: &str,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
command_scope: &CommandScope<Entry>, command_scope: &CommandScope<Entry>,
@@ -997,6 +1077,7 @@ pub fn resolve_file<R: Runtime>(
Ok((file, path)) Ok((file, path))
} }
SafeFilePath::Path(path) => resolve_file_in_fs( SafeFilePath::Path(path) => resolve_file_in_fs(
permission,
webview, webview,
global_scope, global_scope,
command_scope, command_scope,
@@ -1007,6 +1088,7 @@ pub fn resolve_file<R: Runtime>(
} }
pub fn resolve_path<R: Runtime>( pub fn resolve_path<R: Runtime>(
permission: &str,
webview: &Webview<R>, webview: &Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: &GlobalScope<Entry>,
command_scope: &CommandScope<Entry>, command_scope: &CommandScope<Entry>,
@@ -1052,7 +1134,17 @@ pub fn resolve_path<R: Runtime>(
if fs_scope.scope.is_allowed(&path) || scope.is_allowed(&path) { if fs_scope.scope.is_allowed(&path) || scope.is_allowed(&path) {
Ok(path) Ok(path)
} else { } else {
Err(CommandError::Plugin(Error::PathForbidden(path))) #[cfg(not(debug_assertions))]
return Err(CommandError::Plugin(Error::PathForbidden(path)));
#[cfg(debug_assertions)]
Err(
anyhow::anyhow!(
"forbidden path: {}, maybe it is not allowed on the scope for `allow-{permission}` permission in your capability file",
path.display()
)
)
.map_err(Into::into)
} }
} }
+1 -1
View File
@@ -89,7 +89,7 @@ impl<R: Runtime> Fs<R> {
std::fs::File::from_raw_fd(fd) std::fs::File::from_raw_fd(fd)
}) })
} else { } else {
todo!() unimplemented!()
} }
} }
} }
+1
View File
@@ -49,6 +49,7 @@ pub fn watch<R: Runtime>(
.into_iter() .into_iter()
.map(|path| { .map(|path| {
resolve_path( resolve_path(
"watch",
&webview, &webview,
&global_scope, &global_scope,
&command_scope, &command_scope,