From 3154bf89a712e1ea20255b5f56d8fb7d3b7d2ca1 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 12 Jul 2026 07:12:48 +0100 Subject: [PATCH] Report malformed schema JSON as a compile error instead of panicking Previously, import_types! unwrapped the serde_json parse result, so a syntax error in the schema file surfaced as an opaque "proc macro panicked" error. Map the parse error to a spanned syn::Error, matching the existing handling of file-open failures and preserving serde_json's line/column information. --- typify-macro/src/lib.rs | 21 ++++++++++++------- typify/tests/compile-fail/malformed-schema.rs | 6 ++++++ .../compile-fail/malformed-schema.stderr | 5 +++++ typify/tests/compile_fail.rs | 6 ++++++ 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 typify/tests/compile-fail/malformed-schema.rs create mode 100644 typify/tests/compile-fail/malformed-schema.stderr create mode 100644 typify/tests/compile_fail.rs diff --git a/typify-macro/src/lib.rs b/typify-macro/src/lib.rs index 69b74026..fd3a2dbf 100644 --- a/typify-macro/src/lib.rs +++ b/typify-macro/src/lib.rs @@ -252,14 +252,19 @@ fn do_import_types(item: TokenStream) -> Result { let path = dir.join(schema.value()); - let root_schema: schemars::schema::RootSchema = - serde_json::from_reader(std::fs::File::open(&path).map_err(|e| { - syn::Error::new( - schema.span(), - format!("couldn't read file {}: {}", schema.value(), e), - ) - })?) - .unwrap(); + let file = std::fs::File::open(&path).map_err(|e| { + syn::Error::new( + schema.span(), + format!("couldn't read file {}: {}", schema.value(), e), + ) + })?; + + let root_schema: schemars::schema::RootSchema = serde_json::from_reader(file).map_err(|e| { + syn::Error::new( + schema.span(), + format!("couldn't parse file {}: {}", schema.value(), e), + ) + })?; let mut type_space = TypeSpace::new(&settings); type_space diff --git a/typify/tests/compile-fail/malformed-schema.rs b/typify/tests/compile-fail/malformed-schema.rs new file mode 100644 index 00000000..b7a31fdb --- /dev/null +++ b/typify/tests/compile-fail/malformed-schema.rs @@ -0,0 +1,6 @@ +use typify::import_types; + +// Cargo.toml exists in trybuild's generated crate but is not valid JSON. +import_types!("Cargo.toml"); + +fn main() {} diff --git a/typify/tests/compile-fail/malformed-schema.stderr b/typify/tests/compile-fail/malformed-schema.stderr new file mode 100644 index 00000000..14eaee79 --- /dev/null +++ b/typify/tests/compile-fail/malformed-schema.stderr @@ -0,0 +1,5 @@ +error: couldn't parse file Cargo.toml: invalid type: sequence, expected struct RootSchema at line 1 column 1 + --> tests/compile-fail/malformed-schema.rs:4:15 + | +4 | import_types!("Cargo.toml"); + | ^^^^^^^^^^^^ diff --git a/typify/tests/compile_fail.rs b/typify/tests/compile_fail.rs new file mode 100644 index 00000000..1c61f8af --- /dev/null +++ b/typify/tests/compile_fail.rs @@ -0,0 +1,6 @@ +// Copyright 2026 Oxide Computer Company + +#[test] +fn test_compile_fail() { + trybuild::TestCases::new().compile_fail("tests/compile-fail/*.rs"); +}