Skip to content

Commit

Permalink
Auto merge of #6434 - ehuss:fix-fix-concurrent, r=dwijnand
Browse files Browse the repository at this point in the history
cargo fix: fix targets with shared sources.

If `cargo fix` attempts to fix multiple targets concurrently that have shared
source files, it would apply fixes multiple times causing corruption of the
source code. Fix this by locking on the package path instead of the target
filename, essentially serializing all targets within a package.

Fixes #6415.
  • Loading branch information
bors committed Dec 13, 2018
2 parents 79f962f + 41519fb commit 45f12b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const FIX_ENV: &str = "__CARGO_FIX_PLZ";
const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
const PREPARE_FOR_ENV: &str = "__CARGO_FIX_PREPARE_FOR";
const EDITION_ENV: &str = "__CARGO_FIX_EDITION";

const IDIOMS_ENV: &str = "__CARGO_FIX_IDIOMS";

pub struct FixOptions<'a> {
Expand Down Expand Up @@ -258,9 +257,10 @@ fn rustfix_crate(
// process at a time. If two invocations concurrently check a crate then
// it's likely to corrupt it.
//
// Currently we do this by assigning the name on our lock to the first
// argument that looks like a Rust file.
let _lock = LockServerClient::lock(&lock_addr.parse()?, filename)?;
// Currently we do this by assigning the name on our lock to the manifest
// directory.
let dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is missing?");
let _lock = LockServerClient::lock(&lock_addr.parse()?, dir)?;

// Next up this is a bit suspicious, but we *iteratively* execute rustc and
// collect suggestions to feed to rustfix. Once we hit our limit of times to
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/util/lockserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
Expand Down Expand Up @@ -156,11 +155,11 @@ impl Drop for LockServerStarted {
}

impl LockServerClient {
pub fn lock(addr: &SocketAddr, name: &Path) -> Result<LockServerClient, Error> {
pub fn lock(addr: &SocketAddr, name: impl AsRef<[u8]>) -> Result<LockServerClient, Error> {
let mut client = TcpStream::connect(&addr)
.with_context(|_| "failed to connect to parent lock server")?;
client
.write_all(name.display().to_string().as_bytes())
.write_all(name.as_ref())
.and_then(|_| client.write_all(b"\n"))
.with_context(|_| "failed to write to lock server")?;
let mut buf = [0];
Expand Down
14 changes: 14 additions & 0 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,3 +1235,17 @@ fn fix_to_broken_code() {
"pub fn foo() { let x = 3; drop(x); }"
);
}

#[test]
fn fix_with_common() {
let p = project()
.file("src/lib.rs", "")
.file("tests/t1.rs", "mod common; #[test] fn t1() { common::try(); }")
.file("tests/t2.rs", "mod common; #[test] fn t2() { common::try(); }")
.file("tests/common/mod.rs", "pub fn try() {}")
.build();

p.cargo("fix --edition --allow-no-vcs").run();

assert_eq!(p.read_file("tests/common/mod.rs"), "pub fn r#try() {}");
}

0 comments on commit 45f12b1

Please sign in to comment.