Skip to content
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

Skip all cargo fix that tends to write to registry cache. #9938

Merged
merged 1 commit into from
Oct 7, 2021
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
10 changes: 9 additions & 1 deletion src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}