Skip to content

Conversation

@grosser
Copy link
Contributor

@grosser grosser commented Apr 29, 2025

too many times I'm trying to debug someones issues and it's "oh you already set xyz api key in your env" 🤦
so Dotenv.load files, overwrite: :warn would tell them that it's already set.

... might also get annoying after a while 🤷 but wanted to raise an issue since this seems be a common gotcha

might want to make it set+warn instead, to keep the override is truthy behavior, lmk

FYI similar but hacky way of doing it:

Rake::Task["environment"].prerequisites << "dotenv_warning"
task :dotenv_warning do
  next if ENV["DOTENV_WARNING"] == "false"
  keys = Dotenv.parse(".env", overwrite: true).select { |k, v| ENV[k] && ENV[k] != v }.keys
  next if keys.empty?
  warn "Warning: Environment variables #{keys.join(", ")} are set in .env but overwritten by the environment" \
    "(set to the same value in .env or silence the warning with DOTENV_WARNING=false)"
end

Copy link
Owner

@bkeepers bkeepers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request! I get the frustration when you realize that a local variable was set and couldn't figure out why something was not working.

Would it make sense to warn by default? I wonder if there's a clean way to say which variables were set from .env and which were already set.

lib/dotenv.rb Outdated
overwrite ? new_value : old_value
case overwrite
when :warn
warn "Warning: Not overwriting ENV['#{key}'] with '#{new_value}' (is '#{old_value}')"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use the logger instead of warn? I am also hesitant about printing the values because they could contain secrets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good point, keys should be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... how do you suggest using the logger, in non-rails there is no logger right ?
removed the values and changed to .inspect just in case there is some escaping to be worried about

@grosser
Copy link
Contributor Author

grosser commented Jul 10, 2025

re "warn by default" afaik no other dotenv library does that, so I'd rather not make that the default

@grosser
Copy link
Contributor Author

grosser commented Jul 10, 2025

also added gem name to warning so people have a chance of knowing what is going on
once it looks good I'll start fixing tests, but did not want to waste time on that when I'm not sure if the approach is good

* main:
  Fix lint error
  Remove sponsor
  feat: remove to_sentence usage
  Add test for command to variable to command
  Handle parentheses in variables in commands
* main:
  Bump actions/checkout from 4 to 6
  fix a few small issues
@bkeepers bkeepers merged commit ab47820 into bkeepers:main Dec 3, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants