From c8a9f88f097c13fb297f48ca898914dd6d7e683a Mon Sep 17 00:00:00 2001
From: Eric Huss <eric@huss.org>
Date: Mon, 20 May 2019 07:44:41 -0700
Subject: [PATCH] Ignore remap-path-prefix in metadata hash.

---
 .../compiler/context/compilation_files.rs     | 20 ++++++++++++---
 src/cargo/core/compiler/fingerprint.rs        |  2 +-
 tests/testsuite/rustflags.rs                  | 25 +++++++++++++++++++
 3 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/src/cargo/core/compiler/context/compilation_files.rs b/src/cargo/core/compiler/context/compilation_files.rs
index b079307cd36..8bb8ded377d 100644
--- a/src/cargo/core/compiler/context/compilation_files.rs
+++ b/src/cargo/core/compiler/context/compilation_files.rs
@@ -513,10 +513,24 @@ fn compute_metadata<'a, 'cfg>(
     // Throw in the rustflags we're compiling with.
     // This helps when the target directory is a shared cache for projects with different cargo configs,
     // or if the user is experimenting with different rustflags manually.
-    if unit.mode.is_doc() {
-        cx.bcx.rustdocflags_args(unit).hash(&mut hasher);
+    let mut flags = if unit.mode.is_doc() {
+        cx.bcx.rustdocflags_args(unit)
     } else {
-        cx.bcx.rustflags_args(unit).hash(&mut hasher);
+        cx.bcx.rustflags_args(unit)
+    }
+    .into_iter();
+
+    // Ignore some flags. These may affect reproducible builds if they affect
+    // the path. The fingerprint will handle recompilation if these change.
+    while let Some(flag) = flags.next() {
+        if flag.starts_with("--remap-path-prefix=") {
+            continue;
+        }
+        if flag == "--remap-path-prefix" {
+            flags.next();
+            continue;
+        }
+        flag.hash(&mut hasher);
     }
 
     // Artifacts compiled for the host should have a different metadata
diff --git a/src/cargo/core/compiler/fingerprint.rs b/src/cargo/core/compiler/fingerprint.rs
index 89085b26403..32eff7ef6f4 100644
--- a/src/cargo/core/compiler/fingerprint.rs
+++ b/src/cargo/core/compiler/fingerprint.rs
@@ -59,7 +59,7 @@
 //! Target flags (test/bench/for_host/edition) | ✓           |
 //! -C incremental=… flag                      | ✓           |
 //! mtime of sources                           | ✓[^3]       |
-//! RUSTFLAGS/RUSTDOCFLAGS                     | ✓           |
+//! RUSTFLAGS/RUSTDOCFLAGS                     | ✓           | ✓
 //!
 //! [^1]: Build script and bin dependencies are not included.
 //!
diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs
index da5b5eb78c2..439f29dd230 100644
--- a/tests/testsuite/rustflags.rs
+++ b/tests/testsuite/rustflags.rs
@@ -1359,3 +1359,28 @@ fn env_rustflags_misspelled_build_script() {
         .with_stderr_contains("[WARNING] Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?")
         .run();
 }
+
+#[test]
+fn reamp_path_prefix_ignored() {
+    // Ensure that --remap-path-prefix does not affect metadata hash.
+    let p = project().file("src/lib.rs", "").build();
+    p.cargo("build").run();
+    let rlibs = p
+        .glob("target/debug/deps/*.rlib")
+        .collect::<Result<Vec<_>, _>>()
+        .unwrap();
+    assert_eq!(rlibs.len(), 1);
+    p.cargo("clean").run();
+
+    p.cargo("build")
+        .env(
+            "RUSTFLAGS",
+            "--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
+        )
+        .run();
+    let rlibs2 = p
+        .glob("target/debug/deps/*.rlib")
+        .collect::<Result<Vec<_>, _>>()
+        .unwrap();
+    assert_eq!(rlibs, rlibs2);
+}