Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
builder/LogDatum: split into more cases
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Profpatsch committed Oct 4, 2019
1 parent f3b2fdd commit ac45a47
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -129,7 +131,11 @@ pub fn run(root_nix_file: &NixFile, cas: &ContentAddressable) -> Result<Info<Sto
#[derive(Debug, PartialEq)]
enum LogDatum {
/// Nix source file (which should be tracked)
Source(PathBuf),
NixSourceFile(PathBuf),
/// A file/directory copied verbatim to the nix store
CopiedSource(PathBuf),
/// A `builtins.readFile` or `builtins.readDir` invocation (at eval time)
ReadFileOrDir(PathBuf),
/// Arbitrary text (which we couldn’t otherwise classify)
Text(OsString),
/// Text which we coudn’t decode from UTF-8
Expand All @@ -143,14 +149,20 @@ where
T: AsRef<OsStr>,
{
lazy_static! {
// These are the .nix files that are opened for evaluation.
static ref EVAL_FILE: Regex =
Regex::new("^evaluating file '(?P<source>.*)'$").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<source>.*)' -> '(?:.*)'$").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<source>.*)'$").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.
Expand All @@ -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())
}
Expand Down Expand Up @@ -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"
))
);
Expand Down

0 comments on commit ac45a47

Please sign in to comment.