Skip to content

Fix -Zremap-path-scope rmeta handling #139550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

match source_file.name {
FileName::Real(ref original_file_name) => {
// FIXME: This should probably to conditionally remapped under
// a RemapPathScopeComponents but which one?
let adapted_file_name = source_map
.path_mapping()
.to_embeddable_absolute_path(original_file_name.clone(), working_directory);
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
use rustc_span::edition::{DEFAULT_EDITION, EDITION_NAME_LIST, Edition, LATEST_STABLE_EDITION};
use rustc_span::source_map::FilePathMapping;
use rustc_span::{
FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm, Symbol, sym,
FileName, FileNameDisplayPreference, FileNameEmbeddablePreference, RealFileName,
SourceFileHashAlgorithm, Symbol, sym,
};
use rustc_target::spec::{
FramePointer, LinkSelfContainedComponents, LinkerFeatures, SplitDebuginfo, Target, TargetTuple,
Expand Down Expand Up @@ -1316,6 +1317,11 @@ fn file_path_mapping(
} else {
FileNameDisplayPreference::Local
},
if unstable_opts.remap_path_scope.is_all() {
FileNameEmbeddablePreference::RemappedOnly
} else {
FileNameEmbeddablePreference::LocalAndRemapped
},
)
}

Expand Down
34 changes: 11 additions & 23 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub fn with_metavar_spans<R>(f: impl FnOnce(&MetavarSpansMap) -> R) -> R {

// FIXME: We should use this enum or something like it to get rid of the
// use of magic `/rust/1.x/...` paths across the board.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Decodable)]
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Decodable, Encodable)]
pub enum RealFileName {
LocalPath(PathBuf),
/// For remapped paths (namely paths into libstd that have been mapped
Expand All @@ -250,28 +250,6 @@ impl Hash for RealFileName {
}
}

// This is functionally identical to #[derive(Encodable)], with the exception of
// an added assert statement
impl<S: Encoder> Encodable<S> for RealFileName {
fn encode(&self, encoder: &mut S) {
match *self {
RealFileName::LocalPath(ref local_path) => {
encoder.emit_u8(0);
local_path.encode(encoder);
}

RealFileName::Remapped { ref local_path, ref virtual_name } => {
encoder.emit_u8(1);
// For privacy and build reproducibility, we must not embed host-dependant path
// in artifacts if they have been remapped by --remap-path-prefix
assert!(local_path.is_none());
local_path.encode(encoder);
virtual_name.encode(encoder);
}
}
}
}

impl RealFileName {
/// Returns the path suitable for reading from the file system on the local host,
/// if this information exists.
Expand Down Expand Up @@ -368,6 +346,16 @@ impl From<PathBuf> for FileName {
}
}

#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum FileNameEmbeddablePreference {
/// If a remapped path is available, only embed the `virtual_path` and omit the `local_path`.
///
/// Otherwise embed the local-path into the `virtual_path`.
RemappedOnly,
/// Embed the original path as well as its remapped `virtual_path` component if available.
LocalAndRemapped,
}

