Skip to content

Convert macro panics to compile errors - #21

Merged
SeriousBug merged 3 commits into
masterfrom
move-panics-to-compile-errors
Jun 30, 2026
Merged

Convert macro panics to compile errors#21
SeriousBug merged 3 commits into
masterfrom
move-panics-to-compile-errors

Conversation

@SeriousBug

Copy link
Copy Markdown
Owner

Ports the "Move Panics to Compile Errors" change from upstream rust-embed (commit 7a14d89).

What changed

The RustEmbed derive macro previously called panic!() and .unwrap() when it hit a misconfiguration during macro expansion:

  • non-unit / non-struct target
  • missing or duplicate folder attribute
  • failed shellexpand interpolation (interpolate-folder-path feature)
  • missing CARGO_MANIFEST_DIR
  • folder path with no valid string representation
  • more than one prefix attribute
  • unparseable derive input

These surfaced as opaque macro-expansion panics. Now impl_rust_embed_for_web returns syn::Result<TokenStream2> and each error path emits syn::Error::new_spanned(ast, msg). The public derive(RustEmbed) entry converts a returned Err into e.to_compile_error(), so users get a normal compile error pointing at the derive.

Valid inputs are unchanged: the generated code is identical, only the error paths differ.

Verification

  • cargo build
  • cargo test
  • cargo test --all-features

All pass.

https://claude.ai/code/session_01MixCkrYqoVEZZbPRRNtL3M

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.16832% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.74%. Comparing base (94e4a29) to head (ad6dc85).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
impl/src/lib.rs 83.16% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #21      +/-   ##
==========================================
+ Coverage   79.46%   80.74%   +1.28%     
==========================================
  Files           9        9              
  Lines         448      561     +113     
==========================================
+ Hits          356      453      +97     
- Misses         92      108      +16     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The RustEmbed derive macro previously called panic!() and .unwrap()
on misconfiguration (non-unit struct, missing/duplicate folder, bad
path interpolation, missing CARGO_MANIFEST_DIR, multiple prefixes).
These produced opaque macro-expansion panics.

Thread syn::Result through impl_rust_embed_for_web and emit
syn::Error::new_spanned(...).to_compile_error() so users get proper
compile-time diagnostics pointing at the derive instead of a panic.

Ports upstream rust-embed commit 7a14d89.

Claude-Session: https://claude.ai/code/session_01MixCkrYqoVEZZbPRRNtL3M
Apply rustfmt to the multi-argument generate_*_impl calls so the
format check passes, and add unit tests that exercise
impl_rust_embed_for_web directly: enum target, struct with fields,
missing folder attribute, duplicate folder attributes, duplicate
prefix attributes, and a valid unit struct. These cover the new
syn::Error compile-error paths that the patch added.

Claude-Session: https://claude.ai/code/session_01MixCkrYqoVEZZbPRRNtL3M
@SeriousBug
SeriousBug force-pushed the move-panics-to-compile-errors branch from 3117c26 to f3b8a75 Compare June 30, 2026 01:09
The allow_missing change on master still panicked when the folder is
missing and the opt-in is not set. Return a syn::Error like the other
misconfiguration paths so it surfaces as a normal compile error.

Add tests for both the missing-folder error and the allow_missing
opt-in success path.

Claude-Session: https://claude.ai/code/session_01MixCkrYqoVEZZbPRRNtL3M
@SeriousBug
SeriousBug merged commit 4f6400f into master Jun 30, 2026
6 checks passed
@SeriousBug
SeriousBug deleted the move-panics-to-compile-errors branch June 30, 2026 03:00
@SeriousBug SeriousBug mentioned this pull request Jun 30, 2026
SeriousBug added a commit that referenced this pull request Jun 30, 2026
* Bump version to 11.4.0

Releases changes since 11.3.0:
- Add allow_missing derive attribute (#22)
- Convert macro panics to compile errors (#21)
- Use sorted const table + binary search for embedded get() (#24)
- Update sha2 dependency to 0.11 (#25)

* Pin time below 0.3.52 in dev-dependencies

time 0.3.52 changed the Parsable::parse signature, which breaks
cookie 0.16.2 (pulled in transitively via the actix-web dev-dependency),
failing the CI doc test job. Constrain to the 0.3 series below 0.3.52 so
it unifies with cookie's requirement.
SeriousBug added a commit that referenced this pull request Jul 1, 2026
impl/src/lib.rs uses syn::Error::new_spanned (added in #21), which needs
the ToTokens trait from syn's "printing" feature. impl/Cargo.toml sets
default-features = false without it, so the crate fails to compile when
built in isolation — which is exactly what cargo publish's verification
step does. The workspace test build passed only because feature
unification pulled "printing" in via another crate.

utils 11.4.0 already published before impl failed, so bump all crates to
11.4.1 to re-release cleanly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant