@@ -469,7 +469,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
469469 let source_map = self . tcx . sess . source_map ( ) ;
470470 let all_source_files = source_map. files ( ) ;
471471
472- let working_dir = self . tcx . sess . working_dir . stable_name ( ) ;
473472 // By replacing the `Option` with `None`, we ensure that we can't
474473 // accidentally serialize any more `Span`s after the source map encoding
475474 // is done.
@@ -488,23 +487,36 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
488487 } )
489488 . map ( |( _, source_file) | {
490489 let mut adapted = match source_file. name {
491- FileName :: Real ( ref name) => {
492- // Expand all local paths to absolute paths because
493- // any relative paths are potentially relative to a
494- // wrong directory.
490+ FileName :: Real ( ref realname) => {
495491 let mut adapted = ( * * source_file) . clone ( ) ;
496- adapted. name = match name {
492+ adapted. name = FileName :: Real ( match realname {
497493 RealFileName :: LocalPath ( local_path) => {
498- Path :: new ( & working_dir) . join ( local_path) . into ( )
494+ // Prepend path of working directory onto local path.
495+ // because relative paths are potentially relative to a
496+ // wrong directory.
497+ let working_dir = & self . tcx . sess . working_dir ;
498+ if let RealFileName :: LocalPath ( absolute) = working_dir {
499+ // If working_dir has not been remapped, then we emit a
500+ // LocalPath variant as it's likely to be a valid path
501+ RealFileName :: LocalPath ( Path :: new ( absolute) . join ( local_path) )
502+ } else {
503+ // If working_dir has been remapped, then we emit
504+ // Remapped variant as the expanded path won't be valid
505+ RealFileName :: Remapped {
506+ local_path : None ,
507+ virtual_name : Path :: new ( working_dir. stable_name ( ) )
508+ . join ( local_path) ,
509+ }
510+ }
499511 }
500512 RealFileName :: Remapped { local_path : _, virtual_name } => {
501- FileName :: Real ( RealFileName :: Remapped {
513+ RealFileName :: Remapped {
502514 // We do not want any local path to be exported into metadata
503515 local_path : None ,
504516 virtual_name : virtual_name. clone ( ) ,
505- } )
517+ }
506518 }
507- } ;
519+ } ) ;
508520 adapted. name_hash = {
509521 let mut hasher: StableHasher = StableHasher :: new ( ) ;
510522 adapted. name . hash ( & mut hasher) ;
0 commit comments