Skip to content

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

@tomjoro

Description

@tomjoro

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions