Skip to content

Commit

Permalink
Skip all cargo fix that tends to write to registry cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Sep 23, 2021
1 parent 790b01f commit 0565daf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cargo/ops/fix.rs
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand 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;
Expand Down
44 changes: 44 additions & 0 deletions tests/testsuite/fix.rs
Expand Up @@ -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();
}

0 comments on commit 0565daf

Please sign in to comment.