Skip to content

Commit

Permalink
Change dotenv-load default to false (#1082)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
casey authored Feb 2, 2022
1 parent c59e4a9 commit 5995221
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(clippy::all, clippy::pedantic)]
#![allow(
clippy::doc_markdown,
clippy::empty_enum,
clippy::enum_glob_use,
clippy::if_not_else,
clippy::missing_errors_doc,
Expand Down
25 changes: 4 additions & 21 deletions src/load_dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pub(crate) fn load_dotenv(
settings: &Settings,
working_directory: &Path,
) -> 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_path.is_none()
{
return Ok(BTreeMap::new());
}

if let Some(path) = &config.dotenv_path {
return load_from_file(config, settings, path);
return load_from_file(path);
}

let filename = config
Expand All @@ -27,35 +27,18 @@ pub(crate) fn load_dotenv(
for directory in working_directory.ancestors() {
let path = directory.join(&filename);
if path.is_file() {
return load_from_file(config, settings, &path);
return load_from_file(&path);
}
}

Ok(BTreeMap::new())
}

fn load_from_file(
config: &Config,
settings: &Settings,
path: &Path,
) -> RunResult<'static, BTreeMap<String, String>> {
fn load_from_file(path: &Path) -> RunResult<'static, BTreeMap<String, String>> {
// `dotenv::from_path_iter` should eventually be un-deprecated, see:
// https://github.com/dotenv-rs/dotenv/issues/13
#![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 mut dotenv = BTreeMap::new();
for result in iter {
Expand Down
33 changes: 3 additions & 30 deletions src/warning.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use crate::common::*;

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Warning {
DotenvLoad,
}
pub(crate) enum Warning {}

impl Warning {
#[allow(clippy::unused_self)]
fn context(&self) -> Option<&Token> {
match self {
Self::DotenvLoad => None,
}
None
}
}

Expand All @@ -20,30 +17,6 @@ impl ColorDisplay for Warning {

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())?;

if let Some(token) = self.context() {
Expand Down
2 changes: 2 additions & 0 deletions tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ test! {
test! {
name: env_is_loaded,
justfile: "
set dotenv-load
x:
echo XYZ
",
Expand Down
39 changes: 6 additions & 33 deletions tests/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn dotenv() {
".env": "KEY=ROOT",
sub: {
".env": "KEY=SUB",
justfile: "default:\n\techo KEY=$KEY",
justfile: "default:\n\techo KEY=${KEY:-unset}",
},
};

Expand All @@ -21,7 +21,7 @@ fn dotenv() {
assert_eq!(output.status.code().unwrap(), 0);

let stdout = str::from_utf8(&output.stdout).unwrap();
assert_eq!(stdout, "KEY=SUB\n");
assert_eq!(stdout, "KEY=unset\n");
}

test! {
Expand Down Expand Up @@ -61,43 +61,16 @@ test! {
}

#[test]
fn warning() {
fn no_warning() {
Test::new()
.justfile(
"
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)
.run();
}
Expand Down
5 changes: 5 additions & 0 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,8 @@ test! {
name: dotenv_variable_in_recipe,
justfile: "
#
set dotenv-load
echo:
echo $DOTENV_KEY
",
Expand All @@ -1564,6 +1566,7 @@ test! {
name: dotenv_variable_in_backtick,
justfile: "
#
set dotenv-load
X:=`echo $DOTENV_KEY`
echo:
echo {{X}}
Expand All @@ -1575,6 +1578,7 @@ test! {
name: dotenv_variable_in_function_in_recipe,
justfile: "
#
set dotenv-load
echo:
echo {{env_var_or_default('DOTENV_KEY', 'foo')}}
echo {{env_var('DOTENV_KEY')}}
Expand All @@ -1587,6 +1591,7 @@ test! {
name: dotenv_variable_in_function_in_backtick,
justfile: "
#
set dotenv-load
X:=env_var_or_default('DOTENV_KEY', 'foo')
Y:=env_var('DOTENV_KEY')
echo:
Expand Down

0 comments on commit 5995221

Please sign in to comment.