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

Rebuild on mid build file modification #5417

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::str;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use core::PackageId;
use util::errors::{CargoResult, CargoResultExt};
Expand Down Expand Up @@ -79,6 +80,7 @@ pub struct BuildDeps {
pub fn prepare<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
build_start_time: SystemTime,
) -> CargoResult<(Work, Work, Freshness)> {
let _p = profile::start(format!(
"build script prepare: {}/{}",
Expand All @@ -96,7 +98,7 @@ pub fn prepare<'a, 'cfg>(

// Now that we've prep'd our work, build the work needed to manage the
// fingerprint and then start returning that upwards.
let (freshness, dirty, fresh) = fingerprint::prepare_build_cmd(cx, unit)?;
let (freshness, dirty, fresh) = fingerprint::prepare_build_cmd(cx, unit, build_start_time)?;

Ok((work_dirty.then(dirty), work_fresh.then(fresh), freshness))
}
Expand Down
90 changes: 67 additions & 23 deletions src/cargo/core/compiler/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs;
use std::hash::{self, Hasher};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use filetime::FileTime;
use serde::de::{self, Deserialize};
Expand Down Expand Up @@ -50,6 +51,7 @@ pub type Preparation = (Freshness, Work, Work);
pub fn prepare_target<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
build_start_time: SystemTime,
) -> CargoResult<Preparation> {
let _p = profile::start(format!(
"fingerprint: {} / {}",
Expand All @@ -61,7 +63,7 @@ pub fn prepare_target<'a, 'cfg>(

debug!("fingerprint at: {}", loc.display());

let fingerprint = calculate(cx, unit)?;
let fingerprint = calculate(cx, unit, build_start_time)?;
let compare = compare_old_fingerprint(&loc, &*fingerprint);
log_compare(unit, &compare);

Expand Down Expand Up @@ -199,16 +201,21 @@ where
#[derive(Serialize, Deserialize, Hash)]
enum LocalFingerprint {
Precalculated(String),
MtimeBased(MtimeSlot, PathBuf),
MtimeBased(MtimeSlot, PathBuf, SystemTime),
EnvBased(String, Option<String>),
}

impl LocalFingerprint {
fn mtime(root: &Path, mtime: Option<FileTime>, path: &Path) -> LocalFingerprint {
fn mtime(
root: &Path,
mtime: Option<FileTime>,
path: &Path,
build_start_time: SystemTime,
) -> LocalFingerprint {
let mtime = MtimeSlot(Mutex::new(mtime));
assert!(path.is_absolute());
let path = path.strip_prefix(root).unwrap_or(path);
LocalFingerprint::MtimeBased(mtime, path.to_path_buf())
LocalFingerprint::MtimeBased(mtime, path.to_path_buf(), build_start_time)
}
}

Expand All @@ -219,10 +226,21 @@ impl Fingerprint {
let mut hash_busted = false;
for local in self.local.iter() {
match *local {
LocalFingerprint::MtimeBased(ref slot, ref path) => {
LocalFingerprint::MtimeBased(ref slot, ref path, build_start_time) => {
let path = root.join(path);
let mtime = paths::mtime(&path)?;
*slot.0.lock().unwrap() = Some(mtime);
let time = FileTime::from_system_time(build_start_time);
let mtime_str = match *slot.0.lock().unwrap() {
None => "<none>".to_string(),
Some(ft) => format!("{}", ft),
};
debug!("updating fingerprint at {}, from {} to {} (rather than mtime {})",
path.display(),
mtime_str,
time,
mtime,
);
*slot.0.lock().unwrap() = Some(time);
}
LocalFingerprint::EnvBased(..) | LocalFingerprint::Precalculated(..) => continue,
}
Expand Down Expand Up @@ -284,26 +302,37 @@ impl Fingerprint {
}
}
(
&LocalFingerprint::MtimeBased(ref on_disk_mtime, ref ap),
&LocalFingerprint::MtimeBased(ref previously_built_mtime, ref bp),
&LocalFingerprint::MtimeBased(ref on_disk_mtime, ref ap, _),
&LocalFingerprint::MtimeBased(ref previously_built_mtime, ref bp, _),
) => {
let on_disk_mtime = on_disk_mtime.0.lock().unwrap();
let previously_built_mtime = previously_built_mtime.0.lock().unwrap();
let on_disk_mtime = *on_disk_mtime.0.lock().unwrap();
let previously_built_mtime = *previously_built_mtime.0.lock().unwrap();

let should_rebuild = match (*on_disk_mtime, *previously_built_mtime) {
let should_rebuild = match (on_disk_mtime, previously_built_mtime) {
(None, None) => false,
(Some(_), None) | (None, Some(_)) => true,
(Some(on_disk), Some(previously_built)) => on_disk > previously_built,
};

if should_rebuild {
let mtime1 = match previously_built_mtime {
None => "<none>".to_string(),
Some(ft) => format!("{}", ft),
};
let mtime2 = match on_disk_mtime {
None => "<none>".to_string(),
Some(ft) => format!("{}", ft),
};
let paths_are = if ap == bp {
format!("path: {}", ap.display())
} else {
format!("paths are {} and {}", ap.display(), bp.display())
};
bail!(
"mtime based components have changed: previously {:?} now {:?}, \
paths are {:?} and {:?}",
*previously_built_mtime,
*on_disk_mtime,
ap,
bp
"mtime based components have changed: previously {} now {}, {}",
mtime1,
mtime2,
paths_are,
)
}
}
Expand Down Expand Up @@ -413,6 +442,7 @@ impl<'de> de::Deserialize<'de> for MtimeSlot {
fn calculate<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
build_start_time: SystemTime,
) -> CargoResult<Arc<Fingerprint>> {
if let Some(s) = cx.fingerprints.get(unit) {
return Ok(Arc::clone(s));
Expand All @@ -429,7 +459,7 @@ fn calculate<'a, 'cfg>(
let deps = deps.iter()
.filter(|u| !u.target.is_custom_build() && !u.target.is_bin())
.map(|dep| {
calculate(cx, dep).and_then(|fingerprint| {
calculate(cx, dep, build_start_time).and_then(|fingerprint| {
let name = cx.extern_crate_name(unit, dep)?;
Ok((dep.pkg.package_id().to_string(), name, fingerprint))
})
Expand All @@ -440,7 +470,7 @@ fn calculate<'a, 'cfg>(
let local = if use_dep_info(unit) {
let dep_info = dep_info_loc(cx, unit);
let mtime = dep_info_mtime_if_fresh(unit.pkg, &dep_info)?;
LocalFingerprint::mtime(cx.files().target_root(), mtime, &dep_info)
LocalFingerprint::mtime(cx.files().target_root(), mtime, &dep_info, build_start_time)
} else {
let fingerprint = pkg_fingerprint(cx, unit.pkg)?;
LocalFingerprint::Precalculated(fingerprint)
Expand Down Expand Up @@ -505,14 +535,15 @@ fn use_dep_info(unit: &Unit) -> bool {
pub fn prepare_build_cmd<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
build_start_time: SystemTime,
) -> CargoResult<Preparation> {
let _p = profile::start(format!("fingerprint build cmd: {}", unit.pkg.package_id()));
let new = cx.files().fingerprint_dir(unit);
let loc = new.join("build");

debug!("fingerprint at: {}", loc.display());

let (local, output_path) = build_script_local_fingerprints(cx, unit)?;
let (local, output_path) = build_script_local_fingerprints(cx, unit, build_start_time)?;
let mut fingerprint = Fingerprint {
rustc: 0,
target: 0,
Expand Down Expand Up @@ -548,7 +579,8 @@ pub fn prepare_build_cmd<'a, 'cfg>(
let outputs = &outputs[&key];
if !outputs.rerun_if_changed.is_empty() || !outputs.rerun_if_env_changed.is_empty() {
let deps = BuildDeps::new(&output_path, Some(outputs));
fingerprint.local = local_fingerprints_deps(&deps, &target_root, &pkg_root);
fingerprint.local =
local_fingerprints_deps(&deps, &target_root, &pkg_root, build_start_time);
fingerprint.update_local(&target_root)?;
}
}
Expand All @@ -565,6 +597,7 @@ pub fn prepare_build_cmd<'a, 'cfg>(
fn build_script_local_fingerprints<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
build_start_time: SystemTime,
) -> CargoResult<(Vec<LocalFingerprint>, Option<PathBuf>)> {
let state = cx.build_state.outputs.lock().unwrap();
// First up, if this build script is entirely overridden, then we just
Expand Down Expand Up @@ -599,7 +632,12 @@ fn build_script_local_fingerprints<'a, 'cfg>(
// dependencies as well as env vars listed as dependencies. Process them all
// here.
Ok((
local_fingerprints_deps(deps, cx.files().target_root(), unit.pkg.root()),
local_fingerprints_deps(
deps,
cx.files().target_root(),
unit.pkg.root(),
build_start_time,
),
Some(output),
))
}
Expand All @@ -608,14 +646,20 @@ fn local_fingerprints_deps(
deps: &BuildDeps,
target_root: &Path,
pkg_root: &Path,
build_start_time: SystemTime,
) -> Vec<LocalFingerprint> {
debug!("new local fingerprints deps");
let mut local = Vec::new();
if !deps.rerun_if_changed.is_empty() {
let output = &deps.build_script_output;
let deps = deps.rerun_if_changed.iter().map(|p| pkg_root.join(p));
let mtime = mtime_if_fresh(output, deps);
local.push(LocalFingerprint::mtime(target_root, mtime, output));
local.push(LocalFingerprint::mtime(
target_root,
mtime,
output,
build_start_time,
));
}

for var in deps.rerun_if_env_changed.iter() {
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fs;
use std::io::{self, Write};
use std::path::{self, Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;

use same_file::is_same_file;
use serde_json;
Expand Down Expand Up @@ -302,13 +303,16 @@ fn compile<'a, 'cfg: 'a>(
fingerprint::prepare_init(cx, unit)?;
cx.links.validate(cx.resolve, unit)?;

let build_start_time = SystemTime::now();

let (dirty, fresh, freshness) = if unit.mode.is_run_custom_build() {
custom_build::prepare(cx, unit)?
custom_build::prepare(cx, unit, build_start_time)?
} else if unit.mode == CompileMode::Doctest {
// we run these targets later, so this is just a noop for now
(Work::noop(), Work::noop(), Freshness::Fresh)
} else {
let (mut freshness, dirty, fresh) = fingerprint::prepare_target(cx, unit)?;
let (mut freshness, dirty, fresh) =
fingerprint::prepare_target(cx, unit, build_start_time)?;
let work = if unit.mode.is_doc() {
rustdoc(cx, unit)?
} else {
Expand Down
115 changes: 114 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env;
use std::fs::{self, File};
use std::fs::{self, File, OpenOptions};
use std::io::prelude::*;
use std::net::TcpListener;
use std::thread;

use cargo::util::paths::dylib_path_envvar;
use cargo::util::{process, ProcessBuilder};
Expand Down Expand Up @@ -2809,6 +2811,117 @@ fn rebuild_preserves_out_dir() {
);
}

#[test]
fn rebuild_on_mid_build_file_modification() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();

let p = project("p")
.file(
"Cargo.toml",
r#"
[workspace]
members = ["root", "proc_macro_dep"]
"#,
)
.file(
"root/Cargo.toml",
r#"
[project]
name = "root"
version = "0.1.0"
authors = []

[dependencies]
proc_macro_dep = { path = "../proc_macro_dep" }
"#,
)
.file(
"root/src/lib.rs",
r#"
#[macro_use]
extern crate proc_macro_dep;

#[derive(Noop)]
pub struct X;
"#,
)
.file(
"proc_macro_dep/Cargo.toml",
r#"
[project]
name = "proc_macro_dep"
version = "0.1.0"
authors = []

[lib]
proc-macro = true
"#,
)
.file(
"proc_macro_dep/src/lib.rs",
&format!(
r#"
extern crate proc_macro;

use std::io::Read;
use std::net::TcpStream;
use proc_macro::TokenStream;

#[proc_macro_derive(Noop)]
pub fn noop(_input: TokenStream) -> TokenStream {{
let mut stream = TcpStream::connect("{}").unwrap();
let mut v = Vec::new();
stream.read_to_end(&mut v).unwrap();
"".parse().unwrap()
}}
"#,
addr
),
)
.build();
let root = p.root();

let t = thread::spawn(move || {
let socket = server.accept().unwrap().0;
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(root.join("root/src/lib.rs"))
.unwrap();
writeln!(file, "// modified").expect("Failed to append to root sources");
drop(file);
drop(socket);
drop(server.accept().unwrap());
});

assert_that(
p.cargo("build"),
execs().stream().with_status(0)
// .with_stderr(&format!(
// "\
//[COMPILING] proc_macro_dep v0.1.0 ({url}/proc_macro_dep)
//[COMPILING] root v0.1.0 ({url}/root)
//[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
//",
// url = p.url()
// )),
);

assert_that(
p.cargo("build"),
execs().stream().with_status(0).with_stderr(&format!(
"\
[COMPILING] root v0.1.0 ({url}/root)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
url = p.url()
)),
);

t.join().ok().unwrap();
}

#[test]
fn dep_no_libs() {
let foo = project("foo")
Expand Down