@@ -469,7 +469,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
469
469
let source_map = self . tcx . sess . source_map ( ) ;
470
470
let all_source_files = source_map. files ( ) ;
471
471
472
- let working_dir = self . tcx . sess . working_dir . stable_name ( ) ;
473
472
// By replacing the `Option` with `None`, we ensure that we can't
474
473
// accidentally serialize any more `Span`s after the source map encoding
475
474
// is done.
@@ -488,23 +487,36 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
488
487
} )
489
488
. map ( |( _, source_file) | {
490
489
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) => {
495
491
let mut adapted = ( * * source_file) . clone ( ) ;
496
- adapted. name = match name {
492
+ adapted. name = FileName :: Real ( match realname {
497
493
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
+ }
499
511
}
500
512
RealFileName :: Remapped { local_path : _, virtual_name } => {
501
- FileName :: Real ( RealFileName :: Remapped {
513
+ RealFileName :: Remapped {
502
514
// We do not want any local path to be exported into metadata
503
515
local_path : None ,
504
516
virtual_name : virtual_name. clone ( ) ,
505
- } )
517
+ }
506
518
}
507
- } ;
519
+ } ) ;
508
520
adapted. name_hash = {
509
521
let mut hasher: StableHasher = StableHasher :: new ( ) ;
510
522
adapted. name . hash ( & mut hasher) ;
0 commit comments