@@ -234,51 +234,54 @@ impl<Fs: FileSystem> Cache<Fs> {
234234 return Err ( io:: Error :: new ( io:: ErrorKind :: NotFound , "Circular symlink" ) . into ( ) ) ;
235235 }
236236
237- if let Some ( res) = path. canonicalized . get ( ) . map ( |res| {
238- res. as_ref ( ) . map_err ( Clone :: clone) . and_then ( |weak| {
239- weak. upgrade ( ) . map ( CachedPath ) . ok_or_else ( || {
240- ResolveError :: from ( io:: Error :: other ( "Canonicalized path was dropped" ) )
241- } )
242- } )
243- } ) {
244- return res;
245- }
246-
247- path. canonicalizing . store ( tid, Ordering :: Release ) ;
248- let res = path. parent ( ) . map_or_else (
249- || Ok ( path. normalize_root ( self ) ) ,
250- |parent| {
251- self . canonicalize_impl ( & parent) . and_then ( |parent_canonical| {
252- let normalized = parent_canonical
253- . normalize_with ( path. path ( ) . strip_prefix ( parent. path ( ) ) . unwrap ( ) , self ) ;
237+ #[ expect( clippy:: collection_is_never_read) ]
238+ let mut _temp = None ;
254239
255- if self . fs . symlink_metadata ( path. path ( ) ) . is_ok_and ( |m| m. is_symlink ) {
256- let link = self . fs . read_link ( normalized. path ( ) ) ?;
257- if link. is_absolute ( ) {
258- return self . canonicalize_impl ( & self . value ( & link. normalize ( ) ) ) ;
259- } else if let Some ( dir) = normalized. parent ( ) {
260- // Symlink is relative `../../foo.js`, use the path directory
261- // to resolve this symlink.
262- return self . canonicalize_impl ( & dir. normalize_with ( & link, self ) ) ;
263- }
264- debug_assert ! (
265- false ,
266- "Failed to get path parent for {}." ,
267- normalized. path( ) . display( )
268- ) ;
269- }
240+ path. canonicalized
241+ . get_or_init ( || {
242+ path. canonicalizing . store ( tid, Ordering :: Release ) ;
270243
271- Ok ( normalized)
272- } )
273- } ,
274- ) ;
244+ let res = path. parent ( ) . map_or_else (
245+ || Ok ( path. normalize_root ( self ) ) ,
246+ |parent| {
247+ self . canonicalize_impl ( & parent) . and_then ( |parent_canonical| {
248+ let normalized = parent_canonical. normalize_with (
249+ path. path ( ) . strip_prefix ( parent. path ( ) ) . unwrap ( ) ,
250+ self ,
251+ ) ;
275252
276- path. canonicalizing . store ( 0 , Ordering :: Release ) ;
253+ if self . fs . symlink_metadata ( path. path ( ) ) . is_ok_and ( |m| m. is_symlink ) {
254+ let link = self . fs . read_link ( normalized. path ( ) ) ?;
255+ if link. is_absolute ( ) {
256+ return self . canonicalize_impl ( & self . value ( & link. normalize ( ) ) ) ;
257+ } else if let Some ( dir) = normalized. parent ( ) {
258+ // Symlink is relative `../../foo.js`, use the path directory
259+ // to resolve this symlink.
260+ return self
261+ . canonicalize_impl ( & dir. normalize_with ( & link, self ) ) ;
262+ }
263+ debug_assert ! (
264+ false ,
265+ "Failed to get path parent for {}." ,
266+ normalized. path( ) . display( )
267+ ) ;
268+ }
277269
278- // Convert to Weak reference for storage
279- let weak_res = res. as_ref ( ) . map_err ( Clone :: clone) . map ( |cp| Arc :: downgrade ( & cp. 0 ) ) ;
280- path. canonicalized . set ( weak_res) . expect ( "checked by path.canonicalizing" ) ;
270+ Ok ( normalized)
271+ } )
272+ } ,
273+ ) ;
281274
282- res
275+ path. canonicalizing . store ( 0 , Ordering :: Release ) ;
276+ _temp = res. as_ref ( ) . ok ( ) . map ( |cp| Arc :: clone ( & cp. 0 ) ) ;
277+ res. map ( |cp| Arc :: downgrade ( & cp. 0 ) )
278+ } )
279+ . as_ref ( )
280+ . map_err ( Clone :: clone)
281+ . and_then ( |weak| {
282+ weak. upgrade ( ) . map ( CachedPath ) . ok_or_else ( || {
283+ ResolveError :: from ( io:: Error :: other ( "Canonicalized path was dropped" ) )
284+ } )
285+ } )
283286 }
284287}
0 commit comments