vapor icon indicating copy to clipboard operation
vapor copied to clipboard

Exporting .env files does not behave same as loading file in Vapor with regard to empty strings

Open tomjoro opened this issue 3 years ago • 0 comments

When a setting is exported from a .env file empty strings will appear in environment like this:

.env SOME_SETTING=

Running: export $(xargs < config/environments/.env)

Will set in environment: SOME_SETTING=''

Now if you try to load this .env with Vapor the setting SOME_SETTING will not be set at all. Some configurations, e.g. RabbitMQ treat an empty string as a request to autogenerate so we must use an empty string for the setting.

The solution I propose is to detect empty string values and then set it. This change in Vapor.Provider.Dotenv would be this:

defp parse_pair([key, value], acc) do
    cond do
      String.length(key) > 0 && String.length(value) > 0 ->
        key = String.trim(key)
        value = String.trim(value)

        case starting_heredoc(value) do
          [_, delimiter] -> {key, delimiter, [], acc}
          _ -> [{key, value} | acc]
        end
      String.length(key) > 0 && String.length(value) == 0 ->
        key = String.trim(key)
        value = ""
        [{key, value} | acc]
      true ->
        acc
    end
  end

This fix is working for us.

I can generate a pull request but wanted to verify with you if this is correct?

Great project, thanks!

tomjoro avatar Jan 21 '21 10:01 tomjoro