4

Today I found that a project that previously worked no longer compiles. Here is a minimal example that reproduces my sudden error:

use rocket::{get, launch, routes};
use rocket::response::content;

#[get("/health")]
pub fn health() -> content::Json<String> {
    content::Json("ok".parse().unwrap())
}

#[launch]
async fn rocket() -> _ {
    rocket::build().mount("/actuator", routes![health])
}
error[E0412]: cannot find type `Json` in module `content`
 --> src\main.rs:5:29
  |
5 | pub fn health() -> content::Json<String> {
  |                             ^^^^ not found in `content`
  |
help: consider importing this struct
  |
1 | use rocket::serde::json::Json;
  |

error[E0425]: cannot find function, tuple struct or tuple variant `Json` in module `content`
 --> src\main.rs:6:14
  |
6 |     content::Json("ok".parse().unwrap())
  |              ^^^^ not found in `content`
  |
help: consider importing this tuple struct
  |
1 | use rocket::serde::json::Json;
  |

I am using cargo 1.59.0 (49d8809dc 2022-02-10) and rustc 1.59.0 (9d1b2106e 2022-02-23). And here are my dependencies:

[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }

The content::Json is supposed to come from Rocket and should be enabled by the cargo feature above. It definitely worked the past. What should I do to fix this problem? How did this happen?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
spark
  • 663
  • 1
  • 5
  • 18
  • 3
    Try `content::RawJson(...)` instead of `content::Json(...)`. Looks like this changed in 0.5.0-rc.2. – Dogbert May 14 '22 at 02:26
  • 3
    It seems the `content` module has changed between release candidates: compare [`0.5.0-rc.1`](https://docs.rs/rocket/0.5.0-rc.1/rocket/response/content/index.html) and [`0.5.0-rc.2`](https://docs.rs/rocket/0.5.0-rc.2/rocket/response/content/index.html). In `rc.2` you want [`rocket::serde::json::Json`](https://docs.rs/rocket/0.5.0-rc.2/rocket/serde/json/struct.Json.html) or you can [lock the dependency to a specific version](https://stackoverflow.com/questions/45224563/how-to-specify-the-exact-version-of-a-dependency) with `"=0.5.0-rc.1"`. – kmdreko May 14 '22 at 02:27
  • But I did not upgrade the rocket version, I am still using 0.5.1.@Dogbert – spark May 14 '22 at 02:27
  • Are you sure? Inspect the lock file. – Chayim Friedman May 15 '22 at 01:44
  • I finally found out the lock the vision of rocket should add = in the reference. – spark May 15 '22 at 01:48

1 Answers1

3

The content module was changed between Rocket release candidates. Compare the documentation for 0.5.0-rc.1 and 0.5.0-rc.2. The Json type moved to rocket::serde::json::Json. So your code should look like this:

use rocket::serde::json::Json;

pub fn health() -> Json<String> {
    Json("ok".parse().unwrap())
}

But I did not upgrade the rocket version, I am still using 0.5.0-rc.1

It seems you did, albiet unknowningly. This can happen if cargo update was ran or you simply didn't preserve your Cargo.lock file (or something recreated it). It is somewhat dubious if rc.1 should be seen as compatible with rc.2, but regardless, you can lock your dependency to a specific version by prefixing it with = if you want to stay on rc.1:

[dependencies]
rocket = { version = "=0.5.0-rc.1", features = ["json"] }
kmdreko
  • 42,554
  • 6
  • 57
  • 106