allan2 / dotenvy

A well-maintained fork of the dotenv crate
MIT License
726 stars 45 forks source link

Unable to read file #124

Closed VCCICCV closed 1 month ago

VCCICCV commented 1 month ago

My computer is Windows 11, but I see other people can use it,rustc 1.80.1 image

use axum::{ routing::get, Router };
async fn hello()-> String{
    "hello".to_string()
}
use dotenvy::dotenv;
use std::env;
#[tokio::main]
async fn main() {
    dotenv().expect("Failed to load.env file ☹");

    let port = env::var("PORT").expect("Port not set ☹");
    println!("Port: {}", port);
    // for (key, value) in env::vars() {
    //     println!("{key}: {value}");
    // }

    let app = Router::new().route("/",get(hello));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();

    axum::serve(listener, app).await.unwrap();
}

cargo.toml

axum = "0.7.5"
sea-orm = { version = "1.0.0", features = [
    "sqlx-postgres",
    "runtime-tokio-rustls",
    "macros",
] }
tower-http = { version = "0.5", features = ["fs","trace"] }
tokio = { version = "1.40.0", features = ["full"] }
dotenvy = "0.15.7"
allan2 commented 1 month ago

How are you running your program and from which directory? What is the error?

VCCICCV commented 1 month ago

您如何运行程序?从哪个目录运行?错误是什么?

Thank you.I'm running in a new project, I've solved the problem,usedotenvy::dotenv().ok();.env file can be read;dotenvy::dotenv().expect("Failed to load.env file");can not

allan2 commented 1 month ago

ok only discards the error. If you get the error with expect but not ok, it is likely your file still isn't being read.