Warn if .env file is loaded in dotenv-load isn't explicitly set (#925)

If a `.env` file is found and loaded, but the `dotenv-load` setting hasn't been explicitly
set to true, print a warning to stderr. In approximately six months, `dotenv-load` will
change from defaulting to true to defaulting to false, which will be a potentially breaking
change in behavior for justfiles which elicit this warning.

See this issue for more details:

    https://github.com/casey/just/issues/469
This commit is contained in:
Casey Rodarmor 2021-07-28 00:33:44 -07:00 committed by GitHub
parent 9ee1a63e99
commit 1f20ca6481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 41 deletions

View File

@ -1,7 +1,5 @@
use crate::common::*; use crate::common::*;
// Remove this on 2021-07-01.
#[allow(unused)]
pub(crate) fn load_dotenv( pub(crate) fn load_dotenv(
config: &Config, config: &Config,
settings: &Settings, settings: &Settings,
@ -19,15 +17,16 @@ pub(crate) fn load_dotenv(
let path = directory.join(".env"); let path = directory.join(".env");
if path.is_file() { if path.is_file() {
// Un-comment this on 2021-07-01. if settings.dotenv_load.is_none()
// && config.verbosity.loud()
// if settings.dotenv_load.is_none() && config.verbosity.loud() { && !std::env::var_os("JUST_SUPPRESS_DOTENV_LOAD_WARNING")
// if config.color.stderr().active() { .map(|val| val.as_os_str().to_str() == Some("1"))
// eprintln!("{:#}", Warning::DotenvLoad); .unwrap_or(false)
// } else { {
// eprintln!("{}", Warning::DotenvLoad); Warning::DotenvLoad
// } .write(&mut io::stderr(), config.color.stderr())
// } .ok();
}
let iter = dotenv::from_path_iter(&path)?; let iter = dotenv::from_path_iter(&path)?;
let mut dotenv = BTreeMap::new(); let mut dotenv = BTreeMap::new();

View File

@ -25,6 +25,7 @@ impl Warning {
#[rustfmt::skip] #[rustfmt::skip]
write!(w, "\ write!(w, "\
A `.env` file was found and loaded, but this behavior will change in the future. A `.env` file was found and loaded, but this behavior will change in the future.
To silence this warning and continue loading `.env` files, add: To silence this warning and continue loading `.env` files, add:
set dotenv-load := true set dotenv-load := true
@ -33,6 +34,12 @@ To silence this warning and stop loading `.env` files, add:
set dotenv-load := false set dotenv-load := false
This warning may also be silenced by setting the `JUST_SUPPRESS_DOTENV_LOAD_WARNING`
environment variable to `1`. This can be used to silence the warning globally by
adding the following line to your shell rc file:
export JUST_SUPPRESS_DOTENV_LOAD_WARNING=1
See https://github.com/casey/just/issues/469 for more details.")?; See https://github.com/casey/just/issues/469 for more details.")?;
}, },
} }

View File

@ -60,27 +60,44 @@ test! {
stderr: "echo $DOTENV_KEY\n", stderr: "echo $DOTENV_KEY\n",
} }
// Un-comment this on 2021-07-01. #[test]
// fn warning() {
// test! { Test::new()
// name: warning, .justfile(
// justfile: r#" "
// foo: foo:
// echo $DOTENV_KEY echo $DOTENV_KEY
// "#, ",
// stdout: "dotenv-value\n", )
// stderr: " .stdout("dotenv-value\n")
// warning: A `.env` file was found and loaded, but this behavior will .stderr(
// change in the future. To silence this warning and continue loading `.env` "
// files, add: warning: A `.env` file was found and loaded, but this behavior will change in the future.
// set dotenv-load := true To \
silence this warning and continue loading `.env` files, add:
// To silence this warning and stop loading `.env` files, add: set dotenv-load := true
// set dotenv-load := false To silence \
this warning and stop loading `.env` files, add:
// See https://github.com/casey/just/issues/469 for more details. set dotenv-load := false
// echo $DOTENV_KEY
// ", This warning may \
// } also be silenced by setting the `JUST_SUPPRESS_DOTENV_LOAD_WARNING`
environment variable to `1`. \
This can be used to silence the warning globally by
adding the following line to your shell rc \
file:
export JUST_SUPPRESS_DOTENV_LOAD_WARNING=1
See https://github.com/casey/just/issues/469 \
for more details.
echo $DOTENV_KEY
",
)
.suppress_dotenv_load_warning(false)
.run();
}

View File

@ -33,16 +33,17 @@ macro_rules! test {
} }
pub(crate) struct Test { pub(crate) struct Test {
pub(crate) tempdir: TempDir, pub(crate) tempdir: TempDir,
pub(crate) justfile: Option<String>, pub(crate) justfile: Option<String>,
pub(crate) args: Vec<String>, pub(crate) args: Vec<String>,
pub(crate) env: BTreeMap<String, String>, pub(crate) env: BTreeMap<String, String>,
pub(crate) stdin: String, pub(crate) stdin: String,
pub(crate) stdout: String, pub(crate) stdout: String,
pub(crate) stderr: String, pub(crate) stderr: String,
pub(crate) stderr_regex: Option<Regex>, pub(crate) stderr_regex: Option<Regex>,
pub(crate) status: i32, pub(crate) status: i32,
pub(crate) shell: bool, pub(crate) shell: bool,
pub(crate) suppress_dotenv_load_warning: bool,
} }
impl Test { impl Test {
@ -55,12 +56,13 @@ impl Test {
args: Vec::new(), args: Vec::new(),
env: BTreeMap::new(), env: BTreeMap::new(),
justfile: Some(String::new()), justfile: Some(String::new()),
stderr_regex: None,
shell: true, shell: true,
status: EXIT_SUCCESS, status: EXIT_SUCCESS,
stderr: String::new(), stderr: String::new(),
stderr_regex: None,
stdin: String::new(), stdin: String::new(),
stdout: String::new(), stdout: String::new(),
suppress_dotenv_load_warning: true,
tempdir, tempdir,
} }
} }
@ -125,6 +127,11 @@ impl Test {
self.stdout = stdout.into(); self.stdout = stdout.into();
self self
} }
pub(crate) fn suppress_dotenv_load_warning(mut self, suppress_dotenv_load_warning: bool) -> Self {
self.suppress_dotenv_load_warning = suppress_dotenv_load_warning;
self
}
} }
impl Test { impl Test {
@ -150,6 +157,14 @@ impl Test {
let mut child = command let mut child = command
.args(self.args) .args(self.args)
.envs(&self.env) .envs(&self.env)
.env(
"JUST_SUPPRESS_DOTENV_LOAD_WARNING",
if self.suppress_dotenv_load_warning {
"1"
} else {
"0"
},
)
.current_dir(self.tempdir.path()) .current_dir(self.tempdir.path())
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())