Rocket: ]: The attribute `get` is currently unknown to the compiler and may have meaning added to it in the future

Created on 12 Jul 2019  路  8Comments  路  Source: SergioBenitez/Rocket

Hi,
i have the following code:

auth/mod.rs
#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

pub fn startup() {
    ::rocket::ignite().mount("/", routes![index]).launch();
}

main.rs
fn main() {
    rocket::ignite()
    auth::startup()
    .attach(Template::fairing())
    .mount("/", routes![login_page, index])
    .mount("/", StaticFiles::from("assets/"))
    .register(catchers![not_found])
    .launch();
}

but for some reason i get this error messages:

src\auth\mod.rs:1:3
: The attribute `get` is currently unknown to the compiler and may have meaning added to it in the future

 src\auth\mod.rs:7:35
cannot find macro `routes!` in this scope

Can somebody help me with my problem?

question

All 8 comments

You need to import the macros or refer to their full path: #[macro_use] extern crate rocket;, use rocket::get, or [rocket::get("/route")]

You need to import the macros or refer to their full path: #[macro_use] extern crate rocket;, use rocket::get, or [rocket::get("/route")]

This doesn't work for me.
Still the same error messages

Can you post full source and error output, then? I can't think of any other reason for that particular error here.

main.rs

#![feature(proc_macro_hygiene, decl_macro, never_type)]

#[cfg(test)] mod tests;

use rocket::request::FlashMessage;
use rocket::response::Redirect;
use rocket_contrib::templates::Template;
use rocket_contrib::serve::StaticFiles;
use std::collections::HashMap;
use rocket::Request;
use jblog::auth;

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;

#[catch(404)]
fn not_found(_req: &Request) -> Template {
    Template::render("error", "")
}

#[get("/login", rank = 2)]
fn login_page(flash: Option<FlashMessage>) -> Template {
    let mut context = HashMap::new();
    if let Some(ref msg) = flash {
        context.insert("flash", msg.msg());
    }

    Template::render("login", &context)
}

#[get("/", rank = 2)]
fn index() -> Redirect {
    Redirect::to(uri!(login_page))
}

fn main() {
    rocket::ignite()
    auth::startup()
    .attach(Template::fairing())
    .mount("/", routes![login_page, index])
    .mount("/", StaticFiles::from("assets/"))
    .register(catchers![not_found])
    .launch();
}

lib.rs

#[feature(plugin)]
#[macro_use] extern crate diesel;
extern crate dotenv;
#[macro_use] extern crate rocket_contrib;
 extern crate rocket;

use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;

pub mod schema;
pub mod models;
pub mod auth;

//check for connection for the database
pub fn establish_connection() -> PgConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");
    PgConnection::establish(&database_url)
        .expect(&format!("Error connecting to {}", database_url))
}

auth/mod.rs

use rocket::get;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

pub fn startup() {
    ::rocket::ignite().mount("/", routes![index]).launch();
}
>                   cargo build
   Compiling webLogin v0.1.0 (E:\Projects\jblog)
error[E0658]: procedural macros cannot expand to macro definitions
 --> src\auth\mod.rs:3:1
  |
3 | #[get("/")]
  | 
  |
  = note: for more information, see https://github.com/rust-lang/rust/issues/54727
  = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable

error: cannot find macro `routes!` in this scope
 --> src\auth\mod.rs:9:35
  |
9 |     ::rocket::ignite().mount("/", routes![index]).launch();
  |                                   

error[E0658]: `macro` is experimental
 --> src\auth\mod.rs:3:1
  |
3 | #[get("/")]
  | 
  |
  = note: for more information, see https://github.com/rust-lang/rust/issues/39412
  = help: add `#![feature(decl_macro)]` to the crate attributes to enable

warning: unused `#[macro_use]` import
 --> src\lib.rs:6:1
  |
6 | #[macro_use] extern crate rocket_contrib;
  | 
  |
  = note: #[warn(unused_imports)] on by default

warning: type `newUser` should have an upper camel case name
  --> src\models.rs:13:12
   |
13 | pub struct newUser {
   |          help: convert the identifier to upper camel case: `NewUser`
   |
   = note: #[warn(non_camel_case_types)] on by default

warning: unused imports: `User`, `newUser`
  --> src\lib.rs:28:20
   |
28 | use self::models::{User, newUser};
   |                   

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
error: Could not compile `webLogin`.

To learn more, run the command again with --verbose.

It looks like you added the #[macro_use] to main.rs, but use the macros in auth.rs. Since auth.rs is part of the library crate, you need the #[macro_use] in lib.rs.

It looks like you added the #[macro_use] to main.rs, but use the macros in auth.rs. Since auth.rs is part of the library crate, you need the #[macro_use] in lib.rs.

Thanks my Get works now.
But my routes! still doesn't work :(
"error: cannot find macro routes! in this scope"

Site node: it looks like you are/were also missing #![feature(proc_macro_hygiene, decl_macro)] in lib.rs, and you don't need #![feature(plugin)] for Rocket.

The lookup rules for routes! is exactly the same as #[get] - you must either #[macro_use] extern crate rocket; in the crate where routes! is used, or use rocket::routes;, or call it as rocket::routes!.

Site node: it looks like you are/were also missing #![feature(proc_macro_hygiene, decl_macro)] in lib.rs, and you don't need #![feature(plugin)] for Rocket.

The lookup rules for routes! is exactly the same as #[get] - you must either #[macro_use] extern crate rocket; in the crate where routes! is used, or use rocket::routes;, or call it as rocket::routes!.

1000 times thanks.

Its finally working.

Sorry for all my question.
i'm still a noob with rust XD

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PSeitz picture PSeitz  路  3Comments

shssoichiro picture shssoichiro  路  4Comments

klnusbaum picture klnusbaum  路  4Comments

paulvt picture paulvt  路  4Comments

haheute picture haheute  路  4Comments