diff --git a/src/db.rs b/src/db.rs index e42f4a0..6610bb4 100644 --- a/src/db.rs +++ b/src/db.rs @@ -333,7 +333,8 @@ impl<'a> Reader<'a> { } /// Opens or creates an on-disk database, loading its state into the provided Graph. -pub fn open(path: &str, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result { +/// Returns (fresh, Writer) where "fresh" is true when this is the first n2 run. +pub fn open(path: &str, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<(bool, Writer)> { match std::fs::OpenOptions::new() .read(true) .append(true) @@ -341,11 +342,11 @@ pub fn open(path: &str, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Resul { Ok(mut f) => { let ids = Reader::read(&mut f, graph, hashes)?; - Ok(Writer::from_opened(ids, f)) + Ok((false, Writer::from_opened(ids, f))) } Err(err) if err.kind() == std::io::ErrorKind::NotFound => { let w = Writer::create(path)?; - Ok(w) + Ok((true, w)) } Err(err) => Err(anyhow!(err)), } diff --git a/src/load.rs b/src/load.rs index b5f8697..57b4340 100644 --- a/src/load.rs +++ b/src/load.rs @@ -185,6 +185,8 @@ impl Loader { /// State loaded by read(). pub struct State { pub graph: graph::Graph, + /// True if we think this is the first time n2 has been run for this build directory. + pub fresh: bool, pub db: db::Writer, pub hashes: graph::Hashes, pub default: Vec, @@ -199,12 +201,13 @@ pub fn read(build_filename: &str) -> anyhow::Result { loader.read_file(id) })?; let mut hashes = graph::Hashes::default(); - let db = trace::scope("db::open", || { + let (fresh, db) = trace::scope("db::open", || { db::open(".n2_db", &mut loader.graph, &mut hashes) }) .map_err(|err| anyhow!("load .n2_db: {}", err))?; Ok(State { graph: loader.graph, + fresh, db, hashes, default: loader.default, diff --git a/src/run.rs b/src/run.rs index d133c10..1dcb6c4 100644 --- a/src/run.rs +++ b/src/run.rs @@ -33,9 +33,12 @@ fn build( let mut tasks_finished = 0; - // Attempt to rebuild build.ninja. let build_file_target = work.lookup(&build_filename); + // Attempt to rebuild build.ninja. if let Some(target) = build_file_target { + if state.fresh { + work.adopt = true; + } work.want_file(target)?; match trace::scope("work.run", || work.run())? { None => return Ok(None),