Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dotenv to load multiline strings like Ruby #423

Merged
merged 2 commits into from Jan 20, 2024

Conversation

nitsujri
Copy link
Contributor

@nitsujri nitsujri commented Jan 29, 2021

Expected Behavior

dotenv should load multiline environment variables the same way Ruby loads them via CLI.

$ export MULTI_LINE_STR='this\nshould\nload with double escaped new lines'
$ irb
irb(main):001:0> ENV["MULTI_LINE_STR"]
=> "this\\nshould\\nload with double escaped new lines"

What this PR does

Conforms dotenv new line characters to double-escaped \\n like Ruby does.

Previously Raised Issues

DANGER

Potentially breaking change to those who rely on multi-line imports to work in this particular way.

Happy to discuss what is the best way to introduce this change, hence the Draft mode.

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.

Hi @nitsujri, thanks for the pull request. I agree that the behavior of dotenv should match shell. Would you be up for adding tests for this behavior?

@nitsujri
Copy link
Contributor Author

nitsujri commented Apr 10, 2021

@bkeepers Happy to help, what would you like to see in a spec beyond the small modification I did?

Since this is a breaking change, thoughts on a deprecation process? Options I see are:

  1. Total version bump
    • Documentation warning & migration guide
  2. Minor version bump
    • Add a flag/option to config/initializers/dotenv.rb that switches the two modes.
    • Defaults to old mode.
    • Add deprecation warning and next major version bump will only allow new mode.

Obviously the latter is more desirable, but is a lot more involved.

Thoughts?

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 26, 2021
@nitsujri
Copy link
Contributor Author

@bkeepers keeping from going stale, any comments?

@stale stale bot removed the wontfix label Jun 27, 2021
@bkeepers
Copy link
Owner

Happy to help, what would you like to see in a spec beyond the small modification I did?

That spec looks good. Additional specs for double quotes and no quotes might be nice to be explicit about the behavior.

Since this is a breaking change, thoughts on a deprecation process?

The current behavior was the only way to make multi-line environment variables for a long time, so I'm sure it's widely used. I know it's a lot more work, but option 2 feels like the right approach to me.

As for implementation, It could be interesting to use an environment variable to opt in to the new behavior. So adding something like DOTENV_NEWLINE_MODE=strict to the top of a .env file would enable the new behavior. The parser would just have to look for that value in its internal hash or in ENV.

@bkeepers bkeepers merged commit c95f516 into bkeepers:master Jan 20, 2024
indiebrain pushed a commit to powerhome/power-web-development-interview that referenced this pull request Feb 13, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [dotenv-rails](https://togithub.com/bkeepers/dotenv) | `2.8.1` ->
`3.0.0` |
[![age](https://developer.mend.io/api/mc/badges/age/rubygems/dotenv-rails/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/rubygems/dotenv-rails/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/rubygems/dotenv-rails/2.8.1/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/rubygems/dotenv-rails/2.8.1/3.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>bkeepers/dotenv (dotenv-rails)</summary>

###
[`v3.0.0`](https://togithub.com/bkeepers/dotenv/blob/HEAD/Changelog.md#300)

[Compare
Source](https://togithub.com/bkeepers/dotenv/compare/v2.8.1...v3.0.0)

**Breaking Changes**

- Ruby >= 3.0 and Rails >= 6.1 are now required. Lock dotenv to `~> 2.0`
if you are using an outdated Ruby or Rails version.
[bkeepers/dotenv#466,
[bkeepers/dotenv#471
- `\n` is no longer expanded into a newline in quoted strings. Use
multi-line strings with real line breaks, or set
`DOTENV_LINEBREAK_MODE=legacy` to preserve the old behavior.
[@&#8203;nitsujri](https://togithub.com/nitsujri)
[bkeepers/dotenv#423
- `ENV` will be [automatically restored between
tests](https://togithub.com/bkeepers/dotenv#autorestore-in-tests)
(`ActiveSupport::TestCase` and `Rspec`).
[bkeepers/dotenv#472,
[bkeepers/dotenv#475
- Fixed precedence when using `Dotenv::Rails.overload`. So now
`.env.development.local` will overwrite `.env.local`, which will
overwrite `.env.development`, which will overwrite `.env`.
[@&#8203;eriklovmo](https://togithub.com/eriklovmo) -
[bkeepers/dotenv#460
- The instrumentation event `dotenv.load` has been renamed to
`load.dotenv` to properly make use of namespaces in
[ActiveSupport::Notifications](https://guides.rubyonrails.org/active_support_instrumentation.html)
[bkeepers/dotenv#472

**Other improvements**

- All changes to ENV will be logged in Rails apps.
[bkeepers/dotenv#473
- Fixed an issue where `rake` loaded development files
(`.env*development`) for test-related tasks.
[bkeepers/dotenv#470
- Add `-i`/`--ignore` option to `dotenv` CLI to optionally ignore
missing files. [@&#8203;stevenharman](https://togithub.com/stevenharman)
[bkeepers/dotenv#463
- You can [customize which files get
loaded](https://togithub.com/bkeepers/dotenv#customizing-rails) by
setting `Dotenv::Rails.files`.
[bkeepers/dotenv#468

**Deprecations**

- The `dotenv-rails` gem is now superfluous. It's not technically
deprecated yet and will continue to work, but the `dotenv` gem does the
same thing.
[bkeepers/dotenv#468
- `Dotenv::Railtie` has been deprecated. Use `Dotenv::Rails`.
[bkeepers/dotenv#468
- `Dotenv.overload` has been replaced with `overwrite`. `overload` will
still work and is not technically deprecated, but documentation refers
to `Dotenv.overwrite` now.
[bkeepers/dotenv#469

**New Contributors**

- [@&#8203;stevenharman](https://togithub.com/stevenharman) made their
first contribution in
[bkeepers/dotenv#463
- [@&#8203;eriklovmo](https://togithub.com/eriklovmo) made their first
contribution in
[bkeepers/dotenv#460
- [@&#8203;nitsujri](https://togithub.com/nitsujri) made their first
contribution in
[bkeepers/dotenv#423

**Full Changelog**:
bkeepers/dotenv@v2.8.1...v3.0.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/powerhome/power-web-development-interview).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3My4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

None yet

2 participants