diff --git a/src/narinfo/narinfo.mbt b/src/narinfo/narinfo.mbt index cbb4bcd..d7c1d9a 100644 --- a/src/narinfo/narinfo.mbt +++ b/src/narinfo/narinfo.mbt @@ -24,7 +24,7 @@ fn parse_i64(key : String, v : String) -> Int64 raise @store.NixError { ///| /// Parse the text of a narinfo file. Required keys: StorePath, URL, -/// NarHash, NarSize, References. `Compression` defaults to "bzip2" when +/// NarHash, NarSize. `References` is optional (omitted when empty). `Compression` defaults to "bzip2" when /// absent (per the Nix spec); unsupported values fail at decompression time. /// Duplicate keys (except `Sig`, which may repeat) and malformed lines are /// errors. @@ -36,11 +36,14 @@ pub fn NarInfo::parse(text : String) -> NarInfo raise @store.NixError { if line.length() == 0 { continue } - guard line.find(": ") is Some(i) else { + // "Key: value", or "Key:" alone when the value is empty (e.g. no references) + let (key, value) = if line.find(": ") is Some(i) { + (line[0:i].to_owned(), line[i + 2:].to_owned()) + } else if line.has_suffix(":") { + (line[0:line.length() - 1].to_owned(), "") + } else { raise Parse("narinfo: malformed line (no ': '): \{line}") } - let key = line[0:i].to_owned() - let value = line[i + 2:].to_owned() if key == "Sig" { sigs.push(value) continue @@ -75,7 +78,8 @@ pub fn NarInfo::parse(text : String) -> NarInfo raise @store.NixError { .filter(fn(s) { s.length() > 0 }) .map(fn(s) { s.to_owned() }) .collect() - None => raise Parse("narinfo: missing required key References") + // cache.nixos.org omits the line entirely when there are no references + None => [] } for r in references { ignore(@store.StorePath::parse_base(r)) diff --git a/src/narinfo/narinfo_wbtest.mbt b/src/narinfo/narinfo_wbtest.mbt index 95d4b70..a220758 100644 --- a/src/narinfo/narinfo_wbtest.mbt +++ b/src/narinfo/narinfo_wbtest.mbt @@ -36,3 +36,23 @@ test "reject malformed narinfo" { assert_true(@store.raises(() => NarInfo::parse("URL: a\nURL: b\n"))) assert_true(@store.raises(() => NarInfo::parse("URL: a\n"))) } + +///| +test "empty References value" { + let ni = NarInfo::parse( + "StorePath: /nix/store/8padfyi6z9ipfq35j86krpvpvx3j3r3v-x-1\nURL: nar/a.nar.xz\nCompression: xz\nNarHash: sha256:00\nNarSize: 5\nReferences:\n", + ) + assert_eq(ni.references.length(), 0) + let ni2 = NarInfo::parse( + "StorePath: /nix/store/8padfyi6z9ipfq35j86krpvpvx3j3r3v-x-1\nURL: nar/a.nar.xz\nCompression: xz\nNarHash: sha256:00\nNarSize: 5\nReferences: \n", + ) + assert_eq(ni2.references.length(), 0) +} + +///| +test "absent References line means no references (as served by the real cache)" { + let ni = NarInfo::parse( + "StorePath: /nix/store/80v5n8kdscr9r2ls8h72s8ppwmccwrgm-xgcc-16.2.0-libgcc\nURL: nar/a.nar.zst\nCompression: zstd\nNarHash: sha256:00\nNarSize: 5\n", + ) + assert_eq(ni.references.length(), 0) +}