Skip to content

Commit f88996a

Browse files
authored
Fixing a bug where empty variables in .env files were read as multi-line values (#3934) (#3951)
* Fixing a bug where empty variables in .env files were read as multi-line values * changelog * take 2 * formats * adding another test case
1 parent 6fd36a8 commit f88996a

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- Fixes issue when installing a Firebase Extension where secrets would be created before validation.
22
- Fixes issue with filtering on a specific storage bucket using functions in the emulator. (#3893)
33
- Fixes check in Cloud Functions for Firebase initialization to check for API enablement before trying to enable them. (#2574)
4-
- No longer tries to clean up function build images from Artifact Registry when Artifact Registry is not enabled (#3943)
5-
- Show error message when running `firebase init hosting:github` with no Hosting config in `firebase.json` (#3113)
4+
- No longer tries to clean up function build images from Artifact Registry when Artifact Registry is not enabled. (#3943)
5+
- Show error message when running `firebase init hosting:github` with no Hosting config in `firebase.json`. (#3113)
66
- Fixes issue where `remoteconfig:get` was not fetching the latest version by default. (#3559)
7+
- Fixes issue where empty variables in .env files would instead read as multi-line values. (#3934)

src/functions/env.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ const LINE_RE = new RegExp(
4545
"^" + // begin line
4646
"\\s*" + // leading whitespaces
4747
"(\\w+)" + // key
48-
"\\s*=\\s*" + // separator (=)
48+
"\\s*=[\\f\\t\\v]*" + // separator (=)
4949
"(" + // begin optional value
5050
"\\s*'(?:\\\\'|[^'])*'|" + // single quoted or
5151
'\\s*"(?:\\\\"|[^"])*"|' + // double quoted or
52-
"[^#\\r\\n]+" + // unquoted
52+
"[^#\\r\\n]*" + // unquoted
5353
")?" + // end optional value
5454
"\\s*" + // trailing whitespaces
5555
"(?:#[^\\n]*)?" + // optional comment
@@ -109,6 +109,7 @@ export function parse(data: string): ParseResult {
109109
v = v.replace(/\\([\\'"])/g, "$1");
110110
}
111111
}
112+
112113
envs[k] = v;
113114
}
114115

src/test/functions/env.spec.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,39 @@ BAR=bar
135135
want: { FOO: "foo" },
136136
},
137137
{
138-
description: "should ignore comments",
138+
description: "should handle empty values",
139139
input: `
140-
FOO=foo # comment
141-
# line comment 1
142-
# line comment 2
143-
BAR=bar # another comment
140+
FOO=
141+
BAR= "blah"
144142
`,
143+
want: { FOO: "", BAR: "blah" },
144+
},
145+
{
146+
description: "should handle quoted values after a newline",
147+
input: `
148+
FOO=
149+
"blah"
150+
`,
151+
want: { FOO: "blah" },
152+
},
153+
{
154+
description: "should ignore comments",
155+
input: `
156+
FOO=foo # comment
157+
# line comment 1
158+
# line comment 2
159+
BAR=bar # another comment
160+
`,
145161
want: { FOO: "foo", BAR: "bar" },
146162
},
147163
{
148164
description: "should ignore empty lines",
149165
input: `
150-
FOO=foo
166+
FOO=foo
151167
168+
BAR=bar
152169
153-
BAR=bar
154-
155-
`,
170+
`,
156171
want: { FOO: "foo", BAR: "bar" },
157172
},
158173
];

0 commit comments

Comments
 (0)