diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 17de34c8436f..090442bf64bd 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -105,6 +105,9 @@ pub trait FileLoader { /// Read the contents of a potentially non-UTF-8 file into memory. /// We don't normalize binary files, so we can start in an Arc. fn read_binary_file(&self, path: &Path) -> io::Result>; + + /// Current working directory + fn current_directory(&self) -> io::Result; } /// A FileLoader that uses std::fs to load real files. @@ -170,6 +173,10 @@ fn read_binary_file(&self, path: &Path) -> io::Result> { file.read_to_end(&mut bytes)?; Ok(bytes.into()) } + + fn current_directory(&self) -> io::Result { + std::env::current_dir() + } } // _____________________________________________________________________________ @@ -198,6 +205,9 @@ pub struct SourceMap { // `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`. path_mapping: FilePathMapping, + /// Current working directory + working_dir: RealFileName, + /// The algorithm used for hashing the contents of each source file. hash_kind: SourceFileHashAlgorithm, @@ -221,8 +231,14 @@ pub fn new(path_mapping: FilePathMapping) -> SourceMap { pub fn with_inputs( SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind }: SourceMapInputs, ) -> SourceMap { + let cwd = file_loader + .current_directory() + .expect("expecting a current working directory to exist"); + let working_dir = cwd.to_path_buf().into(); + debug!(?working_dir); SourceMap { files: Default::default(), + working_dir, file_loader: IntoDynSyncSend(file_loader), path_mapping, hash_kind, @@ -234,6 +250,10 @@ pub fn path_mapping(&self) -> &FilePathMapping { &self.path_mapping } + pub fn working_dir(&self) -> &RealFileName { + &self.working_dir + } + pub fn file_exists(&self, path: &Path) -> bool { self.file_loader.file_exists(path) }