#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum FileNameDisplayPreference {
/// Display the path after the application of rewrite rules provided via `--remap-path-prefix`.
Expand Down
80 changes: 44 additions & 36 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,18 +1108,28 @@ pub fn get_source_map() -> Option<Arc<SourceMap>> {
pub struct FilePathMapping {
mapping: Vec<(PathBuf, PathBuf)>,
filename_display_for_diagnostics: FileNameDisplayPreference,
filename_embeddable_preference: FileNameEmbeddablePreference,
}

impl FilePathMapping {
pub fn empty() -> FilePathMapping {
FilePathMapping::new(Vec::new(), FileNameDisplayPreference::Local)
FilePathMapping::new(
Vec::new(),
FileNameDisplayPreference::Local,
FileNameEmbeddablePreference::RemappedOnly,
)
}

pub fn new(
mapping: Vec<(PathBuf, PathBuf)>,
filename_display_for_diagnostics: FileNameDisplayPreference,
filename_embeddable_preference: FileNameEmbeddablePreference,
) -> FilePathMapping {
FilePathMapping { mapping, filename_display_for_diagnostics }
FilePathMapping {
mapping,
filename_display_for_diagnostics,
filename_embeddable_preference,
}
}

/// Applies any path prefix substitution as defined by the mapping.
Expand Down Expand Up @@ -1217,11 +1227,13 @@ impl FilePathMapping {
) -> RealFileName {
match file_path {
// Anything that's already remapped we don't modify, except for erasing
// the `local_path` portion.
RealFileName::Remapped { local_path: _, virtual_name } => {
// the `local_path` portion (if desired).
RealFileName::Remapped { local_path, virtual_name } => {
RealFileName::Remapped {
// We do not want any local path to be exported into metadata
local_path: None,
local_path: match self.filename_embeddable_preference {
FileNameEmbeddablePreference::RemappedOnly => None,
FileNameEmbeddablePreference::LocalAndRemapped => local_path,
},
// We use the remapped name verbatim, even if it looks like a relative
// path. The assumption is that the user doesn't want us to further
// process paths that have gone through remapping.
Expand All @@ -1231,12 +1243,18 @@ impl FilePathMapping {

RealFileName::LocalPath(unmapped_file_path) => {
// If no remapping has been applied yet, try to do so
let (new_path, was_remapped) = self.map_prefix(unmapped_file_path);
let (new_path, was_remapped) = self.map_prefix(&unmapped_file_path);
if was_remapped {
// It was remapped, so don't modify further
return RealFileName::Remapped {
local_path: None,
virtual_name: new_path.into_owned(),
// But still provide the local path if desired
local_path: match self.filename_embeddable_preference {
FileNameEmbeddablePreference::RemappedOnly => None,
FileNameEmbeddablePreference::LocalAndRemapped => {
Some(unmapped_file_path)
}
},
};
}

Expand All @@ -1252,17 +1270,23 @@ impl FilePathMapping {

match working_directory {
RealFileName::LocalPath(unmapped_working_dir_abs) => {
let file_path_abs = unmapped_working_dir_abs.join(unmapped_file_path_rel);
let unmapped_file_path_abs =
unmapped_working_dir_abs.join(unmapped_file_path_rel);

// Although neither `working_directory` nor the file name were subject
// to path remapping, the concatenation between the two may be. Hence
// we need to do a remapping here.
let (file_path_abs, was_remapped) = self.map_prefix(file_path_abs);
let (file_path_abs, was_remapped) =
self.map_prefix(&unmapped_file_path_abs);
if was_remapped {
RealFileName::Remapped {
// Erase the actual path
local_path: None,
virtual_name: file_path_abs.into_owned(),
local_path: match self.filename_embeddable_preference {
FileNameEmbeddablePreference::RemappedOnly => None,
FileNameEmbeddablePreference::LocalAndRemapped => {
Some(unmapped_file_path_abs)
}
},
}
} else {
// No kind of remapping applied to this path, so
Expand All @@ -1271,43 +1295,27 @@ impl FilePathMapping {
}
}
RealFileName::Remapped {
local_path: _,
local_path,
virtual_name: remapped_working_dir_abs,
} => {
// If working_directory has been remapped, then we emit
// Remapped variant as the expanded path won't be valid
RealFileName::Remapped {
local_path: None,
virtual_name: Path::new(remapped_working_dir_abs)
.join(unmapped_file_path_rel),
.join(&unmapped_file_path_rel),
local_path: match self.filename_embeddable_preference {
FileNameEmbeddablePreference::RemappedOnly => None,
FileNameEmbeddablePreference::LocalAndRemapped => local_path
.as_ref()
.map(|local_path| local_path.join(unmapped_file_path_rel)),
},
}
}
}
}
}
}

/// Expand a relative path to an absolute path **without** remapping taken into account.
///
/// The resulting `RealFileName` will have its `virtual_path` portion erased if
/// possible (i.e. if there's also a remapped path).
pub fn to_local_embeddable_absolute_path(
&self,
file_path: RealFileName,
working_directory: &RealFileName,
) -> RealFileName {
let file_path = file_path.local_path_if_available();
if file_path.is_absolute() {
// No remapping has applied to this path and it is absolute,
// so the working directory cannot influence it either, so
// we are done.
return RealFileName::LocalPath(file_path.to_path_buf());
}
debug_assert!(file_path.is_relative());
let working_directory = working_directory.local_path_if_available();
RealFileName::LocalPath(Path::new(working_directory).join(file_path))
}

/// Attempts to (heuristically) reverse a prefix mapping.
///
/// Returns [`Some`] if there is exactly one mapping where the "to" part is
Expand Down
73 changes: 73 additions & 0 deletions compiler/rustc_span/src/source_map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ fn path_prefix_remapping() {
let mapping = &FilePathMapping::new(
vec![(path("abc/def"), path("foo"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(map_path_prefix(mapping, "abc/def/src/main.rs"), path_str("foo/src/main.rs"));
Expand All @@ -316,6 +317,7 @@ fn path_prefix_remapping() {
let mapping = &FilePathMapping::new(
vec![(path("abc/def"), path("/foo"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(map_path_prefix(mapping, "abc/def/src/main.rs"), path_str("/foo/src/main.rs"));
Expand All @@ -327,6 +329,7 @@ fn path_prefix_remapping() {
let mapping = &FilePathMapping::new(
vec![(path("/abc/def"), path("foo"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(map_path_prefix(mapping, "/abc/def/src/main.rs"), path_str("foo/src/main.rs"));
Expand All @@ -338,6 +341,7 @@ fn path_prefix_remapping() {
let mapping = &FilePathMapping::new(
vec![(path("/abc/def"), path("/foo"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(map_path_prefix(mapping, "/abc/def/src/main.rs"), path_str("/foo/src/main.rs"));
Expand All @@ -351,6 +355,7 @@ fn path_prefix_remapping_expand_to_absolute() {
let mapping = &FilePathMapping::new(
vec![(path("/foo"), path("FOO")), (path("/bar"), path("BAR"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);
let working_directory = path("/foo");
let working_directory = RealFileName::Remapped {
Expand Down Expand Up @@ -448,13 +453,79 @@ fn path_prefix_remapping_expand_to_absolute() {
);
}

#[test]
fn path_prefix_remapping_expand_to_absolute_and_local() {
// "virtual" working directory is relative path
let mapping = &FilePathMapping::new(
vec![(path("/foo"), path("FOO")), (path("/bar"), path("BAR"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::LocalAndRemapped,
);
let working_directory = path("/foo");
let working_directory = RealFileName::Remapped {
local_path: Some(working_directory.clone()),
virtual_name: mapping.map_prefix(working_directory).0.into_owned(),
};

assert_eq!(working_directory.remapped_path_if_available(), path("FOO"));

// Unmapped absolute path
assert_eq!(
mapping.to_embeddable_absolute_path(
RealFileName::LocalPath(path("/foo/src/main.rs")),
&working_directory
),
RealFileName::Remapped {
local_path: Some(path("/foo/src/main.rs")),
virtual_name: path("FOO/src/main.rs")
}
);

// Unmapped absolute path with unrelated working directory
assert_eq!(
mapping.to_embeddable_absolute_path(
RealFileName::LocalPath(path("/bar/src/main.rs")),
&working_directory
),
RealFileName::Remapped {
local_path: Some(path("/bar/src/main.rs")),
virtual_name: path("BAR/src/main.rs")
}
);

// Already remapped absolute path, with unrelated working directory
assert_eq!(
mapping.to_embeddable_absolute_path(
RealFileName::Remapped {
local_path: Some(path("/bar/src/main.rs")),
virtual_name: path("BAR/src/main.rs"),
},
&working_directory
),
RealFileName::Remapped {
local_path: Some(path("/bar/src/main.rs")),
virtual_name: path("BAR/src/main.rs")
}
);

// Already remapped relative path
assert_eq!(
mapping.to_embeddable_absolute_path(
RealFileName::Remapped { local_path: None, virtual_name: path("XYZ/src/main.rs") },
&working_directory
),
RealFileName::Remapped { local_path: None, virtual_name: path("XYZ/src/main.rs") }
);
}

#[test]
fn path_prefix_remapping_reverse() {
// Ignores options without alphanumeric chars.
{
let mapping = &FilePathMapping::new(
vec![(path("abc"), path("/")), (path("def"), path("."))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(reverse_map_prefix(mapping, "/hello.rs"), None);
Expand All @@ -466,6 +537,7 @@ fn path_prefix_remapping_reverse() {
let mapping = &FilePathMapping::new(
vec![(path("abc"), path("/redacted")), (path("def"), path("/redacted"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(reverse_map_prefix(mapping, "/redacted/hello.rs"), None);
Expand All @@ -476,6 +548,7 @@ fn path_prefix_remapping_reverse() {
let mapping = &FilePathMapping::new(
vec![(path("abc"), path("/redacted")), (path("def/ghi"), path("/fake/dir"))],
FileNameDisplayPreference::Remapped,
FileNameEmbeddablePreference::RemappedOnly,
);

assert_eq!(
Expand Down
Loading
Loading