@@ -9,6 +9,8 @@ use crate::{
99} ;
1010
1111use super :: InvokeContext ;
12+ #[ allow( unused_imports) ]
13+ use anyhow:: Context ;
1214use serde:: {
1315 de:: { Deserializer , Error as DeError } ,
1416 Deserialize , Serialize ,
@@ -128,14 +130,16 @@ impl Cmd {
128130 path : SafePathBuf ,
129131 options : Option < FileOperationOptions > ,
130132 ) -> super :: Result < Vec < u8 > > {
131- file :: read_binary ( resolve_path (
133+ let resolved_path = resolve_path (
132134 & context. config ,
133135 & context. package_info ,
134136 & context. window ,
135137 path,
136138 options. and_then ( |o| o. dir ) ,
137- ) ?)
138- . map_err ( Into :: into)
139+ ) ?;
140+ file:: read_binary ( & resolved_path)
141+ . with_context ( || format ! ( "path: {}" , resolved_path. 0 . display( ) ) )
142+ . map_err ( Into :: into)
139143 }
140144
141145 #[ module_command_handler( fs_write_file, "fs > writeFile" ) ]
@@ -145,15 +149,17 @@ impl Cmd {
145149 contents : Vec < u8 > ,
146150 options : Option < FileOperationOptions > ,
147151 ) -> super :: Result < ( ) > {
148- File :: create ( resolve_path (
152+ let resolved_path = resolve_path (
149153 & context. config ,
150154 & context. package_info ,
151155 & context. window ,
152156 path,
153157 options. and_then ( |o| o. dir ) ,
154- ) ?)
155- . map_err ( Into :: into)
156- . and_then ( |mut f| f. write_all ( & contents) . map_err ( |err| err. into ( ) ) )
158+ ) ?;
159+ File :: create ( & resolved_path)
160+ . with_context ( || format ! ( "path: {}" , resolved_path. 0 . display( ) ) )
161+ . map_err ( Into :: into)
162+ . and_then ( |mut f| f. write_all ( & contents) . map_err ( |err| err. into ( ) ) )
157163 }
158164
159165 #[ module_command_handler( fs_read_dir, "fs > readDir" ) ]
@@ -167,17 +173,16 @@ impl Cmd {
167173 } else {
168174 ( false , None )
169175 } ;
170- dir:: read_dir (
171- resolve_path (
172- & context. config ,
173- & context. package_info ,
174- & context. window ,
175- path,
176- dir,
177- ) ?,
178- recursive,
179- )
180- . map_err ( Into :: into)
176+ let resolved_path = resolve_path (
177+ & context. config ,
178+ & context. package_info ,
179+ & context. window ,
180+ path,
181+ dir,
182+ ) ?;
183+ dir:: read_dir ( & resolved_path, recursive)
184+ . with_context ( || format ! ( "path: {}" , resolved_path. 0 . display( ) ) )
185+ . map_err ( Into :: into)
181186 }
182187
183188 #[ module_command_handler( fs_copy_file, "fs > copyFile" ) ]
@@ -194,7 +199,7 @@ impl Cmd {
194199 & context. package_info ,
195200 & context. window ,
196201 source,
197- Some ( dir. clone ( ) ) ,
202+ Some ( dir) ,
198203 ) ?,
199204 resolve_path (
200205 & context. config ,
@@ -206,7 +211,8 @@ impl Cmd {
206211 ) ,
207212 None => ( source, destination) ,
208213 } ;
209- fs:: copy ( src, dest) ?;
214+ fs:: copy ( src. clone ( ) , dest. clone ( ) )
215+ . with_context ( || format ! ( "source: {}, dest: {}" , src. 0 . display( ) , dest. 0 . display( ) ) ) ?;
210216 Ok ( ( ) )
211217 }
212218
@@ -229,9 +235,11 @@ impl Cmd {
229235 dir,
230236 ) ?;
231237 if recursive {
232- fs:: create_dir_all ( resolved_path) ?;
238+ fs:: create_dir_all ( & resolved_path)
239+ . with_context ( || format ! ( "path: {}" , resolved_path. 0 . display( ) ) ) ?;
233240 } else {
234- fs:: create_dir ( resolved_path) ?;
241+ fs:: create_dir ( & resolved_path)
242+ . with_context ( || format ! ( "path: {} (non recursive)" , resolved_path. 0 . display( ) ) ) ?;
235243 }
236244
237245 Ok ( ( ) )
@@ -256,9 +264,11 @@ impl Cmd {
256264 dir,
257265 ) ?;
258266 if recursive {
259- fs:: remove_dir_all ( resolved_path) ?;
267+ fs:: remove_dir_all ( & resolved_path)
268+ . with_context ( || format ! ( "path: {}" , resolved_path. 0 . display( ) ) ) ?;
260269 } else {
261- fs:: remove_dir ( resolved_path) ?;
270+ fs:: remove_dir ( & resolved_path)
271+ . with_context ( || format ! ( "path: {} (non recursive)" , resolved_path. 0 . display( ) ) ) ?;
262272 }
263273
264274 Ok ( ( ) )
@@ -277,7 +287,8 @@ impl Cmd {
277287 path,
278288 options. and_then ( |o| o. dir ) ,
279289 ) ?;
280- fs:: remove_file ( resolved_path) ?;
290+ fs:: remove_file ( & resolved_path)
291+ . with_context ( || format ! ( "path: {}" , resolved_path. 0 . display( ) ) ) ?;
281292 Ok ( ( ) )
282293 }
283294
@@ -295,7 +306,7 @@ impl Cmd {
295306 & context. package_info ,
296307 & context. window ,
297308 old_path,
298- Some ( dir. clone ( ) ) ,
309+ Some ( dir) ,
299310 ) ?,
300311 resolve_path (
301312 & context. config ,
@@ -307,7 +318,9 @@ impl Cmd {
307318 ) ,
308319 None => ( old_path, new_path) ,
309320 } ;
310- fs:: rename ( old, new) . map_err ( Into :: into)
321+ fs:: rename ( & old, & new)
322+ . with_context ( || format ! ( "old: {}, new: {}" , old. 0 . display( ) , new. 0 . display( ) ) )
323+ . map_err ( Into :: into)
311324 }
312325}
313326
@@ -320,7 +333,7 @@ fn resolve_path<R: Runtime>(
320333 dir : Option < BaseDirectory > ,
321334) -> super :: Result < SafePathBuf > {
322335 let env = window. state :: < Env > ( ) . inner ( ) ;
323- match crate :: api:: path:: resolve_path ( config, package_info, env, path, dir) {
336+ match crate :: api:: path:: resolve_path ( config, package_info, env, & path, dir) {
324337 Ok ( path) => {
325338 if window. state :: < Scopes > ( ) . fs . is_allowed ( & path) {
326339 Ok ( SafePathBuf ( path) )
@@ -330,7 +343,8 @@ fn resolve_path<R: Runtime>(
330343 ) )
331344 }
332345 }
333- Err ( e) => Err ( e. into ( ) ) ,
346+ Err ( e) => super :: Result :: < SafePathBuf > :: Err ( e. into ( ) )
347+ . with_context ( || format ! ( "path: {}, base dir: {:?}" , path. 0 . display( ) , dir) ) ,
334348 }
335349}
336350
0 commit comments