okapi: Okapi obfuscates compiler errors
Thank you for this project! I’ve been struggling with documenting an API I build, and keeping that documentation up to date. I’m trying to add okapi to my project to have the documentation be created automatically. Unfortunately, I’m running into some issues:
As it stands, Rocket is able to provide neat Rust error messages. Consider the following example:
#![feature(decl_macro, proc_macro_hygiene)]
use rocket::{get, routes};
use rocket_contrib::json::Json;
#[derive(serde::Serialize)]
struct Response {
reply: String,
}
#[get("/")]
fn my_controller() -> Json<Response> {
Json(Response {
reply: 0,
})
}
fn main() {
rocket::ignite()
.mount("/", routes![my_controller])
.launch();
}
This fails with a compile error:
[me@mycomputer tmp]$ cargo check
Checking tmp v0.1.0 (/home/me/code/rust/tmp)
error[E0308]: mismatched types
--> src/main.rs:17:16
|
17 | reply: 0,
| ^
| |
| expected struct `std::string::String`, found integer
| help: try using a conversion method: `0.to_string()`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `tmp`.
However, when the code is changed to use Okapi:
#![feature(decl_macro, proc_macro_hygiene)]
use rocket::get;
use rocket_contrib::json::Json;
use rocket_okapi::{openapi, routes_with_openapi};
use schemars::JsonSchema;
#[derive(serde::Serialize, JsonSchema)]
struct Response {
reply: String,
}
#[openapi]
#[get("/")]
fn my_controller() -> Json<Response> {
Json(Response {
reply: 0,
})
}
fn main() {
rocket::ignite()
.mount("/", routes_with_openapi![my_controller])
.launch();
}
The error message changes:
[me@mycomputer tmp]$ cargo check
Checking tmp v0.1.0 (/home/me/code/rust/tmp)
error[E0308]: mismatched types
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `tmp`.
The useful information about what caused the compilation error is gone, making it really hard to debug. Is there any way to get it back?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 15 (13 by maintainers)
Commits related to this issue
- Preserve span info when using #[openapi] (issue #12) — committed to GREsau/okapi by GREsau 4 years ago
- Preserve span info when using nested generic types (issue #12 again) — committed to GREsau/okapi by GREsau 4 years ago
- Remove fix for #12 I don't believe this is necessary in newer nightlies. It also causes its own warnings (#27). — committed to GREsau/okapi by GREsau 4 years ago
- Remove fix for #12 I don't believe this is necessary in newer nightlies. It also causes its own warnings (#27). — committed to mautamu/okapi by GREsau 4 years ago
Done - https://crates.io/crates/rocket_okapi 0.3.3 is available, hopefully this will fix everything!
This should be fixed in rocket_okapi v0.3.2, which is now published on crates.io - could you check that it works for you?
tl;dr: a fix will be out soon
So this was a weird one - I think the root cause is rust-lang/rust#43081
You can see the same problem with the following repro, where both
no_op1andno_op2should both pass through tokens unmodified (althoughno_op2does it by parsing them in then quoting them back out):The output of cargo check is then:
Note that
one_twoloses its span information, obfuscating the error.no_op1/no_op2interact exactly the same way theopenapi/getattributes do. The good news is that this also gives us a way to work-around it: by making theopenapiattribute behave more likeno_op2(by passing the tokens through syn/quote), span information will be retained.I’ll try to get the fix out imminently