Skip to content

Commit

Permalink
Merge pull request #519 from bkeepers/fix-518
Browse files Browse the repository at this point in the history
Restore previous parser behavior of returning existing variables
  • Loading branch information
bkeepers authored Dec 13, 2024
2 parents 9626412 + d5872c8 commit 4c264c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/dotenv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def call
@string.scan(LINE) do
match = $LAST_MATCH_INFO

# Skip parsing values that will be ignored
next if ignore?(match[:key])

# Check for exported variable with no value
if match[:export] && !match[:value]
if existing?(match[:key])
# Use value from already defined variable
@hash[match[:key]] = ENV[match[:key]]
elsif match[:export] && !match[:value]
# Check for exported variable with no value
if !@hash.member?(match[:key])
raise FormatError, "Line #{match.to_s.inspect} has an unset variable"
end
Expand All @@ -70,8 +70,8 @@ def call

private

# Determine if the key can be ignored.
def ignore?(key)
# Determine if a variable is already defined and should not be overwritten.
def existing?(key)
!@overwrite && key != "DOTENV_LINEBREAK_MODE" && ENV.key?(key)
end

Expand Down
9 changes: 7 additions & 2 deletions spec/dotenv/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "spec_helper"

describe Dotenv::Parser do
def env(string)
Dotenv::Parser.call(string)
def env(...)
Dotenv::Parser.call(...)
end

it "parses unquoted values" do
Expand Down Expand Up @@ -298,4 +298,9 @@ def env(string)
end
end
end

it "returns existing value for redefined variable" do
ENV["FOO"] = "existing"
expect(env("FOO=bar")).to eql("FOO" => "existing")
end
end

0 comments on commit 4c264c2

Please sign in to comment.