Change dotenv-load default to false (#1082)

This changes the default value of `dotenv-load` from `true` to `false`. This
is a backwards incompatible change, and will require a minor version bump.
This commit is contained in:
Casey Rodarmor 2022-02-01 19:16:35 -08:00 committed by GitHub
parent c59e4a9e66
commit 5995221555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 84 deletions

View File

@ -1,6 +1,7 @@
#![deny(clippy::all, clippy::pedantic)] #![deny(clippy::all, clippy::pedantic)]
#![allow( #![allow(
clippy::doc_markdown, clippy::doc_markdown,
clippy::empty_enum,
clippy::enum_glob_use, clippy::enum_glob_use,
clippy::if_not_else, clippy::if_not_else,
clippy::missing_errors_doc, clippy::missing_errors_doc,

View File

@ -7,7 +7,7 @@ pub(crate) fn load_dotenv(
settings: &Settings, settings: &Settings,
working_directory: &Path, working_directory: &Path,
) -> RunResult<'static, BTreeMap<String, String>> { ) -> RunResult<'static, BTreeMap<String, String>> {
if !settings.dotenv_load.unwrap_or(true) if !settings.dotenv_load.unwrap_or(false)
&& config.dotenv_filename.is_none() && config.dotenv_filename.is_none()
&& config.dotenv_path.is_none() && config.dotenv_path.is_none()
{ {
@ -15,7 +15,7 @@ pub(crate) fn load_dotenv(
} }
if let Some(path) = &config.dotenv_path { if let Some(path) = &config.dotenv_path {
return load_from_file(config, settings, path); return load_from_file(path);
} }
let filename = config let filename = config
@ -27,35 +27,18 @@ pub(crate) fn load_dotenv(
for directory in working_directory.ancestors() { for directory in working_directory.ancestors() {
let path = directory.join(&filename); let path = directory.join(&filename);
if path.is_file() { if path.is_file() {
return load_from_file(config, settings, &path); return load_from_file(&path);
} }
} }
Ok(BTreeMap::new()) Ok(BTreeMap::new())
} }
fn load_from_file( fn load_from_file(path: &Path) -> RunResult<'static, BTreeMap<String, String>> {
config: &Config,
settings: &Settings,
path: &Path,
) -> RunResult<'static, BTreeMap<String, String>> {
// `dotenv::from_path_iter` should eventually be un-deprecated, see: // `dotenv::from_path_iter` should eventually be un-deprecated, see:
// https://github.com/dotenv-rs/dotenv/issues/13 // https://github.com/dotenv-rs/dotenv/issues/13
#![allow(deprecated)] #![allow(deprecated)]
if config.verbosity.loud()
&& settings.dotenv_load.is_none()
&& config.dotenv_filename.is_none()
&& config.dotenv_path.is_none()
&& !std::env::var_os("JUST_SUPPRESS_DOTENV_LOAD_WARNING")
.map_or(false, |val| val.as_os_str().to_str() == Some("1"))
{
eprintln!(
"{}",
Warning::DotenvLoad.color_display(config.color.stderr())
);
}
let iter = dotenv::from_path_iter(&path)?; let iter = dotenv::from_path_iter(&path)?;
let mut dotenv = BTreeMap::new(); let mut dotenv = BTreeMap::new();
for result in iter { for result in iter {

View File

@ -1,15 +1,12 @@
use crate::common::*; use crate::common::*;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub(crate) enum Warning { pub(crate) enum Warning {}
DotenvLoad,
}
impl Warning { impl Warning {
#[allow(clippy::unused_self)]
fn context(&self) -> Option<&Token> { fn context(&self) -> Option<&Token> {
match self { None
Self::DotenvLoad => None,
}
} }
} }
@ -20,30 +17,6 @@ impl ColorDisplay for Warning {
write!(f, "{} {}", warning.paint("warning:"), message.prefix())?; write!(f, "{} {}", warning.paint("warning:"), message.prefix())?;
match self {
Self::DotenvLoad => {
#[rustfmt::skip]
write!(f, "\
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:
set dotenv-load := true
To silence this warning and stop loading `.env` files, add:
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.")?;
}
}
write!(f, "{}", message.suffix())?; write!(f, "{}", message.suffix())?;
if let Some(token) = self.context() { if let Some(token) = self.context() {

View File

@ -43,6 +43,8 @@ test! {
test! { test! {
name: env_is_loaded, name: env_is_loaded,
justfile: " justfile: "
set dotenv-load
x: x:
echo XYZ echo XYZ
", ",

View File

@ -6,7 +6,7 @@ fn dotenv() {
".env": "KEY=ROOT", ".env": "KEY=ROOT",
sub: { sub: {
".env": "KEY=SUB", ".env": "KEY=SUB",
justfile: "default:\n\techo KEY=$KEY", justfile: "default:\n\techo KEY=${KEY:-unset}",
}, },
}; };
@ -21,7 +21,7 @@ fn dotenv() {
assert_eq!(output.status.code().unwrap(), 0); assert_eq!(output.status.code().unwrap(), 0);
let stdout = str::from_utf8(&output.stdout).unwrap(); let stdout = str::from_utf8(&output.stdout).unwrap();
assert_eq!(stdout, "KEY=SUB\n"); assert_eq!(stdout, "KEY=unset\n");
} }
test! { test! {
@ -61,43 +61,16 @@ test! {
} }
#[test] #[test]
fn warning() { fn no_warning() {
Test::new() Test::new()
.justfile( .justfile(
" "
foo: foo:
echo $DOTENV_KEY echo ${DOTENV_KEY:-unset}
",
)
.stdout("dotenv-value\n")
.stderr(
"
warning: 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:
set dotenv-load := true
To silence \
this warning and stop loading `.env` files, add:
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.
echo $DOTENV_KEY
", ",
) )
.stdout("unset\n")
.stderr("echo ${DOTENV_KEY:-unset}\n")
.suppress_dotenv_load_warning(false) .suppress_dotenv_load_warning(false)
.run(); .run();
} }

View File

@ -1553,6 +1553,8 @@ test! {
name: dotenv_variable_in_recipe, name: dotenv_variable_in_recipe,
justfile: " justfile: "
# #
set dotenv-load
echo: echo:
echo $DOTENV_KEY echo $DOTENV_KEY
", ",
@ -1564,6 +1566,7 @@ test! {
name: dotenv_variable_in_backtick, name: dotenv_variable_in_backtick,
justfile: " justfile: "
# #
set dotenv-load
X:=`echo $DOTENV_KEY` X:=`echo $DOTENV_KEY`
echo: echo:
echo {{X}} echo {{X}}
@ -1575,6 +1578,7 @@ test! {
name: dotenv_variable_in_function_in_recipe, name: dotenv_variable_in_function_in_recipe,
justfile: " justfile: "
# #
set dotenv-load
echo: echo:
echo {{env_var_or_default('DOTENV_KEY', 'foo')}} echo {{env_var_or_default('DOTENV_KEY', 'foo')}}
echo {{env_var('DOTENV_KEY')}} echo {{env_var('DOTENV_KEY')}}
@ -1587,6 +1591,7 @@ test! {
name: dotenv_variable_in_function_in_backtick, name: dotenv_variable_in_function_in_backtick,
justfile: " justfile: "
# #
set dotenv-load
X:=env_var_or_default('DOTENV_KEY', 'foo') X:=env_var_or_default('DOTENV_KEY', 'foo')
Y:=env_var('DOTENV_KEY') Y:=env_var('DOTENV_KEY')
echo: echo: