Skip to content

Commit

Permalink
Merge pull request #27 from cloudflare/fix/missing-nodes
Browse files Browse the repository at this point in the history
Fix missing node errors when importing annotations
  • Loading branch information
ObsidianMinor authored Oct 11, 2024
2 parents 307a866 + 8e5eb48 commit 02b9cdf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
14 changes: 10 additions & 4 deletions recapnc/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use recapn::ptr::{ElementSize, StructSize, UnwrapErrors};
use recapn::{any, data, text, ReaderOf};
use syn::punctuated::Punctuated;
use syn::PathSegment;
use thiserror::Error;

use crate::Result;
use crate::{Error, Result};
use crate::quotes::{
FieldDescriptor, GeneratedConst, GeneratedEnum, GeneratedField, GeneratedFile, GeneratedItem,
GeneratedRootFile, GeneratedStruct, GeneratedVariant, GeneratedWhich,
Expand Down Expand Up @@ -211,7 +210,7 @@ impl fmt::Display for NameContext {
}
}

#[derive(Debug, Error)]
#[derive(Debug, thiserror::Error)]
pub enum GeneratorError {
#[error("name for {context} is not valid UTF8: {error}")]
InvalidName {
Expand Down Expand Up @@ -253,7 +252,14 @@ fn validate_file(
});

for nested in schema.nested_items() {
validate_item(nested?, nodes, identifiers, &mut scope)?;
let nested = match nested {
Ok(n) => n,
// Unreferenced nodes from files not requested can be missing, so we just continue.
// If it turns out we needed it, an error will come up later.
Err(Error::Schema(SchemaError::MissingNode(_))) => continue,
res @ Err(_) => res?,
};
validate_item(nested, nodes, identifiers, &mut scope)?;
}

Ok(())
Expand Down
24 changes: 19 additions & 5 deletions tests/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
use std::env;
use std::path::PathBuf;

fn main() {
println!("cargo::rerun-if-changed=schemas");

recapnc::CapnpCommand::new()
.src_prefix("schemas")
.file("schemas/build.capnp")
.file("schemas/bar.capnp")
.write_to_out_dir();
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

macro_rules! run {
($base:literal: $($path:literal),*) => {
recapnc::CapnpCommand::new()
.src_prefix(concat!("schemas/", $base))
$(.file(concat!("schemas/", $base, "/", $path)))*
.write_to(out_dir.join($base))
};
}

// Simple test with imports
run!("build_rs": "build.capnp", "bar.capnp");

// Test that we ignore missing nodes when generating
run!("missing_annotation": "foo.capnp");
}
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/schemas/missing_annotation/annotate.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@0xb8db38b1a3d69f76;

annotation test(file) :Text;

annotation unused(file) :Text;
8 changes: 8 additions & 0 deletions tests/schemas/missing_annotation/foo.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@0xb782057765b9e46e;

# Add a test annotation, but don't use the "unused" annotation. It should be missing in the
# CodeGeneratorRequest and originally caused an error to occur in the code generator.
$import "annotate.capnp".test("hi!");

struct Foo {}

2 changes: 1 addition & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub mod gen;

pub mod build_gen {
include!(concat!(env!("OUT_DIR"), "/mod.rs"));
include!(concat!(env!("OUT_DIR"), "/build_rs/mod.rs"));
}

use gen::capnp_test_capnp::{TestAllTypes, TestEnum};
Expand Down

0 comments on commit 02b9cdf

Please sign in to comment.