From ac45a47d164a07777af0f708b2b94c6a4f91097b Mon Sep 17 00:00:00 2001 From: Philip Patsch Date: Thu, 5 Sep 2019 14:51:07 +0200 Subject: [PATCH] builder/LogDatum: split into more cases We need a finer differentiation to implement the `default.nix` logic. Also add a bunch of comments to describe what is parsed in the first place. --- src/builder.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 34307c5f..0305e542 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -99,7 +99,9 @@ fn instrumented_build( .into_iter() .fold((vec![], vec![]), |(mut paths, mut log_lines), result| { match result { - LogDatum::Source(src) => { + LogDatum::CopiedSource(src) + | LogDatum::NixSourceFile(src) + | LogDatum::ReadFileOrDir(src) => { paths.push(src); } LogDatum::Text(line) => log_lines.push(line), @@ -129,7 +131,11 @@ pub fn run(root_nix_file: &NixFile, cas: &ContentAddressable) -> Result, { lazy_static! { + // These are the .nix files that are opened for evaluation. static ref EVAL_FILE: Regex = Regex::new("^evaluating file '(?P.*)'$").expect("invalid regex!"); + // When you reference a source file, nix copies it to the store and prints this. + // This the same is true for directories (e.g. `foo = ./abc` in a derivation). static ref COPIED_SOURCE: Regex = Regex::new("^copied source '(?P.*)' -> '(?:.*)'$").expect("invalid regex!"); + // These are printed for `builtins.readFile` and `builtins.readDir`, + // by our instrumentation in `./logged-evaluation.nix`. static ref LORRI_READ: Regex = Regex::new("^trace: lorri read: '(?P.*)'$").expect("invalid regex!"); } + // see the regexes above for explanations of the nix outputs match line.as_ref().to_str() { // If we can’t decode the output line to an UTF-8 string, // we cannot match against regexes, so just pass it through. @@ -159,11 +171,13 @@ where // Lines about evaluating a file are much more common, so looking // for them first will reduce comparisons. if let Some(matches) = EVAL_FILE.captures(&linestr) { - LogDatum::Source(PathBuf::from(&matches["source"])) + LogDatum::NixSourceFile(PathBuf::from(&matches["source"])) } else if let Some(matches) = COPIED_SOURCE.captures(&linestr) { - LogDatum::Source(PathBuf::from(&matches["source"])) + LogDatum::CopiedSource(PathBuf::from(&matches["source"])) + // TODO: parse files and dirs into different LogInfo cases + // to make sure we only watch directories if they were builtins.readDir’ed } else if let Some(matches) = LORRI_READ.captures(&linestr) { - LogDatum::Source(PathBuf::from(&matches["source"])) + LogDatum::ReadFileOrDir(PathBuf::from(&matches["source"])) } else { LogDatum::Text(line.as_ref().to_owned()) } @@ -224,25 +238,26 @@ mod tests { use cas::ContentAddressable; use std::ffi::OsString; use std::os::unix::ffi::OsStrExt; - use std::path::PathBuf; + use std::path::{Path, PathBuf}; + /// Parsing of `LogDatum`. #[test] - fn test_evaluation_line_to_path_evaluation() { + fn evaluation_line_to_log_datum() { assert_eq!( parse_evaluation_line("evaluating file '/nix/store/zqxha3ax0w771jf25qdblakka83660gr-source/lib/systems/for-meta.nix'"), - LogDatum::Source(PathBuf::from("/nix/store/zqxha3ax0w771jf25qdblakka83660gr-source/lib/systems/for-meta.nix")) + LogDatum::NixSourceFile(PathBuf::from("/nix/store/zqxha3ax0w771jf25qdblakka83660gr-source/lib/systems/for-meta.nix")) ); assert_eq!( parse_evaluation_line("copied source '/nix/store/zqxha3ax0w771jf25qdblakka83660gr-source/pkgs/stdenv/generic/default-builder.sh' -> '/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh'"), - LogDatum::Source(PathBuf::from("/nix/store/zqxha3ax0w771jf25qdblakka83660gr-source/pkgs/stdenv/generic/default-builder.sh")) + LogDatum::CopiedSource(PathBuf::from("/nix/store/zqxha3ax0w771jf25qdblakka83660gr-source/pkgs/stdenv/generic/default-builder.sh")) ); assert_eq!( parse_evaluation_line( "trace: lorri read: '/home/grahamc/projects/grahamc/lorri/nix/nixpkgs.json'" ), - LogDatum::Source(PathBuf::from( + LogDatum::ReadFileOrDir(PathBuf::from( "/home/grahamc/projects/grahamc/lorri/nix/nixpkgs.json" )) );