Skip to content

Commit

Permalink
fix: Handle .env errors (#1987)
Browse files Browse the repository at this point in the history
If there is an error loading a .env file, we now print a warning message. There is no warning when the .env was not found because users might not have set up a .env; we only warn when the .env was found but there was some error reading it.

Fixes GH-1624
  • Loading branch information
szokeasaurusrex authored Mar 25, 2024
1 parent 980d9a8 commit 05e0b2f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,20 @@ pub fn execute() -> Result<()> {

fn setup() {
init_backtrace();
load_dotenv();

// Store the result of loading the dotenv file. We must load the dotenv file
// before setting the log level, as the log level can be set in the dotenv
// file, but we should only log a warning after setting the log level.
let load_dotenv_result = load_dotenv();

// we use debug internally but our log handler then rejects to a lower limit.
// This is okay for our uses but not as efficient.
set_max_level(LevelFilter::Debug);
set_logger(&Logger).unwrap();

if let Err(e) = load_dotenv_result {
log::warn!("Failed to load .env file: {}", e);
}
}

/// Executes the command line application and exits the process.
Expand Down
34 changes: 24 additions & 10 deletions src/utils/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::process;

use anyhow::{Error, Result};
use console::style;
use dotenv::Result as DotenvResult;
use lazy_static::lazy_static;
use regex::{Captures, Regex};

Expand Down Expand Up @@ -127,15 +128,28 @@ pub fn init_backtrace() {
pub struct QuietExit(pub i32);

/// Loads a .env file
pub fn load_dotenv() {
if env::var("SENTRY_LOAD_DOTENV")
.map(|x| x.as_str() == "1")
.unwrap_or(true)
{
if let Ok(path) = env::var("SENTRY_DOTENV_PATH") {
dotenv::from_path(path).ok();
} else {
dotenv::dotenv().ok();
}
pub fn load_dotenv() -> DotenvResult<()> {
let load_dotenv_unset = env::var("SENTRY_LOAD_DOTENV")
.map(|x| x.as_str() != "1")
.unwrap_or(false);

if load_dotenv_unset {
return Ok(());
}

match env::var("SENTRY_DOTENV_PATH") {
Ok(path) => dotenv::from_path(path),
Err(_) => dotenv::dotenv().map(|_| ()),
}
.map_or_else(
|error| {
// We only propogate errors if the .env file was found and failed to load.
if error.not_found() {
Ok(())
} else {
Err(error)
}
},
|_| Ok(()),
)
}
2 changes: 2 additions & 0 deletions tests/integration/_cases/invalid_env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# We explicitly want to include invalid-env.in, since the contents are static.
!invalid-env.in
2 changes: 2 additions & 0 deletions tests/integration/_cases/invalid_env/invalid-env.in/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Spaces in the variable assignment will produce an error reading this .env file
TEST=1 4 2
7 changes: 7 additions & 0 deletions tests/integration/_cases/invalid_env/invalid-env.trycmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```
$ sentry-cli
? failed
[..]WARN[..]Failed to load .env file:[..]
...

```
6 changes: 6 additions & 0 deletions tests/integration/invalid_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::register_test;

#[test]
fn testdinvalid_env() {
register_test("invalid_env/invalid-env.trycmd");
}
1 change: 1 addition & 0 deletions tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod deploys;
mod events;
mod help;
mod info;
mod invalid_env;
mod issues;
mod login;
mod monitors;
Expand Down

0 comments on commit 05e0b2f

Please sign in to comment.