From 0565dafd87ce0fde29fcb820873dd782ae2ae090 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 22 Sep 2021 22:33:24 +0800 Subject: [PATCH] Skip all `cargo fix` that tends to write to registry cache. --- src/cargo/ops/fix.rs | 10 +++++++++- tests/testsuite/fix.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index b3e08791098..45460d26c99 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -514,7 +514,7 @@ fn rustfix_crate( // We'll generate new errors below. file.errors_applying_fixes.clear(); } - rustfix_and_fix(&mut fixes, rustc, filename, args)?; + rustfix_and_fix(&mut fixes, rustc, filename, args, config)?; let mut progress_yet_to_be_made = false; for (path, file) in fixes.files.iter_mut() { if file.errors_applying_fixes.is_empty() { @@ -556,6 +556,7 @@ fn rustfix_and_fix( rustc: &ProcessBuilder, filename: &Path, args: &FixArgs, + config: &Config, ) -> Result<(), Error> { // If not empty, filter by these lints. // TODO: implement a way to specify this. @@ -609,6 +610,8 @@ fn rustfix_and_fix( // Collect suggestions by file so we can apply them one at a time later. let mut file_map = HashMap::new(); let mut num_suggestion = 0; + // It's safe since we won't read any content under home dir. + let home_path = config.home().as_path_unlocked(); for suggestion in suggestions { trace!("suggestion"); // Make sure we've got a file associated with this suggestion and all @@ -627,6 +630,11 @@ fn rustfix_and_fix( continue; }; + // Do not write into registry cache. See rust-lang/cargo#9857. + if Path::new(&file_name).starts_with(home_path) { + continue; + } + if !file_names.clone().all(|f| f == &file_name) { trace!("rejecting as it changes multiple files: {:?}", suggestion); continue; diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 6ec21d18bd5..9fd612e8a04 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -1791,3 +1791,47 @@ fn non_edition_lint_migration() { // Check that it made the edition migration. assert!(contents.contains("from_utf8(crate::foo::FOO)")); } + +// For rust-lang/cargo#9857 +#[cargo_test] +fn fix_in_dependency() { + Package::new("bar", "1.0.0") + .file( + "src/lib.rs", + r#" + #[macro_export] + macro_rules! m { + ($i:tt) => { + let $i = 1; + }; + } + "#, + ) + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + bar = "1.0" + "#, + ) + .file( + "src/lib.rs", + r#" + pub fn foo() { + bar::m!(abc); + } + "#, + ) + .build(); + + p.cargo("fix --allow-no-vcs") + .with_stderr_does_not_contain("[FIXED] [..]") + .run(); +}