Skip to content

Commit b1e2145

Browse files
MichaelGoberlingMichael Goberlingshazron
authored
fix(breaking): convert .env variables to uppercase before hoisting (#61)
Co-authored-by: Michael Goberling <mgoberling@Michaels-MacBook-Pro-3.local> Co-authored-by: Shazron Abdullah <36107+shazron@users.noreply.github.com>
1 parent 1cf208c commit b1e2145

6 files changed

+60
-1
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,24 @@ Local configuration is loaded from **$PWD/.aio**
6868

6969
### Dot Env Configuration
7070

71-
A local .env file is also loaded. This file can contain environmental variables
71+
A local .env file is also loaded. This file can contain environmental variables.
72+
73+
Note: All environment variable names will be converted to uppercase.
74+
75+
1. In the event that there are duplicate environment variables, if the uppercase version exists, it will be hoisted.
76+
77+
```
78+
aa=1
79+
aA=2
80+
AA=3 <-- Hoisted as AA
81+
```
82+
83+
2. In the event that there are duplicate environment variables with multiple case variations, the first entry will be hoisted.
84+
85+
```
86+
aa=1 <-- Hoisted as AA
87+
Aa=2
88+
```
7289

7390
## Resolving Values
7491

src/dotenv.js

+12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ module.exports = function(force = false) {
9494
try {
9595
clear()
9696
const envs = parse(file)
97+
98+
// Convert envs to uppercase
99+
for (const key in envs) {
100+
const keyName = key
101+
const uppercaseKeyName = keyName.toUpperCase()
102+
const keyValue = envs[key]
103+
if (!(uppercaseKeyName in envs)) {
104+
envs[uppercaseKeyName] = keyValue
105+
delete envs[keyName]
106+
}
107+
}
108+
97109
const newKeys = diff(envs, process.env).sort()
98110

99111
debug(`loading environment variables from ${file}`)

test/__fixtures__/lowercase

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a_a=
2+
b_b=
3+
c_c=
4+
d_d=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A_A=1
2+
a_a=2
3+
A_a=3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a_a=1
2+
A_a=2

test/dotenv.js

+21
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ describe('parse', () => {
120120
expect(process.env.D).toEqual('')
121121
})
122122

123+
test('converts lowercase names to uppercase names', () => {
124+
fs.writeFileSync('/project/.env', fixtureFile('lowercase'))
125+
dotenv()
126+
expect(process.env.A_A).toEqual('')
127+
expect(process.env.B_B).toEqual('')
128+
expect(process.env.C_C).toEqual('')
129+
expect(process.env.D_D).toEqual('')
130+
})
131+
132+
test('hoists existing uppercase, ignores other case variations', () => {
133+
fs.writeFileSync('/project/.env', fixtureFile('lowercase_dup_already_exists'))
134+
dotenv()
135+
expect(process.env.A_A).toEqual('1')
136+
})
137+
138+
test('hoists first of variations, ignores other case variations', () => {
139+
fs.writeFileSync('/project/.env', fixtureFile('lowercase_dup_last_variation'))
140+
dotenv()
141+
expect(process.env.A_A).toEqual('1')
142+
})
143+
123144
test('comment', () => {
124145
fs.writeFileSync('/project/.env', fixtureFile('comment'))
125146
dotenv()

0 commit comments

Comments
 (0)